Design Patterns (Week 8-9) Flashcards

1
Q

What is a design pattern?

A

A way of reusing abstract knowledge about a problem and its possible solutions

  • Description of a problem
  • Essence of a solution

Patterns describe a problem which occurs over and over again - and then offers a solution

Not a finished design

  • they are a description/template
  • Provide enough information about trade-offs and consequences to enable design decisions to be made
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What types of patterns are there?

A

Architectural patterns
- Describe structural designs for software systems

Design patterns
- Template for addressing design issues in software design

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

Describe architectural patterns

A

Architectural pattern is a general, reusable solution to a commonly occurring problem in software architecture within a given context.
- similar to software design patterns, but with a broader scope

Example:
Client-server pattern
- Describes how a server may be connected with multiple clients

Peer-to-Peer pattern
- Describes how nodes can behave as clients and/or servers and how they might connect

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

What are the three main types of design patterns in OO design?

A

Creational
- deal with classes and object instantiation

Structural
- Deal with class-object composition

Behavioural
- Deal with communications between objects

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

Talk about some creational design patterns

A

Builder
- Separate the construction of a complex object from its representation, so that the same build process can create different representations. E.g constructing meals at a take-away

Prototype
- Create new objects by copying a prototype. E.g cell division, a cell clones itself

Singleton
 - Ensure a class has only one instance. E.g A logger object or a window manager
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a singleton?

A

Software design pattern that restricts the instantiation of a class to one. (ensure a class has only one instance and provide a global point of access to it)

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

What are the steps of creating a singleton?

A
  1. Define a private static attribute in the “single instance” class
  2. Define a public static accessor function in the class
  3. Do “Lazy initialization” (creating on first use) in the accessor function.
  4. Define all constructors to be protected or private
  5. Clients may only use the accessor function to manipulate the singleton
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When is singleton unnecessary?

A

Short answer: most of the time. Long answer: when it’s simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally. The real problem with Singletons is that they give you such a good excuse not to think carefully about the appropriate visibility of an object. Finding the right balance of exposure and protection for an object is critical for maintaining flexibility.

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

What is the advantage of using singletons over global variables?

A

The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.

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

Describe factory pattern

A

Creates object without specifying the exact class of object that will be created

The factory design pattern defines an interface for creating an object

The factory method lets a class defer instantiation to subclasses

Returns an instance of one of several possible classes

  • Depending on data we provide
  • Classes returned have a common parent class
  • Each class performs differently

Factory can return an instance of several possible classes that have a common parent class

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

What programming principle does the factory give us a solution to?

A

Polymorphism

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

What are factory pattern variants?

A

Static factory pattern

  • Create a method which is responsible for creating objects of a particular type
  • if this method is ‘static’ then it is a static factory pattern

Factory Method Pattern

  • The method creating the objects becomes ‘abstract’
  • Causes the class to be abstract and must be used as a base class
  • The method needs to be implemented in a ‘concrete Client’

Abstract Factory Pattern
- Group together individual Factory methods

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

What are structural patterns?

A

Structural patterns modify the structure or design of the system

They are responsible for building simple and efficient class hierarchies and relations between different classes

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

What is a Facade pattern?

A

Facade is a structural design pattern that lets you provide a simplified interface to a complex system of classes, library or framework

The problem:

  • Imagine some code that has to work with a large set of objects of some complex library or framework
    • You have to manually initialize all these objects, keep track of the dependencies, correct order and so on.
  • The business logic of your classes becomes tightly coupled to the implementation details of third party library. Such code is pretty hard to comprehend and maintain
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the solution for working with large set of objects of some complex library or framework?

A

Facade pattern.

Facade pattern provides a simple interface to a complex subsystem containing dozens of classes.

  • the facade may have limited functionality in comparison to working with subsystem directly
  • However, it includes only those features that clients really care about
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the main responsibility of a facade?

A

The main responsibility of a a facade is to wrap up a complex group of subsystems so that it can provide a simple look to the outside world.

for example, you (Client) contact IT Help Deks (Facade). He/she decides which IT help function (Sub-System) you could be directed to

17
Q

What are the advantages of Facade pattern?

A

Simplified interface to a complex system

Modify the inner workings of the subsystem without the client knowing
- As long as the facade interface to the client remains the same

18
Q

What are behavioural patterns?

A

Behavioural patterns are responsible for the efficient and safe communication of behaviours between program’s objects

19
Q

What is observer pattern?

A

Observer pattern lets you define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- Magazine subscriptions. New editions are sent out to all subscribing readers (observers)

20
Q

Describe a solution to this problem using Observer pattern:
There are two objects - Customer and e-store.
Customer could visit the e-store each day to check the availability of a product
- many of these visits would be for nothing
- the e-store could send lots of messages to all its customers when the product is available

Either the customer wastes resources or the e-store does

A

Solution:

  • The customer ‘subscribes’ to the e-store to get product updates
  • The e-store then has a list of customer subscribers for certain product updates
  • Only those subscribing customers are informed (updated) when the product details/availability changes.

The customers have no access to the data in the e-store, they are dependent on the ‘publisher’ to provide them data.

21
Q

Summarize Factory pattern, Facade pattern and observer pattern

A

Factory pattern

  • When we want to create different objects
  • object creation code is not needed by the client

Facade pattern

  • When we want to hide the inner working (of an API for example)
  • Facade interface masks complex behaviour from a client

Observer pattern
- Software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.