Presentation is loading. Please wait.

Presentation is loading. Please wait.

WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.

Similar presentations


Presentation on theme: "WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010."— Presentation transcript:

1 WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint They may not show up well on other PowerPoint versions. You can download PowerPoint 2010 viewer from here. These slides contain a lot of animations. For optimal results, watch in slideshow mode.

2 Collaborating objects
How to structure? Object rules Collaborating objects Object structure Follow a paradigm OOP Object behavior 1 2 3 4 1. Abstraction 2. Encapsulation 3. Inheritance 4. ……………..

3 Advanced OO concepts

4 I don’t teach SE differently…
Male vs Female ISE vs CS vs CEG 2nd, 3rd, 4th year 2nd, 3rd, 4th year 2nd, 3rd, 4th year 2nd, 3rd, 4th year … but you learn SE differently

5 I want to register Students
Example 1 Gaaah….! I want to register Students UGStudent PGStudent NGStudent Here you go! Admin

6 Example 1 foreach Student s: s.register(); … Can? Cannot! UGStudent
Admin NGStudent PGStudent

7 Example 2 Storage s; s.load(); Logic Storage

8 Example 2 TestDriver Logic Storage StorageStub

9 Example 2 TestDriver Logic Storage StorageStub Dependency Injection

10 Dependency Injection Example 2 Storage s; … s.load(); TestDriver Logic
StorageStub setStorage(Storage) Storage s; void setStorage(Storage s){ this.s = s; } Dependency Injection

11 Dependency Injection Example 2 Storage s; … s.load(); TestDriver Logic
StorageStub setStorage(Storage) Storage s; void setStorage(Storage s){ this.s = s; } Dependency Injection

12 Example 2 TestDriver Logic Storage setStorage(Storage) StorageStub
void setStorage(Storage s){ this.s = s; }

13 Treat one type as another and still get the behavior of the actual object
Example 2 Example 1 TestDriver Logic Storage foreach Student s: s.register(); setStorage(Storage) StorageStub UGStudent Storage s; void setStorage(Storage s){ this.s = s; } Admin NGStudent PGStudent

14 Polymorphism Example 2 Example 1
Treat one type as another and still get the behavior of the actual object Example 2 Example 1 TestDriver Logic Storage foreach Student s: s.register(); setStorage(Storage) StorageStub UGStudent Storage s; void setStorage(Storage s){ this.s = s; } Admin NGStudent PGStudent

15 Polymorphism

16 Polymorphism

17 Polymorphism Different game, different behavior
Same hardware/ software

18 Polymorphism Different game, different behavior
= ability to take many forms

19 Polymorphism ?

20 Polymorphism Ability to treat different types as same type and yet get different behavior Ability to be accepted as a different type but maintain own behavior

21 Inheritence * foreach Student s: s.register(); … :UGStudent UGStudent
Admin Student NGStudent register() PGStudent

22 * foreach Student s: s.register(); … :UGStudent UGStudent Admin
NGStudent register() PGStudent

23 * foreach Student s: s.register(); … :UGStudent UGStudent Admin
NGStudent register() PGStudent

24 Substitutability If expecting super class, can accept sub class *
foreach Student s: s.register(); :UGStudent UGStudent * Admin Student NGStudent Substitutability PGStudent

25 Example 2 TestDriver StorageStub Logic setStorage(Storage) Storage
Logic logic; logic .setStorage(new StorageStub() ); Storage s; void setStorage(Storage s){ this.s = s; } load() Example 2

26 Storage s; void setStorage(Storage s){ this.s = s; } FileStorage Storage TestDriver Logic DBStorage setStorage(Storage) load() StorageStub Logic logic; logic .setStorage(new StorageStub() ); All ‘load’ the same way?

27 Overriding FileStorage Storage load() TestDriver Logic DBStorage
setStorage(Storage) load() load() Overriding StorageStub load()

28 s.load(); load() At compile time… Storage s; … FileStorage Storage
TestDriver Logic DBStorage setStorage(Storage) load() load() StorageStub load()

29 s.load(); load() :Logic s:StorageStub At run-time… Storage s; …
void setStorage(Storage s){ this.s = s; } Storage s; s.load(); FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() Logic logic; logic .setStorage(new StorageStub() ); StorageStub load()

30 s.load(); :Logic s:StorageStub load() At run-time… Storage s; …
FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() Logic logic; logic .setStorage(new StorageStub() ); StorageStub load()

31 Dynamic binding s.load(); :Logic s:??????? load() At run-time…
Storage s; s.load(); FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() StorageStub load()

32 s.load(); :Logic s:??????? load() At run-time… Storage s; … Cannot!
FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() void load (){ //do nothing } StorageStub Can remove? load()

33 load() FileStorage Storage load() TestDriver Logic DBStorage
setStorage(Storage) load() load() All Storage child classes should support a load method, but implementation is up to the child classes StorageStub load()

34 abstract void load () ; load() FileStorage Storage load() TestDriver
Logic DBStorage setStorage(Storage) load() load() StorageStub abstract void load () ; load()

35 load() {abstract} Incomplete! Cannot create objects! FileStorage
TestDriver Logic DBStorage setStorage(Storage) load() {abstract} load() StorageStub load()

36 Abstract method/class
FileStorage {abstract} Storage load() TestDriver Logic DBStorage setStorage(Storage) load() {abstract} load() StorageStub load()

37 Abstract method/class
FileStorage {abstract} Storage load() TestDriver Logic {abstract} DBStorage setStorage(Storage) load() {abstract} init() store() StorageStub A sub class must implement all inherited abstract methods, or it will become an abstract class itself load()

38 subs {a|b|c} e.g. subs b feed2103 blah blah…
Which of the subclass is not substitutable? BankSystem Account Throws exception if not 0 < i < 100 getAccount(int i) i is a parameter from BankSystem to Account AccountTypeA AccountTypeB getAccount(int i) AccountTypeC … if not 0 < i < 1000 … if not 0 < i < 100 … if not 0 < i < 10 subs {a|b|c} e.g. subs b feed2103 blah blah… OR tinyurl.com/answerpost

39 Throws exception if not 0 < i < 100
BankSystem Account Throws exception if not 0 < i < 100 getAccount(int i) i is a parameter from BankSystem to Account AccountTypeA AccountTypeB getAccount(int i) AccountTypeC … if not 0 < i < 1000 … if not 0 < i < 100 … if not 0 < i < 10 To preserve substitutability, sub classes should not impose conditions that are more restrictive than the super class.

40 Substitutability Based on Liscov Substitution Principle by Barbara Liskov To preserve substitutability, sub classes should not impose conditions that are more restrictive than the super class.

41 Command cmd = createCommand(commandString); cmd.execute();
history.add(cmd); cmd.undo(); Command execute() undo() Edit Delete Add History add(Command)

42 Command cmd = createCommand(commandString); cmd.execute();
history.add(cmd); cmd.undo(); Add {abstract} Command execute() undo() * History execute() {abstract} undo() {abstract} Delete add(Command) execute() undo() Edit execute() undo()

43 <<interface>>
Command cmd = createCommand(commandString); cmd.execute(); history.add(cmd); cmd.undo(); Add <<interface>> Command execute() undo() * History execute() undo() Delete add(Command) execute() undo() Edit execute() undo()

44 Collaborating objects
How to structure? Object rules Collaborating objects Object structure Follow a paradigm OOP Object behavior 1 2 3 4 1. Abstraction 2. Encapsulation 3. Inheritance 4. Polymorphism


Download ppt "WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010."

Similar presentations


Ads by Google