Interfaces 1 Interface Class Class classification of objects

Interfaces 1

Interface ~ Class • Class – classification of objects based on common attributes, relations and operations. • Interface - categorization based on functionality, i. e. . , sets of operations. • A class which implements an interface agrees on supporting the specified functionality. 2

Interface ~ Protocol Interface defines ability to play a certain role. A plane can move. Up(), move. Down(), . . the same can a flight simulator. Planes and flight simulators are different types of objects - still they have the same interface (implemented differently). Interfaces as modeling elements allows us see this as the same interface. 3

A Named Set of Operations That Describes Some Behavior (No Implementation) In UML an interface is a named set of operations that characterize the behaviour of an element. stereotype interface name operation compartment ISortable interface - iconic form interface - expanded form 4

Example A register can store objects of classes that have implemented interface IRegister. Manageable. Register * <<Interface>> IRegister. Manageable read() write() edit() Person implements (realizes) the interface. Person name The register calls the operations when needed. The operations could implement the following functionality: • read() – the user is prompted for attribute values and stores the values read (e. g. , read the name of a person). • write() – the attribute values are written so that the user can see them (e. g. , write the name of the person). • edit() – the user is given the possibility to edit the attribute values (e. g. , edit the name of the person). 5

Example Continues Register * <<Interface>> IRegister. Manageable read() write() edit() Person name An object of type Register can be used with different types of objects, it can for instance be used to store Car objects (Register class does not have to be changed). Register * <<Interface>> IRegister. Manageable read() write() edit() Car regno 6

Example Continues Register <<Interface>> IRegister. Manageable * read() write() edit() Person name Interface notation alternatives – the diagram above has same meaning as the one below. Register * IRegister. Manageable Person name 7

Example Continues <<Interface>> IStorable Register -reg 0. . * relization public void read(){ terminal. print("Name: "); name = terminal. read. Line(); . . . } edit() write() read() Person edit() write() read() <<Interface>> ISortable Register IStorable Sortable. Register is. Less(the. Other : ISortable) : boolean is. Equal(the. Other : ISortable) : boolean is. Greater(the. Other : ISortable) : boolean Person 8

A Java Example: Interface Action. Listener From the Java documentation: The listener interface for receiving action events. The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's add. Action. Listener method. When the action event occurs, that object's action. Performed method is invoked. Component <<Interface> Action. Listener * Button + add. Action. Listener(actionlistener : Action. Listener) action. Performed(e: Action. Event) My. Listener realize or implements the Action. Listener interface UML 2. 0 Notation: My. Listener Action. Listener Button action. Performed(e: Action. Event) 9

Sequence diagram Corresponding Class Diagram one. Listener * 10

1. Interface Contra Class Example: part of an air conditioning simulation system Class diagram showing the structure Collaboration diagram showing a possible message sequence 11

2. Interface Contra Class What kind of classes can substitute the Controller class? If you have single inheritance all subclasses of Controller can take its place. If you have a prefabricated class and you want to use this as a Controller, than you have a problem! 12

3. Interface Contra Class What kind of classes can substitute the Controller class? If you have multiple inheritance and you have a prefabricated class (e. g. one called Some. Base. Class that you want to use as a Controller: make a new class that inherit from Controller and from the prefabricated class. 13

4. Interface Contra Class A new solution where interfaces are used: No associations directly to a class, everything is going through explicit defined interfaces. 14

5. Interface Contra Class What kind of classes can substitute the Controller class? • Now all classes that implements ITemp. Change. Listener, uses IHeater and uses ICooler can be used as a controller. • (You achieve much the same with abstract classes and multiple inheritance, but multiple inheritance is not recommended!) 15

6. Interface Contra Class Use of component notation: UML 2. 0 component: A modular part of a system that encapsulates its contents and whose manifestation is replaceable within its environment. A component defines its behavior in terms of provided and required interfaces. Same as above, but with component notation. «component» Temp. Source ITemp. Change. Listener IHeater «component» Controller «component» ICooler 16 «component» Cooler

7. Interface Contra Class • Use of interfaces advocates a new way of thinking, now focus is on roles and not on object types. Often a role can be filled by objects that are very different. • One operation can by itself be seen as an interface; by putting coherent operations into the same interface (you can also use inheritance), you put more information into the model. Heater and Cooler is substituted with a Heater. Cooler. 17
- Slides: 17