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 2010. They may not show up well on other PowerPoint versions. You can download PowerPoint 2010 viewer from here.here These slides contain a lot of animations. For optimal results, watch in slideshow mode.

2 Command cmd = createCommand(commandString); cmd.exectute(); history.add(cmd); … cmd.undo(); Command execute() undo() History add(Command) A code extract from a past CS2103 project (similar to yours) (a) 1 (b) 0..1 (c) * (d) 0 mul {a|b|c|d} e.g. mul b mul {a|b|c|d} e.g. mul b 77577

3 Command cmd = createCommand(commandString); cmd.exectute(); history.add(cmd); … cmd.undo();

4 Advanced OO concepts CS2103/T, Lecture 6, Part 1, [Sep 20, 2013]

5 Advanced OO concepts

6

7

8

9 BookStudent borrows BookStudent borrowed by

10 BookStudent borrows Where to put return date?

11 BookStudent borrows BookStudent borrows Loan returnDate

12 BookStudent borrows BookStudent borrows Loan returnDate BookStudentLoan returnDate

13 BookStudent borrows Loan returnDate

14 BookStudent borrows Loan returnDate Chapter

15 BookStudent borrows Loan returnDate Chapter

16 BookStudent borrows Loan returnDate ChapterShelf

17 BookStudent borrows Loan returnDate ChapterShelf

18 BookChapterShelf

19

20

21 BookStudent borrows Loan returnDate Chapter Shelf

22 BookStudent borrows Loan returnDate Chapter Shelf

23 BookStudent borrows Loan returnDate Chapter Shelf Where to put total students?

24 Book borrows Loan returnDate ChapterShelf You mean I can say Student.getTotal() ? Student -totalStudents +getTotal()

25 Book borrows Loan returnDate ChapterShelfStudent -totalStudents +getTotal() OK. Can I say Student.totalStudents ? You mean I can say Student.getTotal() ?

26 Book borrows Loan returnDate Student -totalStudents +getTotal()

27

28

29

30 Example 1 Admin I want to calculate average age of Students UGStudent PGStudent NGStudent Here you go!

31 Example 1 Admin Gaaah….! UGStudent PGStudent NGStudent

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

33 Example 2 StorageLogic Storage s; … s.load(); … Storage s; … s.load(); …

34 Example 2 Storage TestDriver StorageStub How? Logic

35 Example 2 Storage TestDriver StorageStub Logic

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

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

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

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

40

41 Different game, different behavior Same hardware/ software

42 = ability to take many forms

43 12

44 12

45 12

46 12 Admin UGStudent PGStudent NGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * getAge()

47 12 Admin UGStudent PGStudent NGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * getAge() :UGStudent

48 12 UGStudent PGStudent NGStudent Student getAge() is a

49 How many of these show true inheritance? Animal Dog Cat Tiger Car Engine Wheel Book TextBook Novel iPhone Telephone Camera (a) (b) (c) (d) true {0|1|2|3|4} e.g. true 3 true {0|1|2|3|4} e.g. true 3 77577

50 12 UGStudent PGStudent NGStudent Student getAge() super class : sub class parent class : child class specialization generalization

51 12 UGStudent PGStudent NGStudent Student getAge() UGStudent PGStudent NGStudent inheritance tree/hierarchy

52 Find two things that are completely different and not similar in any way.

53 Java: Single class hierarchy Object HelloWorld Square FilledSquare … … … … C++: Many class hierarchies

54 Java: Single class hierarchy Object HelloWorld Square FilledSquare … … … … C++: Many class hierarchies

55 12 Admin UGStudent PGStudent NGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * getAge() :UGStudent

56 12 Admin UGStudent PGStudent NGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * getAge() :UGStudent

57 12 Admin UGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * :UGStudent If expecting super class, can accept sub class PGStudent NGStudent

58 Which one of these subclasses are not substitutable? Account AccountTypeA AccountTypeB AccountTypeC Precondition: 0 < i < 100 Precondition: 0 < i < 10 Precondition: 0 < i < 100 Precondition: 0 < i < 1000 BankSystem subs {a|b|c} e.g. subs b subs {a|b|c} e.g. subs b 77577

