top of page

Abstract Factory design pattern

The Abstract Factory design pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It is part of the Gang of Four (GoF) design patterns.

Here's how the Abstract Factory pattern works:

  1. Abstract Factory Interface: It defines a set of methods that create different abstract products (e.g., WebBasePage and BasePageAssertion). These methods usually return these abstract products.

  2. Concrete Factories: Concrete factories implement the abstract factory interface. Each concrete factory is responsible for creating a family of related concrete products. For example, you might have a WebFactory that creates Browser-specific products and a MobileFactory that creates Android/iOS-specific products.

  3. Abstract Products: These are abstract classes or interfaces that define the common interface for different product types within a family. For example, you might have abstract products like Button, Checkbox, and TestInput for a user interface library.

  4. Concrete Products: Concrete products are the actual classes that implement the abstract product interfaces [eg: LoginPageElement]. Each concrete factory is responsible for creating its set of concrete products. For example, the WebFactory creates WebButton, WebCheckbox, and WebWindow, while the MobileFactory creates MobileButton, MobileDropDown, and MobileTextInput.

  5. Client Code: The client code interacts with the abstract factory and the abstract product interfaces. It requests concrete objects [eg: LoginPage] from the factory without needing to know the specific classes of objects it's creating.

Key Benefits of the Abstract Factory Pattern:

  1. Encapsulation: It encapsulates the creation of related objects, ensuring that the created objects are compatible and properly configured.

  2. Isolation of System-Specific Code: The pattern allows you to isolate system-specific code in concrete factories, making it easier to adapt your application to different platforms or configurations.

  3. Consistency: It promotes the creation of consistent families of objects, reducing the chances of compatibility issues.

  4. Scalability: You can easily introduce new families of products (e.g., new operating systems or themes) by creating new concrete factories, without modifying existing client code.

  5. Maintainability: Changes to the product families are localized within the respective concrete factories, making maintenance and updates more manageable.

Example:

Imagine you are SDET automating both Web and Mobile scripts . You could have an abstract factory called PageElement with methods for creating Button and Checkbox. You might then have two concrete factories, WebFactory and MobileFactory, each implementing the BasePageElement interface and returning WebButton, WebTextInput, MobileButton, and MobileTextInput respectively.

This way, when a client wants to use elements, it can use the abstract factory and work with the abstract product interfaces (Button and TextInput) without needing to know the specific details of the underlying operating system.

5 views0 comments

Recent Posts

See All

Byju's

Careers: Click to Apply Profile : QA Engineer Location : Bengaluru Date : 09th Oct 2021 Java: 1. Segregate 0's and 1's in a given integer array. 2. Write a method using java that can create only 5 obj

Wipro Interview Questions

Careers: Click to Apply Profile : QA Engineer Location : Pune Date : 11th May 2021 Java: What are the differences between Constructor and Methods? What are the components that are not allowed in Inter

Capgemini Interview Questions

Careers: Click to Apply Profile : QA Engineer Location : Pune Date : 26th Jan 2021 Java: What is an abstract class? What is the difference between abstract and interface? Purpose of constructor in Jav

Comments


bottom of page