Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 200391.3913 R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.

Similar presentations


Presentation on theme: "March 200391.3913 R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software."— Presentation transcript:

1 March 200391.3913 R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software 1995 GoF Patterns – Ch 23

2 March 200391.3913 R McFadyen2 Design Patterns contains the GoF patterns including: Abstract factory (23.3) Builder Factory method Prototype Singleton (23.4) These are creational patterns for the instantiation process Help to make a system independent of how its objects are created They hide how instances of classes are created GoF Patterns

3 March 200391.3913 R McFadyen3 Factory (23.3) Problem: who should be responsible for creating objects when there are special considerations such as complex creational logic, … Solution: create a Pure Fabrication object called a Factory that handles the creation Factory Pattern

4 March 200391.3913 R McFadyen4 Textbook example The NextGen POS system supports several kinds of 3 rd party services. For example: tax calculators such as TaxMaster and Good As Gold, account packages such as SAP and Great Northern. The support is provided through “adapters”. The text is concerned with determining the class that should create the required adapters. Choosing a class in the domain model will lower the cohesion of that class. Applying the Factory Pattern, we would then have a Factory object that is given the responsibility to create such adapter objects. Factory Pattern

5 March 200391.3913 R McFadyen5 Singleton (23.4) Problem: Exactly one instance of a certain object is required (this object is called a singleton). Ensure a class has only one instance. (Factories and Facades are usually singletons). Solution: use a static method of the class to return the singleton Singleton Pattern

6 March 200391.3913 R McFadyen6 Singleton (23.4) In Java, you would have code such as: Public static synchronized ServicesFactory getInstance() { If (instance == null) Instance := new ServicesFactory() Return instance } Singleton Pattern The point of the above is that only one instance is ever created. If one already exists, then the create is bypassed and the reference to the existing object is returned.

7 March 200391.3913 R McFadyen7 Singleton Pattern Collaboration diagram: suppose an instance of the ServicesFactory is created and, later on, a message, doIt, is passed to this instance. (Note: on page 350 the text makes reference to the situation illustrated below as “uninteresting”) :Register :ServicesFactory getInstance() ServicesFactory create() doIt()

8 March 200391.3913 R McFadyen8 Singleton Pattern Figure 23.6 :Register :AccountingAdapter getAccountingAdapter() > :ServicesFactory create() doIt() By applying the > stereotype to this object, they are implying that the getInstance method was used to instantiate it.

9 March 200391.3913 R McFadyen9 Singleton Pattern The text makes reference to Lazy Instantiation – The ServicesFactory object was created when it was needed, and not before :Register :AccountingAdapter getAccountingAdapter() > :ServicesFactory create() doIt()

10 March 200391.3913 R McFadyen10 Singleton Pattern Example. 256-262 of Design Patterns Explained (Shalloway & Trott)


Download ppt "March 200391.3913 R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software."

Similar presentations


Ads by Google