Design Patterns Flashcards

1
Q

What is a Singleton?

A

A singleton is a class that wraps itself attempting to ensure that only one instance of the class is ever used during runtime.

Singletons contain an instance property that is an instance of itself, alongside a static method that gets that instance of itself. The singleton class then contains any other functionality the class required.

Despite violating the Single responsibility principle, can be useful when needing a global object accessible across the environment.

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

What is a factory?

A

A factory is a function/method that simplifies class instantiation where a standard operation would be repeated (e.g. type = button).

When To Use: When repeated instantiation, especially with consistent specification/constructor arguments, occurs.

NON-FACTORY: new Element() + element.type = "button" 
FACTORY: Element.CreateButton();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an abstract factory?

A

An abstract factory is a factory - a method that simplifies class instantiation - but without specifying concrete classes - instead abstract classes or interfaces. It is an abstraction of a factory

Element.CreateButton(AndroidButton)

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

What is a factory method?

A

Factory Method provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created (by inheritance)

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

What is the difference in approach between an abstract factory & a factory method

A

Composition vs. inheritance.

Both are factories - simplifying object instantiation. However the abstraction changes - an abstract factory relies on an interface for interchangable classes that share interfaces (public method definitions) using composition. A factory method uses inheritance of a high-level abstract class that is inherited from.

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

What is a Builder pattern?

A

Builders construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.

When To Use: When many multiple classes inherit a base class and require complex constructors and/or manual steps after initialisation.

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

What is a Prototype pattern?

A

Prototype is a creational design pattern that lets you copy existing objects without making your code dependent on their classes.

When To Use: When deep copies of objects are required.

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

What is an Adapter class?

A

An Adapter class should be used when a class or interface does not match another but need to be used in the context of another hole. For example to fit a square peg in a round hole, use a square peg adapter.

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

What is a Bridge?

A

Bridge is a structural design pattern that lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently of each other.

For example if there are 2 circle and 2 square classes, and each has a colour assingned, that is four classes. However instead a bridge can be used - a shape class and a colour bridge.

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

What is a Composite?

A

Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.

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

What is a (class) Decorator?

A

Decorator is a structural pattern that allows adding new behaviors to objects dynamically by placing them inside special wrapper objects.

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

What is a Facade?

A

Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.

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