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

3

4 One-and-only-one object, shared among others

5 One-and-only-one object, shared among others
Problem: how to avoid multiple objects?

6 One-and-only-one object, shared among others
Solution: Logic +Logic( ) + getInstance( ) : Logic Logic - Logic( ) + getInstance( ) : Logic

7 One-and-only-one object, shared among others
Solution: Logic - theOne : Logic - Logic( ) + getInstance( ) : Logic

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

9 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]

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

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

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

13 experience … is valuable.

14 Learning from experience … is too costly.

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

16 Learning from experience Patterns

17 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

18 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

19 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

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

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

22 Patterns = a high-level vocabulary

23 [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.

24 [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.

25 1. Singleton 2. Façade 3. Command

26 1. Singleton 2. Façade 3. Command

27 1. Singleton 2. Façade 3. Command
Logic UI ATD

28 1. Singleton 2. Façade 3. Command
Logic TextUI MSLogic ATD

29 <<Façade>>
1. Singleton Façade Command <<Client1>> <<Façade>> <<Client2>>

30 User commands are kept as objects
1. Singleton Façade Command Edit User commands are kept as objects Sort Delete

31 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) *

32 1. Singleton 2. Façade 3. Command

33 Learning → Applying

34 Learning → Applying

35 Learning → Applying

36 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

37 Learning → Applying

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

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

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

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

42 Learning → Applying

43 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

44


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