Abstract Factory Flashcards

1
Q

The abstract factory pattern solves the problem of

A

creating families of related products.

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

abstract factory pattern is defined as

A

defining an interface to create 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
3
Q

The class diagram consists of the following entities

A

Abstract Factory
Concrete Factory
Abstract Product
Concrete Product
Client

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

The abstract factory is particularly useful for frameworks and toolkits that work on different operating systems.

A

For instance, if your library provides fancy widgets for the UI, then you may need a family of products that work on MacOS and a similar family of products that work on Windows.

Similarly, themes used in IDE can be another example. If your IDE supports light and dark themes then it may use the abstract factory pattern to create widgets that belong to the light or dark theme just by varying the concrete factory that creates the widgets.

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

The difference between the factory method pattern and the abstract factory pattern

A

The factory method pattern is usually responsible for creating a single product whereas an abstract factory pattern creates entire families of related products.

Furthermore, in the factory method pattern, we use inheritance to create more specialized products whereas, in an abstract factory pattern, we practice object composition by passing in factories that are consumed to create the desired products.

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

Concrete factories can be best represented as

A

a singleton object

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

One of the fundamental principles of good object orientated design is to hide the concrete classes and expose interfaces to clients. The client should know what requests an object responds to rather than the implementation.

A

An object responds to a set of requests, these requests can be captured by an interface which the object’s class implements.

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

Instead of new-ing up objects in client code, we’ll have a class responsible for manufacturing the requested objects and returning them to the client.

A

We’ll call this class F16Factory since it can create the various parts of the F16 aircraft and deliver them to the requesting client.

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

We’ll call this class F16Factory since it can create the various parts of the F16 aircraft and deliver them to the requesting client.

A

public class F16Factory {

public IEngine createEngine() {
    return new F16Engine();
}

public IWings createWings() {
    return new F16Wings();
}

public ICockpit createCockpit() {
    return new F16Cockpit();
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Suddenly the consumer code is free of the implementation details of what class implements the F-16 engine and works with an interface.

We would still like to hide the new F16Engine() part of the code. We don’t want the consumer to know what class we are instantiating.

Suppose we pass in the F16Factory object in the constructor to the client code and it would now create objects like so:

A

public void main(F16Factory f16Factory) {
IEngine f16Engine = f16Factory.createEngine();
List<IEngine> engines = new ArrayList<>();
engines.add(f16Engine);
for (IEngine engine : engines) {
engine.start();
}
}</IEngine>

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

Note how this setup allows us the freedom to change the concrete class representing the F16Engine as long as it commits to the IEngine interface.

A

We can rename, enhance or modify our class without causing a breaking change in the client.

Also note that by just differing the factory class passed into the client constructor, we are able to provide the client with the same parts for a completely new aircraft.

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

the abstract factory.

A

all the factories commit to a common interface whose methods they’ll implement

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

an interface that define the methods the factories for different aircraft would need to implement. The client code is written against the abstract factory but composed at runtime with a concrete factory.

The create methods don’t return concrete products rather interfaces to decouple the factory consumers from the concrete implementation of parts.

A

public interface IAircraftFactory {
IEngine createEngine();
IWings createWings();
ICockpit createCockpit();
}

public class F16Factory implements IAircraftFactory {
@Override
public IEngine createEngine() {
return new F16Engine();
}
@Override
public IWings createWings() {
return new F16Wings();
}
@Override
public ICockpit createCockpit() {
return new F16Cockpit();
}
}

public class Boeing747Factory implements IAircraftFactory {
@Override
public IEngine createEngine() {
return new Boeing747Engine();
}
@Override
public IWings createWings() {
return new Boeing747Wings();
}
@Override
public ICockpit createCockpit() {
return new Boeing747Cockpit();
}
}

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

In our scenario, all aircrafts are expected to follow the same pattern. They first get manufactured, then taxi on the runway and then fly away. We can thus create a class for an aircraft that does these three tasks.

A

// Incomplete skeleton of the class.
public class Aircraft {
IEngine engine;
ICockpit cockpit;
IWings wings;
IAircraftFactory factory;
public Aircraft(IAircraftFactory factory) {
this.factory = factory;
}
protected Aircraft makeAircraft() {
engine = factory.createEngine();
cockpit = factory.createCockpit();
wings = factory.createWings();
return this;
}
private void taxi() {
System.out.println(“Taxing on runway”);
}
public void fly() {
Aircraft aircraft = makeAircraft();
aircraft.taxi();
System.out.println(“Flying”);
}
}

public class Client {
public void main() {
F16Factory f16Factory = new F16Factory();
Boeing747Factory boeing747Factory = new Boeing747Factory();
Collection<Aircraft> myPlanes = new ArrayList<>();
myPlanes.add(new Aircraft(f16Factory));
myPlanes.add(new Aircraft(boeing747Factory));
for (Aircraft aircraft : myPlanes) {
aircraft.fly();
}
}
}</Aircraft>

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