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, [Oct 14, 2016]

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

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

56 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();

57 77577 OR tinyurl.com/answerpost
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer How many classes in your project can benefit from the Singleton pattern? a. None b. Only 1 c. Only 2 d. Only 3 e or more single {a|b|c|d|e} e.g. single c 77577 OR tinyurl.com/answerpost

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

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 ATD

60 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

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

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 Edit Sort Delete

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

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 Create/ edit/ delete/read 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 UI elements VIEW Create/ edit/ delete/read CONTROLLER MODEL Data objects

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

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 Celebrity

73 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

74 <<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

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 <<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( )

78 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( )

79 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

80 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

81 Where? → Learning → Applying

82 Where? → Learning → Applying

83 Where? → Learning → Applying

84 Which way do I face? Window or the TV? Darn…
Where? → Learning → Applying Which way do I face? Window or the TV? Darn…

85 Where? → Learning → Applying

86 Where? → Learning → Applying
Christopher Alexander

87 Abstraction occurrence Analysis patterns Singleton  Design patterns
Where? → Learning → Applying Abstraction occurrence Analysis patterns Singleton  Design patterns MVC  Architectural patterns …. Testing patterns …. Project management patterns

88 Abstraction occurrence Analysis patterns Singleton  Design patterns
Where? → Learning → Applying Abstraction occurrence Analysis patterns Singleton  Design patterns MVC  Architectural patterns …. Testing patterns …. Project management patterns

89 Where? → Learning → Applying

90 Where? → Learning → Applying

91 GoF patterns Where? → 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

92 Where? → Learning → Applying

93 Where? → Learning → Applying
Pattern So many patterns, so little time…

94 Where? → Learning → Applying
Pattern So many patterns, so little time…

95 Where? → Learning → Applying
Pattern So many patterns, so little time…

96 Can apply Singleton in your project?
Where? → Learning → Applying Can apply Singleton in your project?

97 Where? → Learning → Applying

98

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

100 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

101 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