Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces.

Similar presentations


Presentation on theme: "Interfaces."— Presentation transcript:

1 Interfaces

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

3 Interface ~ Protocol Interface defines ability to play a certain role.
A plane can moveUp(), moveDown(), the same functionality can be simulated by a flight simulator. Planes and flight simulators are different types of objects - still they implement the same interface (implemented differently). 2

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

5 Example A register can store objects of classes that have implemented interface IRegisterManageable. Class Person implements (realizes) the interface. <<Interface>> IRegisterManageable Register * read() write() edit() Person name The register calls the operations when needed. The operations could be implement in the following way: 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).

6 Example Continues <<Interface>> IRegisterManageable Register * 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 without changing class Register. <<Interface>> IRegisterManageable Register * read() write() edit() Car regno

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

8 Example Continues relization IStorable Register -reg 0..* 0..* edit()
<<Interface>> IStorable Register -reg 0..* 0..* edit() write() <<Interface>> relization read() IStorable edit() write() read() public void read(){ Person terminal.print("Name: "); name = terminal.readLine(); edit() ... write() } read() <<Interface>> ISortable -reg Register isLess(theOther : ISortable) : boolean isEqual(theOther : ISortable) : boolean 0..* isGreater(theOther : ISortable) : boolean SortableRegister Person

9 A Java Example: Interface ActionListener
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 addActionListener method. When the action event occurs, that object's actionPerformed method is invoked. Component Button * <<Interface> ActionListener + addActionListener(actionlistener : ActionListener) actionPerformed(e: ActionEvent) MyListener realize or implements the ActionListener interface UML 2.0 Notation: Button MyListener ActionListener MyListener actionPerformed(e: ActionEvent)

10 Sequence diagram Corresponding Class Diagram oneListener *

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

12 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!

13 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 SomeBaseClass, that you want to use as a Controller: Make a new class that inherit from Controller and from the prefabricated class.

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

15 5. Interface Contra Class What kind of classes can substitute the Controller class?
Now all classes that implements ITempChangeListener, 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 possible in Java!

16 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. IHeater ITempChange- Listener «component» Heater «component» TempSource «component» Controller «component» Cooler ICooler

17 7. Interface Contra Class
Use of interfaces advocates a new way of thinking, now focus is on roles/functionality and not on object types – it is a service/client-way of thinking . It allows more explicit information about “functional dependencies” (groups of operations) to be included in the model. Heater and Cooler is substituted with a HeaterCooler.


Download ppt "Interfaces."

Similar presentations


Ads by Google