0% found this document useful (0 votes)
12 views

Lecture 4 - Creational Design Patterns (Factory Method, Abstract Factory)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture 4 - Creational Design Patterns (Factory Method, Abstract Factory)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Lecture 4

Creational Design Patterns


(Factory Method, Abstract Factory)
Agenda
● Creational Design Patterns
● Factory Method
● Abstract Factory
Creational Design Patterns
Creational design patterns provide various object
creation mechanisms, which increase flexibility and
reuse of existing code.
Factory Method
Factory Method
Factory Method
● Problem
○ Initially, the logistics management application is designed exclusively for trucks. As popularity grows
and requests emerge to incorporate sea transportation (ships), a problem of code coupling arises.
Most of the code is tightly coupled to the Truck class, making it difficult to integrate new
transportation types.
■ Challenges:
● Adding new transportation types requires extensive changes throughout the codebase.
● Introducing conditionals to switch behavior based on transportation class results in
complex and error-prone code.
○ This situation highlights the need for a more flexible and extensible design to accommodate different
transportation types without major code modifications.
Factory Method
● Solution
○ The Factory Method pattern recommends replacing direct object construction calls with calls to a
dedicated factory method. While objects are still created using the new operator, this operator is
invoked within the factory method. Objects produced by the factory method are commonly known as
"products." This pattern enhances flexibility and encapsulation, allowing subclasses or
implementations to determine the specific product creation while maintaining a common interface.
Factory Method
● Solution
○ While the Factory Method pattern may seem like moving constructor calls, it offers significant
advantages. It enables method overriding in subclasses, allowing changes in the class of products
created by the method. However, a limitation exists: subclasses can return different product types
only if they share a common base class or interface, and the factory method in the base class declares
its return type as this shared interface. This pattern enhances flexibility and maintainability in object
creation.
Factory Method
● Solution
○ In the Factory Method pattern, both Truck and Ship classes implement a common Transport
interface, defining a shared deliver method. Each class implements this method differently, reflecting
their specific modes of transportation (land for trucks and sea for ships). Factory methods in classes
like RoadLogistics and SeaLogistics return objects of different classes (trucks or ships), but the
client code interacts with them as abstract Transport objects. The client treats all products uniformly,
expecting the deliver method, but it remains unconcerned with how it's implemented for each
product. This separation of concerns enhances code flexibility and encapsulation, promoting
maintainability and ease of extension.
Factory Method
❏ Structure
Factory Method
● Product Interface:
○ Declares a common interface shared by all products created by the creator and its subclasses.
● Concrete Products:
○ Different implementations of the product interface, each with specific functionality.
● Creator Class:
○ Declares the factory method responsible for creating product objects.
○ The return type of the factory method matches the product interface.
○ Can be declared as abstract to enforce implementation in subclasses.
○ The primary responsibility may include core business logic, while the factory method decouples
product creation from concrete product classes.
● Concrete Creators:
○ Subclasses of the creator class.
○ Override the base factory method to return different types of products.
○ The factory method can return new instances, existing objects from caches, pools, or other sources.
Factory Method
❏ Pseudocode
Factory Method
● Base Dialog Class
○ Utilizes various UI elements to render its window.
○ Differences across operating systems in appearance but consistent behavior.
● Benefits of Factory Method
○ Avoid rewriting Dialog class logic for each OS.
○ Declare a factory method within Dialog for creating UI elements.
● Subclassing for OS-specific UI
○ Create subclasses for different OS (e.g., Windows).
○ Subclasses override factory method to produce OS-specific UI elements.
● Working with Abstract Buttons
○ Base Dialog class works with abstract buttons (base class or interface).
○ Ensures functional code regardless of the type of buttons used.
● Applicability to Other UI Elements
○ Factory method approach can be extended to other UI elements.
○ Potential transition to the Abstract Factory pattern as more factory methods are added.
Factory Method
Factory Method
Factory Method
Factory Method
Factory Method
Factory Method
Factory Method
❏ Applicability
● Use the Factory Method when you don’t know beforehand the exact types and dependencies of
the objects your code should work with.
○ The Factory Method pattern decouples product construction from product usage, allowing easy
extension of product types by creating new creator subclasses and overriding the factory method.
● Use the Factory Method when you want to provide users of your library or framework with a way
to extend its internal components.
○ To override the default behavior of a library or framework, consolidate component construction into a
single factory method. Allow users to extend and override this method while also extending the
component itself. This approach enables customization, such as using a subclass (e.g., RoundButton)
instead of a default component in the framework by creating a subclass of the framework class and
overriding the factory method (e.g., createButton).
● Use the Factory Method when you want to save system resources by reusing existing objects
instead of rebuilding them each time.
○ Reusing large, resource-intensive objects like database connections, file systems, and network
resources involves creating a storage pool for tracking and managing objects. To achieve this, a
factory method is often used because constructors can't return existing instances but only create new
Factory Method (How to Implement)
● Define Common Interface
○ Ensure all products adhere to the same interface with relevant methods.
● Create an Empty Factory Method
○ Inside the creator class, add a factory method returning the common product interface.
● Replace Product Constructors
○ In the creator's code, replace product constructors with calls to the factory method.
○ Extract product creation code into the factory method.
○ Use a temporary parameter to control the product type.
● Create Subclasses
○ Develop creator subclasses for each product type listed in the factory method.
○ Override the factory method in these subclasses.
○ Extract specific construction code from the base factory method.
● Handling Multiple Product Types
○ For numerous product types, consider using a control parameter in subclasses.
○ Clients can specify the desired product type through the factory method's parameter.
● Abstract Factory Method
○ If the base factory method becomes empty after extractions, make it abstract.
○ If some code remains, keep it as the default behavior of the method.
Factory Method
Factory Method
Abstract Factory
Abstract Factory
Abstract Factory (Problem)
● Family of Related Products
○ Define a family of furniture products: Chair, Sofa, and CoffeeTable.
● Variants within the Family
○ Recognize multiple variants for the products: Modern, Victorian, and ArtDeco.
● Objective: Matched Furniture Sets
○ Ensure that created furniture objects within a family match each other in style and variant.
○ Prevent customers from receiving non-matching furniture.
● Flexible Product Addition
○ Implement a Abstract Factory pattern to create furniture objects.
○ Avoid changing existing code when adding new products or product families.
● Adapt to Frequent Catalog Updates
○ Accommodate frequent updates from furniture vendors without modifying core code.
● Benefits of Abstract Factory
○ Achieve product consistency within a family and variant.
○ Enable seamless integration of new products and families into the simulation.
● Conclusion
○ Utilizing the Abstract Factory pattern ensures consistent and adaptable furniture creation in the shop
simulator, catering to customer preferences and frequent catalog updates.
Abstract Factory (Problem)
Abstract Factory (Solution)
● Interface Declaration
○ Explicitly declare interfaces for each product within the product family (e.g., Chair, Sofa, CoffeeTable).
○ Ensure all product variants adhere to these interfaces for consistency.
● Abstract Factory Declaration
○ Create an Abstract Factory interface with creation methods for all products in the family (e.g.,
createChair, createSofa, createCoffeeTable).
○ These methods return abstract product types based on the previously defined interfaces.
● Variant-Specific Factory Classes
○ For each product variant within the family, create separate factory classes based on the Abstract
Factory interface.
○ Example: ModernFurnitureFactory for ModernChair, ModernSofa, and ModernCoffeeTable.
● Client Code Interaction
○ Client code interacts with factories and products via their abstract interfaces.
○ Allows flexibility in changing factory types and product variants without affecting client code.
● Uniform Product Handling
○ Regardless of chair variant (e.g., Modern or Victorian), clients treat all chairs using the abstract Chair
interface.
○ Ensures consistency among products within the same family.
● Factory Object Creation
Abstract Factory
Abstract Factory
Abstract Factory (Pseudocode)

