ECM 2414 Design Patterns Flashcards
What is a design pattern
Design patterns are meant to provide a general reusable solution to one type of problems during software development –
they provide an architecture.
Object Oriented Design Principles
Identify the aspects of your application that vary and separate them from
what stays the same
Program to an interface, not an implementation
Favor ‘object composition’ over class inheritance
3 key types of design patterns
Creational
Structural
Behavioural
Factory Pattern
A Creational Design Pattern
“Defines an interface for creating an object, but lets subclasses decide which class to instantiate. It lets a class defer instantiation
to subclasses.”
We provide a creator, but make the class abstract. The concrete classes that extend the creator class provide a concrete implementation of the factory method (often parametrised)
Abstract Factory Pattern
“Provides an interface for creating families of related or dependent objects without specifying their concrete classes.”
Factory vs Abstract Factory
Both used for object creation
Factory Pattern uses inheritance and gives you a complete object in one shot
Abstract Factory Pattern uses composition and returns a family of related classes
Inheritance
extend a class and override a factory method
Composition
defines an abstract type for creating a family of products and lets subclasses decide how those products are produced
Builder Pattern
A creational design pattern
“Separates the construction of a complex object from its representation so that the same construction processes can create
different representations”
- Let you vary a product’s internal representation
- Isolate code for construction and representation
- Finer control over the construction process
How does builder pattern work
- the builder specifies the interface for creating parts of the complex (Product) object
- the ConcreteBuilder objects create and assemble the parts that make up the Product through the Builder interface
- the director object takes responsibility for the construction process of the complex object, however it delegates the actual creation and assembly to the builder interface.
- the product represents the complex object that is created by the ConcreteBuilder object(s). The Produt consists of multiple parts that are created separately by the ConcreteBuilder objects
Builder Pattern Benefits
- Encapsulates the way a complex object is constructed
- Allows objects to be constructed in a multistep and varying process (as opposed to one-step factories)
- Hides the internal representation of the product from the client
Singleton Pattern
A creational Design Pattern
“Ensures that a class has only one instance, and provides a global point of access to it.”
The Problem: Want to have just a single instance of an object
Examples
- Window managers
- File systems
- Print spoolers
How to create a Singleton pattern
Static member: Create a private static variable of the Singleton class. This is the only instance of Singleton class
Private constructor: Create a private constructor for making sure an outer class can NOT instantiate object from the Singleton class
Static public method: Create a global point of access to get a Singleton instance
This object may be resource hungry and not always used, so there is a cost to its implementation
Eager Initialisation
the instance of singleton class is created at the time of class loading
Lazy Initialisation
The singleton class employs a technique known as lazy instantiation to create the singleton, to ensure that the singleton instance is not created until the getInstance() method is called for the first time