ECM 2414 Design Patterns Flashcards

1
Q

What is a design pattern

A

Design patterns are meant to provide a general reusable solution to one type of problems during software development –
they provide an architecture.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Object Oriented Design Principles

A

 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

3 key types of design patterns

A

Creational
Structural
Behavioural

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Factory Pattern

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Abstract Factory Pattern

A

“Provides an interface for creating families of related or dependent objects without specifying their concrete classes.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Factory vs Abstract Factory

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Inheritance

A

extend a class and override a factory method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Composition

A

defines an abstract type for creating a family of products and lets subclasses decide how those products are produced

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Builder Pattern

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does builder pattern work

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Builder Pattern Benefits

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Singleton Pattern

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to create a Singleton pattern

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Eager Initialisation

A

the instance of singleton class is created at the time of class loading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Lazy Initialisation

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Thread Safe Singleton

A

The easier way to create a thread-safe singleton class is to make the global access method synchronized, so that only one thread can execute this method at a time.

To avoid this extra overhead everytime, double checked locking principle is used

The synchronized block is used inside the if condition with an additional check to ensure that only one instance of singleton class is created

17
Q

Decorator Pattern

A

A Structural Pattern

“Attaches additional behavioural responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality”

The concrete component of an abstract class gets wrapped in a number of decorators that affect its attributes

18
Q

Decorator facts

A
  • You can use one or more decorators to wrap an object
  • Decorators have the same supertype as the objects they decorate
    • So we can pass around a decorated object in place of the original object
  • Objects can be decorated at any time, so we can perform dynamic runtime decoration
  • Behaviour comes through the composition of decorators with the base components (and other decorators)
  • Some limitations
    • Takes time and effort
    • Introduces new levels of abstraction to the object composition
19
Q

The Command Pattern

A

A Behavioural Design Pattern

“Encapsulates a request as an object, thereby letting you parameterise other objects with different requests, queue or log requests, and support undoable operations”

The client sets up the remote control/radio, such that a Receiver is bound to a particular ConcreteCommand

The Invoker then interacts with the button and the behaviour that results is therefore determined by the client

This pattern is widespread in GUI design

20
Q

Command Pattern Invoke

A

Holds a command and at some point asks the command to carry out a request by calling its execute() method

21
Q

Command Pattern Command

A

Command: declares interface for all commands (concrete implementors therefore interchangeable)

22
Q

Command Pattern ConcreteCommand

A

fines the binding between an action and a Receiver. Once bound (due to work by the Client) the Invoker can make a request of its Command object (execute())– resulting in the ConcreteCommand calling one or more actions on the Receiver

23
Q

Command pattern receiver

A

Performs the work required to carry out the request.
Effectively any class can act as a Receiver.

24
Q

Command Pattern Client

A

Responsible for instantiating the ConcreteCommand and setting its Receiver

25
Q

Factory pattern quick summary

A

constructs a complete object in one step

26
Q

Abstract factory pattern quick summary

A

creates a family of related classes

27
Q

builder pattern quick summary

A

constructs a complex object (with parts) step by step

28
Q

singleton pattern quick summary

A

creates just a single instance of a class

29
Q

Decorator pattern quick summary

A

dynamically attaches additional functionalities

30
Q

Command pattern quick summary

A

encapsulates a request as an object