The Abstract Factory pattern allows the


creation of cross-platform UI elements
while maintaining consistency with the
selected operating system's style. It
involves an abstract interface with creation
methods, concrete factories for specific
operating systems, and client code that
interacts with UI elements through abstract
interfaces. This approach ensures the right
elements are created based on the current
OS, minimizes dependencies on concrete
classes, and supports future additions of
new UI element variations without
modifying client code.
Abstract Factory (Pseudocode)
Abstract Factory (Pseudocode)
Abstract Factory (Pseudocode)
Abstract Factory (Pseudocode)
Abstract Factory (Pseudocode)
Abstract Factory (Pseudocode)
Abstract Factory
Abstract Factory
Abstract Factory
Abstract Factory
References

● https://www.geeksforgeeks.org/coupling-in-java/
● https://www.geeksforgeeks.org/introduction-to-pattern-designing/
● https://www.pentalog.com/blog/design-patterns/factory-method-design-
pattern/
● https://www.geeksforgeeks.org/factory-method-for-designing-pattern/
● https://www.geeksforgeeks.org/abstract-factory-pattern/
● https://www.geeksforgeeks.org/python-design-patterns/
● https://sourcemaking.com/design_patterns/factory_method
● https://sourcemaking.com/design_patterns/abstract_factory
Thank You!

You might also like