Design Patterns v3 Amal Introduction Creational Patterns
Design Patterns v3 Amal Introduction Creational Patterns
Tempus
McConnell
Module Topics (Cont’d)
Watch Video!
Facing Design Problems
A hard-to-do assignment for the architect/designer to design
reusable software!
Requires: (i) good understanding of the problem, and proper software;
(ii) flexible, modular and robust software design
Singleton
Factory Method
Abstract Factory
Builder
Prototype
Singleton
Watch the
Video!
Singleton
• How to
avoid this
and access
the memory
only once?
Singleton (Cont’d)
Motivation (Cont’d): An idea might come up to your mind by
saying:
“Hey, it’s static class!; the solution of the one-instantiated-object”.
The static class has a limitations like, e.g.:
it’s rigid (it isn’t flexible, it can’t be extended, overridden,… i.e. No
Inheritance),
there is no object so tracking the state is a nightmare. Therefore, static
limitations is a motivation to a new solution.
The solution simply resides in forcing the class to track its first object
by itself. The class can ensure that no other instance can be created
(by intercepting requests to create new objects), and it can provide a
way to access the instance. This is the Singleton pattern.
Singleton (Cont’d)
Applicability:
o There must be exactly one instance of a class, and it must be
accessible to clients from a well-known access point.
o When the sole instance should be extensible by subclassing, then
clients should be able to use an extended instance without modifying
their code.
Singleton (Cont’d)
Structure:
return instance
Singleton (Cont’d)
Participants:
• Defines Instance class interface which is responsible for accessing the
unique instance
• May instantiate its instance by itself
Collaborations: clients can access singleton class through
Instance operation (class interface)
Singleton (Cont’d)
Consequences:
o Controls the access to the only instance
o Avoids global variables in namespaces which reduces it.
o Allows inherence which permits run-time assignment of the subclasses
to the superclass
o Allows a two, or three, or any limited specific number of objects by
almost using the same technique.
Singleton (Cont’d)
Implementation:
o Firstly, we need to hide the singleton's constructor in order to block
instantiating the any objects from it. This will be done by marking the
constructor with a protect access modifier. This in turn will produce
compilation-time error if any object is instantiated through the
constructor.
o In order to avail the initiation of the constructor or obtaining the soul
instance, a static method have to be provided. This static method
would have access to the protected constructor and it can be accessed
from the outside. In addition, this method will have an exclusive
access to a private singleton member which is an instance of the
singleton class.
Singleton (Cont’d)
Implementation (Cont’d):
o Inheriting Singleton class has an issue of defining the unique instance
that clients will be able to use it.
• A possible solution is initializing the singleton instance with one of the subclasses
based on a variable to determine which class will be instantiated. This simply can
be implemented in the Instance operation of the singleton class.
• Another solution is to take the Instance operation from the parent singleton to
the subclass, but any client of the parent singleton will not be able to know the
instance. The link approach fixes the choice of singleton class at link-time, which
makes it hard to choose the parent singleton class at run-time.
• A more flexible way, is to create the a registry of singleton that captures the
instances by classes’ names, instead of having the Instance operation define the
set of possible classes with a variable. When Instance operation needs a
singleton it looks up the registry by the singleton class name.
Singleton (Cont’d): C#
Source Code:
public class mySingleton {
//shared members
private static mySingleton _instance;
public static mySingleton getInstance() {
if(_instance == null)
_instance = new mySingelton();
return _instance;
}
//instance members
protected mySingleton() {
//public instantiation disallowed
}
//other instance members
//...
}
Singleton (Cont’d)
Source Code(Cont’d):
public class Program {
static void Main(string[] args) {
mySingleton Singleton1 = SingletonSample.getInstance();
}
}
Singleton (Cont’d)
Known Uses:
o For “Abstract Factory Design” pattern only one instance of ConcreteFactory
class is needed per product family. “Singleton Design Pattern” is applied
o Single configuration for an application.
Related Patterns:
o Abstract Factory
o Builder
o Prototype
Exercise 3.2
Singleton Exercise
Configuration
UML Class Diagram
Singleton
Factory Method
Abstract Factory
Builder
Prototype
Simple Factory Idiom and Factory Method Pattern
Watch Video!
Simple Factory Idiom
Simple Factory Idiom
Simple Factory Idiom
Simple Factory Idiom
• What is the
problem with
this code?
• It violates the
single
responsibility
principle and
the open-
closed
principle
Simple Factory Idiom
Simple Factory Idiom
Simple Factory Idiom
Simple Factory Idiom
Simple Factory Idiom
Simple Factory Idiom
Simple Factory Idiom
Simple Factory Idiom
Abstract
Client Factory product
Concrete
products
Simple Factory Idiom
Simple Factory Idiom
violates the
open–closed
principle (OCP)
Factory Method pattern
Factory Method pattern
Factory Method pattern
Factory Method pattern
Factory Method pattern
Factory Method pattern
Factory Method pattern
Factory Method pattern
Factory Method pattern
Factory Method pattern: usage
Factory Method pattern
Burger
Restaurant
Abstract
Factory
method
BeefBurgerRetaurant
BeefBurger
VeggieBurger
VeggieBurgerRetaurant
When to use the Factory Method Patten?
Assume that your business expanded, and you opened a
new Italian Restaurant?
• This code
becomes open
for
modifications,
violating the
open-closed
principle
• Any other
problems in this
code?
Factory Method pattern
The Abstract Factory Pattern
Abstract Factory Pattern
Watch Video!
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern
The Abstract Factory Pattern: usage
MsiManufacturer
MsiGpu MsiMonitor
Company
Gpu Monitor
AsusGpu AsusMonitor
AsusManufacturer
Abstract Factory
Participants:
o AbstractFactory: declares an interface for operations that
create abstract product objects. (Consider it the protocol of a
factory making products)
o ConcreteFactory (RequirementConcreteFactory and
BugConcreteFactory ): implements the operations to create
concrete product objects. (Consider it the order of creating
specific products)
Abstract Factory (Cont’d)
Participants (Cont’d):
o AbstractProduct (AgileProcessModel and CMMIProcessModel):
declares an interface for a type of product object.
o ConcreteProduct (AgileRequirement, CMMIRequirement,
CMMIRequirement, CMMIBug):
• defines a product object to be created by the corresponding concrete
factory.
• implements the AbstractProduct interface.
o Client (TeamMember): uses only interfaces declared by
AbstractFactory and AbstractProduct classes.
Abstract Factory (Cont’d)
Collaboration:
o AbstractFactory defers creation of product objects to its
ConcreteFactory subclass.
o ConcreteFactory instantiates the ConcreteProduct class’s
object.
Abstract Factory (Cont’d)
Consequences:
o Concrete classes isolation
o Ease of selection between product families
o Promotes consistency among products
Abstract Factory (Cont’d)
Implementation:
o AbstractFactory declares creating the abstract products using the class
interface only. However, the ConcreteProduct has the implementation for
each product independently. In order to make the abstract factory create
the products instance, a ConcreteFactory class implements the class
interface in the AbstractFactory class. The concreteFactory class
instantiate the objects of a products family from each ConcreteProduct
class.
• In order to create a new product family, a new ConcreteFamily class is created.
• The class interface in the AbstractFactory class and its implementation in the
ConcreteFactory class which instantiate the ConcreteProduct ’s objects is a design
Pattern called “Abstract Method”
Abstract Factory (Cont’d)
Implementation:
o Only one instance of ConcreteFactory class is needed per
product family. This typically provided by “Singleton Design
Pattern”.
o If many product families are expected, the ConcreteFactory can
be implemented using the “Prototype Design Pattern”. We are
not going to discuss it in this presentation. The ConcreteFactory
is initialized with a prototypical instance of each product in the
family, and it creates a new product by cloning its prototype.
The Prototype-based approach eliminates the need for a new
concrete factory class for each new product family.
Abstract Factory (Cont’d) in C# (Write the same code in
Java)
Sample Code:
abstract class AgileProcessModel { } //AbstractProductA
abstract class CMMIProcessModel { } //AbstractProductB
class AgileRequirement : AgileProcessModel { } //ProductA1
class AgileBug : AgileProcessModel { } //ProductA2
class CMMIRequirement : CMMIProcessModel { } // ProductB1
class CMMIBug : CMMIProcessModel { } // ProductB2
Abstract Factory (Cont’d)
Tempus