quiz image

Design Patterns: Abstract Factory

SelfSufficientRadon avatar
SelfSufficientRadon
·
·
Download

Start Quiz

Study Flashcards

40 Questions

What is the primary advantage of using the Builder pattern in object construction?

It enables the creation of objects with different representations without changing the client code.

Which of the following is a characteristic of the Builder pattern?

It separates the construction of an object from its representation.

What is the purpose of having multiple builder classes in the Builder pattern?

To allow for the creation of different objects with the same set of construction steps.

Which of the following patterns is most similar to the Builder pattern in terms of object construction?

Abstract Factory

What is the main difference between the Builder pattern and the Factory Method pattern?

The Builder pattern creates objects step-by-step, while the Factory Method pattern creates objects in a single step.

What is the benefit of using the Builder pattern in terms of code reuse?

It allows for the reuse of code across different object types.

What is the primary benefit of using the Factory Method pattern in terms of coupling?

It decouples the creator from the concrete products, making the code more flexible.

In which scenario is the Factory Method pattern most suitable?

When introducing a new hierarchy of creator classes.

What is the primary difference between the Factory Method pattern and the Abstract Factory pattern?

Factory Method creates a single product, while Abstract Factory creates a family of products.

What is the advantage of introducing the Factory Method pattern into an existing hierarchy of creator classes?

It makes the code more flexible and easier to support.

Which pattern is often used to compose the methods on Abstract Factory classes?

Prototype

What is the primary disadvantage of using the Factory Method pattern?

It increases the complexity of the code.

Which principle does the Factory Method pattern support?

All of the above

What is the typical evolution of designs using the Factory Method pattern?

From Factory Method to Abstract Factory to Prototype.

Which of the following design patterns is used to construct complex objects step by step?

Builder

Which design pattern is often used as an alternative to Facade when you only want to hide the way the subsystem objects are created from the client code?

Abstract Factory

Which design pattern specializes in creating families of related objects?

Abstract Factory

Which design pattern is used to produce different types and representations of an object using the same construction code?

Builder

Which design pattern is often implemented as a Singleton?

All of the above

Which design pattern uses a set of Factory Methods to create objects?

Abstract Factory

Which design pattern is used to encapsulate the relations between abstractions and implementations?

Abstract Factory

Which design pattern is used to compose the methods on a class using Prototype?

Abstract Factory

What is the main issue that arises when trying to add a new class to the program if the rest of the code is already coupled to existing classes?

The code becomes more complex and harder to maintain

What is the primary purpose of the Factory Method pattern?

To provide a way to create objects without specifying the exact class of object

What is a limitation of the Factory Method pattern?

Subclasses can only return products that have a common base class or interface

What is the result of not using the Factory Method pattern when adding a new class to the program?

The code becomes more complex and harder to maintain

What is the key difference between the Factory Method pattern and the Abstract Factory pattern?

The Abstract Factory pattern creates families of related objects

When is the Factory Method pattern most suitable?

When the type of object to be created is determined by subclasses

What is the primary advantage of using the Factory Method pattern in terms of coupling?

It reduces coupling between classes

What is the relationship between the Factory Method pattern and the Builder pattern?

The Factory Method pattern and the Builder pattern are unrelated

What is the primary responsibility of the creator class in the Factory Method pattern?

To contain some core business logic that relies on product objects

How does the client code treat the products returned by the factory method in the Factory Method pattern?

As abstract products

What is the purpose of declaring a factory method in the base Dialog class in the cross-platform dialog example?

To allow subclasses to provide their own implementation of the factory method

What is the benefit of using the Factory Method pattern in terms of code reuse?

It reduces code duplication

What is the difference between the Factory Method pattern and the Abstract Factory pattern?

The Factory Method pattern returns a single product, while the Abstract Factory pattern returns a family of related products

What is the role of the product interface in the Factory Method pattern?

To declare the operations that all concrete products must implement

What is the purpose of the factory method in the RoadLogistics class?

To create a truck object

What is the advantage of using the Factory Method pattern in terms of coupling?

It decreases coupling between the client code and the concrete product classes

What is the primary difference between the Factory Method pattern and the Builder pattern?

The Factory Method pattern is used for creating objects in a single step, while the Builder pattern is used for creating objects in a step-by-step fashion

What is the purpose of the factory method in the WindowsDialog class?

To create a Windows-styled button

Study Notes

Relations with Other Patterns

  • Many designs start with Factory Method, which is less complicated and more customizable via subclasses, and evolve towards Abstract Factory, Prototype, or Builder, which are more flexible but more complicated.
  • Abstract Factory classes are often based on a set of Factory Methods, but you can also use Prototype to compose the methods on these classes.

Abstract Factory

  • Abstract Factory specializes in creating families of related objects.
  • It returns the product immediately.
  • It can serve as an alternative to Facade when you only want to hide the way the subsystem objects are created from the client code.
  • It can be used along with Bridge to encapsulate relations and hide complexity from the client code.
  • Abstract Factory classes can be implemented as Singletons.

Builder

  • Builder is a creational design pattern that lets you construct complex objects step by step.
  • It allows you to produce different types and representations of an object using the same construction code.
  • It organizes object construction into a set of steps (e.g. buildWalls, buildDoor, etc.).
  • You can create different builder classes that implement the same set of building steps, but in a different manner.
  • Different builders execute the same task in various ways.
  • Builder can be implemented as a Singleton.

Problem

  • Imagine a complex object that requires laborious, step-by-step initialization of many fields and nested objects.
  • The initialization code is usually buried inside a monstrous constructor with lots of parameters or scattered all over the client code.

Pros and Cons of Factory Method

  • Pros: avoids tight coupling between the creator and the concrete products, follows the Single Responsibility Principle, and follows the Open/Closed Principle.
  • Cons: the code may become more complicated since you need to introduce a lot of new subclasses to implement the pattern.

Factory Method Pattern

  • Also known as Virtual Constructor
  • Solved problem: Code coupled to specific classes, making it hard to add new classes without changing entire codebase

Problem Scenario

  • Logistics management application initially handles transportation by trucks, but needs to be extended to sea transportation
  • Adding new classes (e.g., Ships) is complex due to tight coupling with existing classes

Solution

  • Replace direct object construction calls with calls to a special factory method
  • Factory method returns objects, often referred to as products
  • Subclasses can alter the class of objects being returned by the factory method

Key Points

  • The factory method in the base class should have its return type declared as an interface
  • All products must follow the same interface
  • Subclasses can return different types of products only if these products have a common base class or interface
  • The client code treats all products as abstract, without knowing the actual product type

Example: Cross-Platform UI Elements

  • Creating UI elements without coupling client code to concrete UI classes
  • Factory method produces buttons inside the base Dialog class
  • Subclasses can return Windows-styled or Linux-styled buttons from the factory method

Pseudocode Example

  • Dialog class uses different UI elements to render its window
  • Factory method produces buttons, which can be overridden in subclasses
  • Client code works with an instance of a concrete creator, via the base interface

Learn about the Abstract Factory design pattern, its relation to other patterns, and how it specializes in creating families of related objects.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Use Quizgecko on...
Browser
Browser