Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns (GoF) contains the creational patterns:

Similar presentations


Presentation on theme: "Design Patterns (GoF) contains the creational patterns:"— Presentation transcript:

1 Design Patterns (GoF) contains the creational patterns:
GoF Patterns Design Patterns (GoF) contains the creational patterns: Abstract factory Builder Factory method (section 23.3 has a Simple Factory) 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 November 2004 R McFadyen

2 Simple Factory Pattern
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. (The factory creates objects.) E.g. Which adapters should exist when NextGenPOS is running? The text presents a class that determines which adapters will be instantiated. November 2004 R McFadyen

3 Simple Factory Pattern
Textbook example The NextGen POS system supports several kinds of 3rd 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. Instead we apply pure fabrication, and using the Simple Factory Pattern, we have a Factory class that has the responsibility to create such adapter objects. November 2004 R McFadyen

4 Simple Factory Pattern
Textbook example ServicesFactory is a class that is responsible for creating the various adapters required in NextGenPOS. It is a factory. ServicesFactory accountingAdapter:… inventoryAdapter:… taxCalculatorAdapter:… getAccountingAdapter():… getInventoryAdapter():… getTaxCalculatorAdapter():… November 2004 R McFadyen

5 Simple Factory Pattern
Textbook example Suppose an external source contains the names of the adapters that are to be used. This source is available at runtime. ServicesFactory accesses this source and instantiates the correct adapter objects. Different adapters will be instantiated for different NextGenPOS systems. ServicesFactory accountingAdapter:… inventoryAdapter:… taxCalculatorAdapter:… getAccountingAdapter():… getInventoryAdapter():… getTaxCalculatorAdapter():… November 2004 R McFadyen

6 Solution: use a static method of the class to return the singleton
Singleton Pattern Singleton (23.4) Problem: Exactly one instance of a certain object is required (this object is called a singleton). We must ensure a class has only one instance. Solution: use a static method of the class to return the singleton Factories and Facades are usually singletons. Textbook problem: how do we control the instantiation of the ServicesFactory? November 2004 R McFadyen

7 Singleton Pattern Singleton (23.4)
In Java, you would have code such as: Public static synchronized ServicesFactory getInstance() { If (instance == null) Instance := new ServicesFactory() Return instance } 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. November 2004 R McFadyen

8 Singleton Pattern Singleton (23.4)
In Java, you would have code such as: Public static synchronized ServicesFactory getInstance() { If (instance == null) Instance := new ServicesFactory() Return instance } The “synchronized” keyword ensures that only one “thread” runs this code at a time. The text refers to a “critical section”. November 2004 R McFadyen

9 Singleton Pattern Suppose an instance of the ServicesFactory is created and, later on, other messages are passed to this instance. (Note: on page 350 the text makes reference to the situation illustrated below as “uninteresting”) :Register ServicesFactory getInstance() create() :ServicesFactory getTaxCalculatorAdapter() getAccountingAdapter() November 2004 R McFadyen

10 <<Singleton>>
Singleton Pattern Figure 23.6 By applying the <<singleton>> stereotype to this object, they are implying that the getInstance method was used to instantiate it. <<Singleton>> :ServicesFactory :Register getAccountingAdapter() create() :AccountingAdapter post() November 2004 R McFadyen

11 <<Singleton>>
Singleton Pattern The text makes reference to lazy instantiation – The ServicesFactory object was created when it was needed, and not before. Creating it before would be eager instantiation <<Singleton>> :ServicesFactory :Register getAccountingAdapter() create() :AccountingAdapter post() November 2004 R McFadyen


Download ppt "Design Patterns (GoF) contains the creational patterns:"

Similar presentations


Ads by Google