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.
One-and-only-one object, shared among others
One-and-only-one object, shared among others Problem: how to avoid multiple objects?
One-and-only-one object, shared among others Solution: Logic +Logic( ) + getInstance( ) : Logic Logic - Logic( ) + getInstance( ) : Logic
One-and-only-one object, shared among others Solution: Logic - theOne : Logic - Logic( ) + getInstance( ) : Logic
One-and-only-one object, shared among others 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();
Using patterns to solve recurring problems Part I Déjà vu: Using patterns to solve recurring problems Part I CS2103/T, Lecture 9, Part 2, [March, 2018]
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: One-and-only-one Logic object, but shared by others Problem: Need to control the construction of the Logic object, but allow sharing Solution: Use private constructor and maintain a static reference for the only copy of the Logic object Logic - theOne : Logic - Logic( ) + getInstance( ) : Logic
Name: Singleton Pattern Context: One-and-only-one object, but shared by others Problem: Need to control the construction, but allow sharing Solution: Use private constructor and maintain a static reference for the only copy of the object Logic - theOne : Logic - Logic( ) + getInstance( ) : Logic Singleton - theOne : Singleton - Singleton( ) + getInstance( ) : Singleton
Name: Singleton Pattern Context: One-and-only-one object, but shared by others Problem: Need to control the construction, but allow sharing Solution: Use private constructor and maintain a static reference for the only copy of the object [An elegant solution to a recurring problem] Pattern Singleton - theOne : Singleton - Singleton( ) + getInstance( ) : Singleton
Name: Singleton Pattern Context: One-and-only-one object, but shared by others Problem: Need to control the construction, but allow sharing Solution: Use private constructor and maintain a static reference for the only copy of the object [An elegant solution to a recurring problem] Pattern <<Singleton>> - theOne : <<Singleton>> - Singleton( ) + getInstance( ) : <<Singleton>>
Name: Singleton Pattern … for that, I used the Name: Singleton Pattern Show off ! Context: One-and-only-one object, but shared by others Problem: Need to control the construction, but allow sharing Solution: Use private constructor and maintain a static reference for the only copy of the object <<Singleton>> - theOne : <<Singleton>> - Singleton( ) + getInstance( ) : <<Singleton>>
Patterns = a high-level vocabulary
[A stupid solution to a recurring problem] Anti-Pattern Context: One-and-only-one object, but shared by others Problem: Need to control the construction, but allow sharingSolution: Represent abstraction and occurrences as different classes.
[A stupid solution to a recurring problem] Anti-Pattern Context: One-and-only-one object, but shared by others Problem: Need to control the construction, but allow sharingSolutionSolution: Represent abstraction and occurrences as different classes.
1. Singleton 2. Façade 3. Command
1. Singleton 2. Façade 3. Command
1. Singleton 2. Façade 3. Command Logic UI ATD
1. Singleton 2. Façade 3. Command Logic TextUI MSLogic ATD
<<Façade>> 1. Singleton 2. Façade 3. Command <<Client1>> <<Façade>> <<Client2>>
User commands are kept as objects 1. Singleton 2. Façade 3. Command Edit User commands are kept as objects Sort Delete
Explain this design in less than 5 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) *
1. Singleton 2. Façade 3. Command
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
1. Singleton 2. Façade 3. Command 4. More to come…. Identify patterns in sample code Use more patterns if it helps Should be able to relate each pattern to the project