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 Explain this design in less than 10 words, without diagrams or code
Command cmd = createCommand(commandString); cmd.execute(); history.add(cmd); cmd.undo(); {abstract} Command execute() {abstract} undo() {abstract} Edit execute() undo() Delete Add History add(Command) *

3

4 Design (a) TV Brand ModelName Price SerialNo

5 Design (a) :TV Brand: Sharp ModelName: Aquos Price: 1000
SerialNo: 452-5 Design (a) TV Brand ModelName Price SerialNo :TV Brand: Sony ModelName: Bravia Price: 1200 SerialNo: 452-1 :TV Brand: Panasonic ModelName: Viera Price: 1300 SerialNo: 452-3

6 Design (a) :TV Brand: Sharp ModelName: Aquos Price: 1000
SerialNo: 452-5 :TV Brand: Sharp ModelName: Aquos Price: 1000 SerialNo: 452-7 Design (a) TV Brand ModelName Price SerialNo :TV Brand: Sony ModelName: Bravia Price: 1200 SerialNo: 452-1 :TV Brand: Sony ModelName: Bravia Price: 1200 SerialNo: 452-9 :TV Brand: Sony ModelName: Bravia Price: 1200 SerialNo: 452-4 :TV Brand: Panasonic ModelName: Viera Price: 1300 SerialNo: 452-3 :TV Brand: Panasonic ModelName: Viera Price: 1300 SerialNo: 452-2 :TV Brand: Panasonic ModelName: Viera Price: 1300 SerialNo: 452-6

7 Design (b) :AquosTV SerialNo: 452-5 AquosTV Brand: Sharp
ModelName: Aquos Price: 1000 Design (b) :AquosTV SerialNo: 452-7 TV Brand ModelName Price SerialNo :BraviaTV SerialNo: 452-1 BraviaTV Brand: Sony ModelName: Bravia Price: 1200 :BraviaTV SerialNo: 452-9 :BraviaTV SerialNo: 452-4 VieraTV Brand: Panasonic ModelName: Viera Price: 1300 :VieraTV SerialNo: 452-3 :VieraTV SerialNo: 452-2 :VieraTV SerialNo: 452-6

8 Design (b) TV Brand ModelName Price SerialNo

9 Design (c) TVModel Brand ModelName Price TVStockItem SerialNo

10 Design (c) :TVStockItem :TVModel SerialNo: 452-5 Brand: Sharp
ModelName: Aquos Price: 1000 Design (c) :TVStockItem SerialNo: 452-7 TVModel Brand ModelName Price :TVStockItem SerialNo: 452-1 :TVModel Brand: Sony ModelName: Bravia Price: 1200 :TVStockItem SerialNo: 452-9 :TVStockItem SerialNo: 452-4 TVStockItem SerialNo :TVModel Brand: Panasonic ModelName: Viera Price: 1300 :TVStockItem SerialNo: 452-3 :TVStockItem SerialNo: 452-2 :TVStockItem SerialNo: 452-6

11 Design (c) :TVStockItem :TVModel SerialNo: 452-5 Brand: Sharp
ModelName: Aquos Price: 1000 Design (c) :TVStockItem SerialNo: 452-7 TVModel Brand ModelName Price :TVStockItem SerialNo: 452-1 :TVModel Brand: Sony ModelName: Bravia Price: 1200 :TVStockItem SerialNo: 452-9 :TVStockItem SerialNo: 452-4 TVStockItem SerialNo :TVModel Brand: Panasonic ModelName: Viera Price: 1300 :TVStockItem SerialNo: 452-3 :TVStockItem SerialNo: 452-2 :TVStockItem SerialNo: 452-6

12 Design (c) :TVStockItem :TVModel SerialNo: 452-5 Brand: Sharp
ModelName: Aquos Price: 1000 Design (c) :TVStockItem SerialNo: 452-7 TVModel Brand ModelName Price :TVStockItem SerialNo: 452-1 :TVModel Brand: Sony ModelName: Bravia Price: 1200 :TVStockItem SerialNo: 452-9 :TVStockItem SerialNo: 452-4 TVStockItem SerialNo :TVModel Brand: Panasonic ModelName: Viera Price: 1300 :TVStockItem SerialNo: 452-3 :TVStockItem SerialNo: 452-2 :TVStockItem SerialNo: 452-6

13

14

15

16

17 Book copies in a library
TV drama episodes Video copies in a rental shop

18 Book copies in a library
TV drama episodes Video copies in a rental shop

19     Book copies in a library TV drama episodes
Video copies in a rental shop

