Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.

Similar presentations


Presentation on theme: "Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML."— Presentation transcript:

1 Class Relationships Lecture Oo08 Polymorphism

2 References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML Distilled Applying the Standard Object Modeling Language, AWL, 1997, Chapt 4 n Booch, Object Oriented Analysis and Design, Chapt 3

3 Teaching Points n Abstract Classes n Polymorphism n Late-Binding Mechanisms n Interfaces n When/Why to use Polymorhism? n Patterns –Case/Enum –Factory Method

4 Review n What is the difference between type and class? n What is an operation signature?

5 Abstract Classes n Are objects normally instantiated from every class in a system?

6 Polymorphism n Overloading (ad hoc polymorphism) –Binding based on number and type of arguments –I.e. operation signature –A static binding mechanism (compile/link - time binding)

7 Polymorphism n polymorphism (parametric polymorphism) –depends on type of actual parameter of function call at run time –A dynamic binding mechanism (late- binding)(run-time binding)

8 Polymorphism n Allows us to treat the same kinds of objects in the same way n Objects with the same interface can be treated in the same way n Objects of different types can be treated in the same way provided they have a common base-type n A VERY POWERFUL MECHANISM!

9 Things Needed for a Polymorphic Function Call n Common methods in different classes such that the classes are substitutable for each other, and a late binding mechanism n In statically typed langagues (e.g. Java, C++) –Base-class Object Reference –Over ridden methods n REMEMBER THESE!

10 Operation Overriding class shape { private point centre; private int id; public int getId(){… } public point getCentre(){… } public void draw(){… }// overridden operation } class circle extends shape { private double radius; public void draw(){… }//overriding method (same signature) }

11 Example n List of shapes n Drawing the list

12 Abstract Classes n Provide a common interface n Abstract operations n A class with all abstract operations and no state, like a Java interface n Interface-inheritance

13 Abstract Operations abstract class shape { // Now an abstract class private point centre; private int id; public int getId(){… } public point getCentre(){… } public abstract void draw(); // Now an abstract operation } class circle extends shape { private double radius; public void draw(){… }//overriding method (same signature) }

14 Sub-typing without Sub-classing n Java Interfaces n Abstract classes with no field and no operation implementations n These ideas only come into play when working with the implementation perspective

15 Modeling an Interface

16 Alternate Interface Notation n Lollipop n Equivalent to last slide

17 Implementing an Interface interface shape { // Now an interface public int getId(); // All operations now abstract public point getCentre(); public void draw(); } class circle implements shape { private point centre; //State variables have to move to private int id; //the implementing class private double radius; public void draw(){… }//overriding method (same signature) }

18 Sub-classing without Sub-typing n Private and Protected Inheritance n Allows us to reuse a class without having to present its interface to the rest of the world n The base class is concrete n Can be similar to aggregation

19 When to Use Polymorphism n When we want to treat a set of things in the same way, even though they may not be objects of the same concrete class –The concept of an abstract interface is critical –Probably the prime reason for using generalization/inheritance

20 What is a Pattern? n “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” –Christopher Alexander

21 What is a Pattern? n “A design pattern describes a commonly-recurring structure of communicating components that solve a general design problem in a particular context.” –Gamma, et al.

22 Why Use Patterns? n Advantages of design patterns are reusability and modularity, which are precisely the goals of object-oriented software design. n Because design patterns capture expert knowledge, the solutions it provides are tried and true. Therefore, they not only make it less time-consuming to design a large system, it also improves design quality.

23 Why Use Patterns? n Design patterns provide a common vocabulary for designers to communicate by expressing software design at higher level of abstraction than that of a design notation or programming language. n Being able to recognise design patterns helps one to understand existing systems.

24 Pattern Examples n Meta-pattern for case/enum n Factory

25 Case/Enum //Fertilization schedule switch(fruitType) { case APPLES: //apples fertilization code break; case ORANGES: //oranges fertilization code break; case PEARS: //pears fertilization code break; }

26 Case/Enum

27 Factory Method n Intent –Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. n Also Known As –Virtual Constructor

28 Factory Method n Use the Factory Method pattern when: –a class can't anticipate the class of objects it must create. –a class wants its subclasses to specify the objects it creates. –classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.

29 Factory Method

30 Teaching Points n Abstract Classes n Polymorphism n Late-Binding Mechanisms n Interfaces n When/Why to use Polymorhism? n Patterns –Case/Enum –Factory Method


Download ppt "Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML."

Similar presentations


Ads by Google