Creational Flashcards

1
Q

What is a creational design pattern?

A

Patterns concerned with the method of creating objects.
These patterns have decisions to be made at the time of instantiation of the object.

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

What are the types of creational design patterns

A

Factory Method,
Abstract Factory,
Singleton,
Prototype,
Builder,
Object Pool

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

What is a Factory Method Pattern?

A

The pattern is defining an interface or abstract class for creating an object, AND the sub-classes decide how to instantiate. [ Inheritance Model ]

This pattern promotes loose-coupling, the code interacts solely with the interface or abstract class.

[For example] if a utility company needs a database with multiple rate plans, the abstract class would have a #rate property, and an undefined getRate() method. The subclasses [ Commercial, Residential, etc. ] would have their own instantiation of each getRate().

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

What is an Abstract Factory?

A

An Abstract Factory pattern defines an interface or abstract class for creating families or objects. AKA “kit”
[ Composition model ]

It promotes consistency among objects. It is a good fit if the system needs to be independent of how its objects are created and composed. Or, if the family of objects need to be used together.

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

What is a singleton?

A

A singleton is a class that only has one instance and provides a global point to access it.

The class must ensure that only a single instance should be created and only a single object can be used.

Singletons save memory because the object is not created at each request.

Mostly used in database or multi-threaded applications.

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

What are the two forms of a singleton?

A

Easy instantiation: the creation of the instance at load time

Lazy instantiation: the creation of the instance when required

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

What is a Prototype?

A

The prototype pattern is the cloning of an existing object instead of creating a new instance. The clone can be customized as per the requirement.

Prototypes reduce the need for sub-classing, hides complexity, and enables adding and removing objects at runtime.

Useful for when creating objects is expensive or you need to keep the number of classes to a minimum.

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

What is a builder?

A

A builder pattern constructs a complex object from simple objects using a step-by-step approach.

This pattern provides clear separation between the construction and representation of the object, it provides better control over construction, and it supports change to the internal representation of objects.

An example would be a pizza order application where the client can request various toppings and other options.

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

What is an Object Pool?

A

An object pool pattern is a container that holds a specific amount of objects, when an object is taken from the pool, it is not available until it is put back.

Objects in the pool have a lifecycle: creation, validation, and destruction.

An object pool helps manage resources in a better way: it boosts performance, it manages connections a provides a way to reuse and share them, and it can provide a limit for maximum amount of objects.

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

What Creational Patterns are rarely seen in modern programming?

A

Abstract Factory, Factory Method, Prototype, and Singleton

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

Why is a Factory Method unpopular?

A

Uses inheritance to add one method. Alone an Abstract Factory (composition) would be preferable.

They make your code inflexible by defining the classes at compile time.

In languages with higher order functions, a function could just be passed in as an argument.

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

Why is a prototype pattern unpopular?

A

A data structure such as a hashmap could be passed in to configure the object instead.

Instead of trying to create from multiple pre-defined child classes. Much less code to write if there is just one configurable object.

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

Why is a Singleton Pattern unpopular?

A

Even as singular global objects are used frequently. Forcing an object to know and enforce that it is the only instance can make testing more difficult.

And it is extra logic to enforce that there is only one.

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

What are the most useful creational pattern?

A

The Builder Pattern, it provides a single object whose API builds and returns an object hierarchy for us.

And Object Pool

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