20 Recurring tasks in a schedule
Book copies in a library TV drama episodes Video copies in a rental shop Recurring tasks in a schedule

21 Using patterns to solve recurring problems
Déjà vu: Using patterns to solve recurring problems CS2103/T, Lecture 9, Part 2, [March, 2017]

22 Using patterns to solve recurring problems
Déjà vu: Using patterns to solve recurring problems

23 Using patterns to solve recurring problems
experience Déjà vu: Using patterns to solve recurring problems

24 experience … is what you get when you didn’t get what you wanted.

25 experience … is valuable.

26 Learning from experience … is too costly.

27 experience Learning from
Note to self: never volunteer to be the headstand guy

28 Learning from experience Patterns

29

30 Context: Multiple stock items of same TV model Problem: Some data (but not all) shared among stock items of the same model. Solution: Represent TV model and TV stock item as different classes.

31 Context: Multiple stock items of same TV model Problem: Some data (but not all) shared among stock items of the same model. Solution: Represent TV model and TV stock item as different classes. Stock items Book copies in a library TV drama episodes

32 Context: Multiple stock items of same TV model Problem: Some data (but not all) shared among stock items of the same model. Solution: Represent TV model and TV stock item as different classes.

33 Context: Multiple occurrences of some abstraction Problem: Some data (but not all) shared among occurrences of the same abstraction. Solution: Represent abstraction and occurrences as different classes.

34 <<Abstraction>> <<Occurrence>>
Context: Multiple occurrences of some abstraction Problem: Some data (but not all) shared among occurrences of the same abstraction. Solution: Represent abstraction and occurrences as different classes. * <<Abstraction>> <<Occurrence>>

35 <<Abstraction>> <<Occurrence>>
Context: Multiple occurrences of some abstraction Problem: Same data (but not all) shared among occurrences of the same abstraction. Solution: Represent abstraction and occurrences as different classes. * <<Abstraction>> <<Occurrence>>

36 <<Abstraction>> <<Occurrence>>
TVModel BookTitle Lesson * * * TVStockItem BookCopy LessonDelivery * <<Abstraction>> <<Occurrence>>

37 <<Abstraction>> <<Occurrence>>
TVModel BookTitle Lesson * * * TVStockItem BookCopy LessonDelivery * <<Abstraction>> <<Occurrence>>

38 Name: Abstraction Occurrence Pattern
Context: Multiple occurrences of some abstraction Problem: Same data (but not all) shared among occurrences of the same abstraction. Solution: Represent abstraction and occurrences as different classes. * <<Abstraction>> <<Occurrence>>

39 Name: Abstraction Occurrence Pattern
Context: Multiple occurrences of some abstraction Problem: Same data (but not all) shared among occurrences of the same abstraction. Solution: Represent abstraction and occurrences as different classes. [An elegant solution to a recurring problem] Pattern * <<Abstraction>> <<Occurrence>>

40 Name: Abstraction Occurrence Pattern
Context: Multiple occurrences of some abstraction Problem: Same data (but not all) shared among occurrences of the same abstraction. Solution: Represent abstraction and occurrences as different classes. [An elegant solution to a recurring problem] Pattern * <<Abstraction>> <<Occurrence>>

41 Name: Abstraction Occurrence Pattern
Context: Multiple occurrences of some abstraction Problem: Same data (but not all) shared among occurrences of the same abstraction. Solution: Represent abstraction and occurrences as different classes. <<Abstraction>> <<Occurrence>> *

42 Name: Abstraction Occurrence Pattern
… for that, I used the Name: Abstraction Occurrence Pattern Show off ! Context: Multiple occurrences of some abstraction Problem: Same data (but not all) shared among occurrences of the same abstraction. Solution: Represent abstraction and occurrences as different classes. <<Abstraction>> <<Occurrence>> * * <<Abstraction>> <<Occurrence>>

43 Patterns = a high-level vocabulary

44 Context: Multiple occurrences of some abstraction Problem: Same data (but not all) shared among occurrences of the same abstraction.

45 [A stupid solution to a recurring problem]
Anti-Pattern Context: Multiple occurrences of some abstraction Problem: Same data (but not all) shared among occurrences of the same abstraction. Solution: Represent abstraction and occurrences as different classes.

46 [A stupid solution to a recurring problem]
Anti-Pattern Context: Multiple occurrences of some abstraction Problem: Same data (but not all) shared among occurrences of the same abstraction. Solution: Represent abstraction and occurrences as different classes.

47 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer

48 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer

49 Context: one-and-only-one object, shared among others
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Context: one-and-only-one object, shared among others

