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. These slides contain a lot of animations. For optimal results, watch in slideshow mode.
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) *
Design (a) TV Brand ModelName Price SerialNo
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
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
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
Design (b) TV Brand ModelName Price SerialNo
Design (c) TVModel Brand ModelName Price TVStockItem SerialNo
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
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
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
Book copies in a library TV drama episodes Video copies in a rental shop
Book copies in a library TV drama episodes Video copies in a rental shop
Book copies in a library TV drama episodes Video copies in a rental shop
Recurring tasks in a schedule Book copies in a library TV drama episodes Video copies in a rental shop Recurring tasks in a schedule
Using patterns to solve recurring problems Déjà vu: Using patterns to solve recurring problems CS2103/T, Lecture 9, Part 2, [March, 2017]
Using patterns to solve recurring problems Déjà vu: Using patterns to solve recurring problems
Using patterns to solve recurring problems experience Déjà vu: Using patterns to solve recurring problems
experience … is what you get when you didn’t get what you wanted.
experience … is valuable.
Learning from experience … is too costly.
experience Learning from Note to self: never volunteer to be the headstand guy
Learning from experience Patterns
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.
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 …
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.
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>> 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>>
<<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>>
<<Abstraction>> <<Occurrence>> TVModel BookTitle Lesson * * * TVStockItem BookCopy LessonDelivery * <<Abstraction>> <<Occurrence>>
<<Abstraction>> <<Occurrence>> TVModel BookTitle Lesson * * * TVStockItem BookCopy LessonDelivery * <<Abstraction>> <<Occurrence>>
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>>
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>>
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>>
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>> *
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>>
Patterns = a high-level vocabulary
Context: Multiple occurrences of some abstraction Problem: Same data (but not all) shared among occurrences of the same abstraction.
[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.
[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.
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
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
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
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?
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
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
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();
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
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();
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 77577 OR tinyurl.com/answerpost
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
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
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
<<Façade>> 1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer <<Client1>> <<Façade>> <<Client2>>
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
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
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
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
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
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
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
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
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
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
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
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
<<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
<<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( )
<<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( )
<<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( )
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( )
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 77577 OR tinyurl.com/answerpost
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
Learning → Applying
Learning → Applying
Learning → Applying
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
Learning → Applying
Learning → Applying Pattern So many patterns, so little time…
Learning → Applying Pattern So many patterns, so little time…
Learning → Applying Pattern So many patterns, so little time…
Which pattern can you apply in your project? Learning → Applying Which pattern can you apply in your project?
Learning → Applying
Part 1 - Déjà vu: Using patterns to solve recurring problems.
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
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 77577 OR tinyurl.com/answerpost