59 To preserve sustitutability, sub classes should not impose preconditions that are more restrictive than the super class. AccountTypeA AccountTypeB Account AccountTypeC Precondition: 0 < i < 100 Precondition: 0 < i < 10 Precondition: 0 < i < 100 Precondition: 0 < i < 1000 BankSystem

60 Based on Liscov Substitution Principle by Barbara Liskov

61 12 Admin UGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * :UGStudent PGStudent NGStudent Example 1

62 12 Admin UGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * :UGStudent PGStudent NGStudent Example 1

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

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

65 12 TestDriver Logic setStorage(Storage) Storage load() StorageStub load() DBStorage load() FileStorage load()

66 12 TestDriver Logic setStorage(Storage) Storage load() StorageStub load() DBStorage load() FileStorage load() Storage s; … s.load(); Storage s; … s.load(); At compile time… Logic logic; … logic.setStorage(new StorageStub() ); Logic logic; … logic.setStorage(new StorageStub() );

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

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

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

70 12 TestDriver Logic setStorage(Storage) Storage load() StorageStub load() DBStorage load() FileStorage load() :Logics:?????????? At run-time… Storage s; … s.load(); Storage s; … s.load();

71 12 TestDriver Logic setStorage(Storage) Storage load() StorageStub load() DBStorage load() FileStorage load() void load (){ //do nothing } void load (){ //do nothing } Storage s; … s.load(); Storage s; … s.load(); Can remove? Cannot!

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

73 12 TestDriver Logic setStorage(Storage) Storage load() StorageStub load() DBStorage load() FileStorage load() abstract void load () ; virtual void load () = 0

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

75 12 TestDriver Logic setStorage(Storage) {abstract} Storage load() {abstract} StorageStub load() DBStorage load() FileStorage load()

76 12 TestDriver Logic setStorage(Storage) {abstract} Storage load() {abstract} init() store() StorageStub load() DBStorage load() FileStorage load()

77 12 TestDriver Logic setStorage(Storage) {abstract} Storage load() {abstract} init() {abstract} store(){abstract} StorageStub load(), init(), store() DBStorage load(), init(), store() FileStorage load(), init(), store()

78 12 TestDriver Logic setStorage(Storage) > Storage load() init() store() StorageStub load(), init(), store() DBStorage load(), init(), store() FileStorage load(), init(), store()

79 12 TestDriver Logic setStorage(Storage) > Storage load() init() store() StorageStub load(), init(), store() DBStorage load(), init(), store() FileStorage load(), init(), store() 2

80 12 TestDriver Logic setStorage(Storage) > Storage load() init() store() StorageStub load(), init(), store() DBStorage load(), init(), store() FileStorage load(), init(), store() 2 Logic logic; … logic.setStorage(new StorageStub() ); Logic logic; … logic.setStorage(new StorageStub() ); Storage s; void setStorage(Storage s){ this.s = s; } Storage s; void setStorage(Storage s){ this.s = s; }

81 12 > Storage load() init() store() 2 class interface Storage { public void load (); public void init(); } extends class StorageStub implements { … } class Storage { public: virtual void load() = 0; virtual void init() = 0; }; StorageStub load(), init(), store() DBStorage load(), init(), store() FileStorage load(), init(), store()

82 122 Command cmd = createCommand(commandString); cmd.exectute(); history.add(cmd); … cmd.undo(); Command execute() undo() Edit execute() undo() execute() undo() Delete execute() undo() execute() undo() Add execute() undo() execute() undo() History add(Command)

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

84 Command cmd = createCommand(commandString); cmd.exectute(); history.add(cmd); … cmd.undo(); 122 > Command execute() undo() Edit execute() undo() execute() undo() Delete execute() undo() execute() undo() Add execute() undo() execute() undo() History add(Command) *

85 Advanced OO Concepts

86 … gradually, and as needed.


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