50 Context: one-and-only-one object, shared among others
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Context: one-and-only-one object, shared among others Problem: how to avoid multiple objects?

51 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Solution: Logic - Logic( ) + getInstance( ) : Logic Logic +Logic( ) + getInstance( ) : Logic

52 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Solution: Logic - theOne : Logic - Logic( ) + getInstance( ) : Logic

53 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Solution: public static Logic getInstance(){ if (theOne == null) theOne = new Logic(); return theOne; } Logic - theOne : Logic - Logic( ) + getInstance( ) : Logic //somewhere else in the system… Logic logic = Logic.getInstance(); //instead of … Logic logic = new Logic();

54 Why not make all members static instead? OO ↓ Testability ↓
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Why not make all members static instead? OO ↓ Testability ↓ UI Logic LogicStub

55 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Logic - theOne : Logic - Logic( ) + getInstance( ) : Logic public static Logic getInstance(){ if (theOne == null) theOne = new Logic(); return theOne; } //somewhere else in the system… Logic logic = Logic.getInstance(); //instead of … Logic logic = new Logic();

56 77577 OR tinyurl.com/answerpost
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Name one Singleton class from AB4? Is Singleton applicable for your project? single {a|b|c|d|e} e.g. single c 77577 OR tinyurl.com/answerpost

57 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer

58 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Logic UI ATD

59 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Logic TextUI MSLogic ATD

60 <<Façade>>
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer <<Client1>> <<Façade>> <<Client2>>

61 User commands are kept as objects
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Edit User commands are kept as objects Sort Delete

62 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer

63 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer

64 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer UI elements Create/ edit/ delete/read Data objects

65 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer UI elements VIEW Create/ edit/ delete/read CONTROLLER MODEL Data objects

66 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer UI elements VIEW Create/ edit/ delete/read CONTROLLER MODEL Data objects

67 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer

68 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Paparazzi

69 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Paparazzi Celebrity

70 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Paparazzi Celebrity

71 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Paparazzi Celebrity

72 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Paparazzi = Observers AddUI ListUI SummaryUI Celebrity = Observable Data

73 <<Interface>>
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer = Observers AddUI ListUI SummaryUI update( ) update( ) = Observable Data <<Interface>> Observer * +addView(Observer) -notifyUIs( ) update( ) :Data :Observer :Observer :Observer

74 <<Interface>>
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer ListUI SummaryUI update( ) update( ) Data <<Interface>> Observer * +addView(Observer) -notifyUIs( ) update( )

75 <<Interface>>
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer ListUI SummaryUI update( ) update( ) Data <<Interface>> Observer * +addView(Observer) -notifyUIs( ) update( )

76 <<Interface>>
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer ListUI SummaryUI update( ) update( ) Data <<Interface>> Observer * +addView(Observer) -notifyUIs( ) update( )

77 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer <<Observer1>> <<Observer2>> update( ) update( ) <<Observable>> <<Interface>> Observer * +add(Observer) -notifyObservers( ) update( )

78 observer {yes|no} e.g. observer yes
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer Is the Observer pattern used in your project? <<Observer1>> <<Observer2>> update( ) update( ) <<Observable>> <<Interface>> Observer Can be used to reduce by directional dependencies. * +add(Observer) -notifyObservers( ) update( ) observer {yes|no} e.g. observer yes OR tinyurl.com/answerpost

79 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer

80 Learning → Applying

81 Learning → Applying

82 Learning → Applying

83 GoF patterns Learning → Applying Creational: Behavioral :
Abstract Factory Builder Factory Method Prototype Singleton Behavioral : Chain of Responsibility Command Interpreter Template Method Iterator Memento State Visitor Mediator Observer Strategy Structural: Adapter Bridge Composite Decorator Façade Flyweight Proxy GoF patterns

84 Learning → Applying

85 Learning → Applying Pattern So many patterns, so little time…

86 Learning → Applying Pattern So many patterns, so little time…

87 Learning → Applying Pattern So many patterns, so little time…

88 Which pattern can you apply in your project?
Learning → Applying Which pattern can you apply in your project?

89 Learning → Applying

90

91 Part 1 - Déjà vu: Using patterns to solve recurring problems.

92 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer 7. …… Identify patterns in sample code Use more patterns if it helps Should be able to relate each pattern to the project

93 77577 OR tinyurl.com/answerpost
What are the negative consequence of applying the façade pattern? a. Extra code. b. Slower performance. c. Both of the above. d. No negative consequences. negative {a|b|c|d} e.g. negative c OR tinyurl.com/answerpost


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