Secure Software Design:
Creational Design Patterns
Creational design patterns
 The creational design patterns deal with the best way to create
instances of objects.
 The simplest way to create an instance of an object is by using the new
operator.
Fred = new Fred(); //instance of Fred class
 This amounts to hard coding, depending on how you create the
object within your program.
 In many cases, the exact nature of the object that is created could
vary with the needs of the program and abstracting the creation process
into a special “creator” class can make your program more flexible and
general.
2
Creational design patterns
Singleton
Design
Pattern
Prototype
Design
Pattern
Factory
Design
Pattern
Singleton pattern – Intent
 Singleton is a creational design pattern that ensure that a class has only
one instance, while providing a global access point to this instance.
 Sometimes it’s appropriate to have exactly one instance of a class:
 window managers,
 print spoolers,
 database connection
 The Singleton pattern addresses all the concerns above.
 With the Singleton design pattern, you can:
 Ensure that only one instance of a class is created.
 Provide a global point of access to the object.
 Allowmultiple instances in the future without affecting a singleton
class' clients.
4
Singleton pattern – Intent
Singleton Pattern – Problem
 The Singleton pattern ensures a class has only one instance and provides a global
point of access to it.
 The class itself is responsible for keeping track of its sole instance. 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.
 Singletons maintain a static reference to the sole singleton instance and return a
reference to that instance from a static instance() method.
6
Singleton Pattern – Solution
• All implementations of the Singleton have these two steps in
common:
• Make the default constructor private
• Create a static creation method that acts as a constructor. Under the hood,
this method calls the private constructor to create an object and saves
it in a static field. All following calls to this method return the cached
object.
• If the code has access to the Singleton class, then it’s able to call the
Singleton’s static method. So, whenever that method is called, the
same object is always returned.
Singleton Pattern – How to Implement
1.Add a private static field to the class for storing the singleton instance.
2.Declare a public static creation method for getting the singleton instance.
3. Implement “lazy initialization” inside the static method. It should create
a new object on its first call and put it into the static field. The method
should always return that instance on all subsequent calls.
4. Make the constructor of the class private. The static method of the
class will still be able to call the constructor, but not the other objects.
5.Go over the client code and replace all direct calls to the singleton’s
constructor with calls to its static creation method.
CODE EXAMPLE
Prototype pattern – Intent
 The Prototype pattern specifies
the kinds of objects to create using a
prototypical instance and create new
objects by copying this prototype.
 A Prototype pattern is used when
creating an instance of a class is very
time-consuming or complex in some
way. Then, rather than creating more
instances, you make copies of the
original instance and modify them as
appropriate.
Prototype pattern – Problem
 Application "hard wires" the class of object
to create in each "new" expression.
• Say you have an object, and you want to
create an exact copy of it. How would you do
it? First, you must create a new object of the
same class. Then you must go through all the
fields of the original object and copy their
values over to the new object.
• Nice! But there’s a catch. Not all objects can
be copied that way because some of the
object’s fields may be private and not visible
from outside of the object itself.
Prototype pattern – Solution
• The Prototype pattern delegates the cloning
process to the actual objects that are being
cloned. The pattern declares a common
interface for all objects that support cloning.
This interface lets you clone an object without
coupling your code to the class of that object.
• The method creates an object of the current
class and carries over all the field values of
the old object into the new one. It can even
copy private fields because most
programming languages let objects access
private fields of other objects that belong to
the same class.
Prototype pattern – How to Implement
• Create the prototype interface and declare the Clone method in it. Or just
add the method to all classes of an existing class hierarchy.
• A prototype class must define the alternative constructor that accepts an
object of that class as an argument. The constructor must copy the values
of all fields defined in the class from the passed object into the newly
created instance. If your programming language doesn’t support method
overloading, you won’t be able to create a separate “prototype”
constructor.
• The cloning method usually consists of just one line: running a new
operator with the prototypical version of the constructor.
• Optionally, create a centralized prototype registry to store a catalogue of
frequently used prototypes.
CODE EXAMPLE
Factory pattern - Intent
 The Factory pattern returns
an instance of one of several
possible classes depending on
the data provided to it.
 Define an interface for creating
an object, but let subclasses
decide which class to instantiate.
Factory Method lets a class defer
instantiation to subclasses.
Factory pattern - Problem
 A framework needs to
standardize the architectural
model for a range of applications
but allow for individual
applications to define their own
domain objects and provide for
their instantiation.
Factory pattern - Solution
• The Factory Method pattern suggests that
you replace direct object construction
calls with calls to a special factory
method. Objects returned by a factory
method are often referred to as products.
• There’s a slight limitation though:
subclasses may return different types of
products only if these products have a
common base class or interface.
• The code that uses the factory method
(often called the client code) doesn’t see
a difference between the actual products
returned by various subclasses.
Factory pattern – How to Implement
• Make all products follow the same interface. This interface should declare
methods that make sense in every product.
• Add an empty factory method inside the creator class. The return type
of the method should match the common product interface.
• In the creator’s code find all references to product constructors. One by
one, replace them with calls to the factory method, while extracting the
product creation code into the factory method.
• Now, create a set of creator subclasses for each type of product listed in
the factory method. Override the factory method in the subclasses and
extract the appropriate bits of construction code from the base method.
• If there are too many product types and it doesn’t make sense to create
subclasses for all of them, you can reuse the control parameter from the
base class in subclasses.
CODE EXAMPLE
CODE EXAMPLE
Questions?