The Abstract Factory Pattern is a design pattern that provides an interface for creating families of related or dependent objets without specifying their concrete classes. This pattern is particularly useful when you need to create objects that are part of a group or have some common characteristics.
Key concepts of the Abstract Factory Pattern:
- Separate the process of creating objects from the actual implementation.
- Use interfaces to define factories and product families.
- Provide a common interface for creating a family of related objects.
- Enhance the maintainability and flexbility of the code by eliminating the need for hard-coded dependencies.
To further illustrate the Abstract Factory Pattern's advantages, let's consider an example involving a "Vehicle" class hierarchy. A vehicle can have different types such as car, motorcycle, or truck. Each vehicle type can have various attributes, such as color, engine type, number of wheels, etc. Using the Abstract Factory Pattern, you can create instances of different vehicle types while using the same construction process, making it easier to create different vehicle variations without having to write separate constructors for each combination.
Implementing the Abstract Factory Pattern:
- Create an interface for the abstract factory that declares methods for creating abstract products.
- Implement concrete factories that inherit from the abstract factory interface and create concrete products.
- Create an interface for each type of abstract product, which declares methods that the products must implement.
- Implement concrete products that inherit from the abstract product interfaces.
- Use the concrete factories to create instances of concrete products.
Here's a simple example of implementing the Abstract Factory Pattern using a "Vehicle" class hierarchy:
Abstract Vehicle class:
public abstract class Vehicle {
public abstract String getColor();
public abstract String getEngineType();
public abstract int getNumberOfWheels();
}
Concrete Car class:
public class Car extends Vehicle {
private String color;
private String engineType;
private int numberOfWheels;
public Car(String color, String engineType, int numberOfWheels) {
this.color = color;
this.engineType = engineType;
this.numberOfWheels = numberOfWheels;
}
@Override
public String getColor() {
return color;
}
@Override
public String getEngineType() {
return engineType;
}
@Override
public int getNumberOfWheels() {
return numberOfWheels;
}
}
Abstract Factory interface:
public interface VehicleFactory {
Vehicle createVehicle(String color, String engineType, int numberOfWheels);
}
Concrete CarFactory class:
public class CarFactory implements VehicleFactory {
@Override
public Vehicle createVehicle(String color, String engineType, int numberOfWheels) {
return new Car(color, engineType, numberOfWheels);
}
}
Client code:
public class Main {
public static void main(String[] args) {
VehicleFactory carFactory = new CarFactory();
Vehicle car = carFactory.createVehicle("Red", "Gasoline", 4);
System.out.println("Car color: " + car.getColor());
System.out.println("Car engine type: " + car.getEngineType());
System.out.println("Car number of wheels: " + car.getNumberOfWheels());
}
}
Using the Abstract Factory Pattern improves the maintainability and flexibility of your code, making it easier to create complex objects with various attributes, and simplifying the process of extending the code with new types of objects.
'Java > Design Pattern' 카테고리의 다른 글
Understand of Builder Pattern (0) | 2023.04.06 |
---|