Design Patterns Flashcards

1
Q

What are

Design Patterns?

A

Codified methods for describing design problems and their solutions.

A Design Pattern is a named problem/solution pair that can be applied in new contexts, with advice on how to apply it in novel situations and a description of it’s trade-offs

These enable the Software Engineering community to capture design knowledge in a way that enables it to be re-used.

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

Three Major Classes

of

Design Patterns

A

Creational

Structural

Behavioral

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

Major Design Patterns,

organized by classification

(9)

A

Creational Patterns

  • Factory
  • Singleton
  • Builder

Structural Patterns

  • Adapter
  • Facade
  • Bridge

Behavioral Patterns

  • Observer
  • Strategy
  • Model-View-Controller
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Three Creational

Design Patterns

A

Factory

Singleton

Builder

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

Three Structural

Design Patterns

A

Adapter

Facade

Bridge

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

Three Behavioral

Design Patterns

A

Observer (or Publish-Subscribe)

Strategy

Model-View-Controller

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

Creational Design Patterns:

Singleton Pattern

Basic Description

A

Context/Problem

We want a single instance of a class, such as a Factory that can be accessed easily

Solution

Define a static method of the class that returns the singleton.

This yields global visibility to the single instance via a static “getInstance” method of the class

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

Creational Design Patterns:

Builder Pattern

Basic Description

A

Intent

Separate the construction of a complex object from it’s representation, so that the same construction process can create different representations/configurations.

Solution/Process

Three major Objects: Client, Director and Builder

They coordinate to produce a “Product”.

The Client requests a product from the Director, the Director ‘directs’ the Builder to build various parts of the product.

  • Client knows WHAT to build, not HOW
  • Director knows how to call Builder based on different inputs from the Client
  • Builder knows HOW to build, not WHAT
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Creational Design Patterns:

Builder Pattern

Process

A

Solution/Process

  1. Client creates Director object, configures it with desired Builder object
  2. Director notifies Builder whenever a part of the Product should be built
  3. Builder handles request from director and adds parts to the product
  4. Client retrieves product from Builder
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Creational Design Patterns:

Factory Pattern:

Basic Process of creating an object

A

Create an instance of “Foo”

  • Client calls FooFactory.createFoo( args )
  • FooFactory creates a new Foo instance
  • Using the provided arguments, FooFactory sets the various properties of the Foo instance
  • Foo instance is returned to the Client
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Creational Design Patterns:

Factory Pattern:

How to create and access a Factory?

A

Create the Factory as a Singleton

Two Options for accessing:

  • Pass the factory instance around as a parameter to wherever it is needed
  • Or initialize the objects that need visibility with a permanent reference(global, or higher context)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Creational Design Patterns:

Factory Pattern

Basic Description

A

Context/Problem

Need to create specialized instances of objects. There are special considerations such as complex logic.

Desire to separate creation responsibility from the object class.

Solution

Create a “Pure Fabrication” object, called a Factory, which handles the creation of instances of the class.

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

Structural Design Patterns:

Adapter Pattern

Basic Description

A

Context/Problem

Need to resolve incompatible interfaces, or provide a stable interface to similar components with different interfaces

Solution

Convert the original interface of the component into another interface through an intermediate Adapter object. The Adapter has the interface the Client needs, and uses the existing interface to manipulate the component.

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

Structural Design Patterns:

Bridge Pattern

Basic Description

A

Context/Problem

The derivations of an abstract class must use multiple implementations, without causing an “explosion” in the number of classes

Solution

Need to decouple a set of implementations from the set of objects using them.

Define an interface for all implementations to use, and have the derivations of the abstract class use it.

Basically creates a common interface

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

Structural Design Patterns:

Facade

Basic Description

A

Context/Problem

We need a common, unified interface to a different set of implementations or interfaces, such as within a subsystem.

Solution

Define a single point of contact to the subsystem:

A “Facade” object that “Wraps” the entire subsystem.

The facade presents a single unified interface and is responsible for collaboration with the subsystem components.

Implementation and other components of the subsystem are private and can’t be seen by external components.

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

Behavioral Design Patterns:

Observer

(or Publish-Subscribe)

Basic Description

A

Context/Problem

  • Different kinds of “Subscriber” objects are interested in the state changes or events of some “Publisher” object
  • They need to react in their own way when a Publisher generates an event
  • The Publisher should maintain low coupling to Subscribers

Solution

  • Define a Subscriber Interface that is implemented by Subscribers
  • Publisher can dynamically register subscribers who are interested in a particular event
  • The Subscribers are notified when an event occurs
17
Q

Behavioral Design Patterns:

Strategy Pattern

Basic Description

A

Context/Problem

  • Design needs to account for varying, but related, algorithms or policies
  • Need to be able to make changes to algorithms or policies

Solution

  • Define each algorithm/policy/strategy in a separate class, but using a common interface
  • Strategies can be used interchangeably
18
Q

Behavioral Design Patterns:

Model-View-Controller (MVC)

Basic Description

A

In a complex system, it is best to separate behavior that handles updating what is presented to the user(View) and what is happening internally in the system(Model)

Solution

  • Organize components into three basic layers/Classes:
    • Model - The “actual” system
    • View - Screen Presentation
    • Controller - Handles input to make changes to the model or View
  • Multiple Views and Controllers can be defined for various components
  • Relies on the Observer Pattern to connect everything
  • The View must ensure it reflects the current state of the Model
    • Representations in the View subscribe to events in the Model
  • The Model notifies all Views that depend on it
  • The Controller recieves input, such as User Actions, and updates the Model accordingly