工厂模式
The Factory Pattern is a creational design pattern used in software development. It provides a way to encapsulate object creation without specifying the exact class of object that will be created.
工厂模式是一种用于软件开发的创建设计模式。它提供了一种封装对象创建的方法,而无需指定将要创建的对象的具体类别。
Scenario: Vehicle Manufacturing Application 车制造应用
Problem Description:
Imagine you are developing a software system for a vehicle manufacturer. The system needs to create instances of different types of vehicles like cars, motorcycles, and trucks. Directly instantiating these objects in the client code can lead to several issues, such as tight coupling between the client code and the concrete classes, making it difficult to add or change the types of vehicles in the future.
想象一下,您正在为一家汽车制造商开发一个软件系统。该系统需要创建汽车、摩托车和卡车等不同类型车辆的实例。在客户端代码中直接实例化这些对象可能会导致一些问题,例如客户端代码与具体类之间的紧密耦合,使得将来添加或更改车辆类型变得困难。
How Factory Pattern Solves the Problem:
Defining an Interface or Abstract Class:
- First, you define a vehicle interface or an abstract vehicle class. This forms the contract for all concrete vehicle types. 首先,定义一个车辆接口或抽象车辆类。这构成了所有具体车辆类型的定约。
Creating Concrete Classes:
- Next, you create concrete classes for each vehicle type, like
Car,Motorcycle, andTruck. These classes implement the vehicle interface or extend the abstract vehicle class. 然后,为每种车辆类型创建具体类,如Car、Motorcycle和Truck。这些类实现了车辆接口或扩展了抽象车辆类。
- Next, you create concrete classes for each vehicle type, like
Implementing the Factory:
- Then, you create a
VehicleFactoryclass. This class has a method, often calledcreateVehicle, which takes a parameter that specifies the type of vehicle to create. 然后,您将创建一个VehicleFactory类。该类有一个方法,通常称为createVehicle,它接受一个参数,指定要创建的车辆类型。
- Then, you create a
Decoupling Object Creation:
- The client code calls the
createVehiclemethod of the factory to instantiate objects, instead of directly using thenewkeyword. This decouples the client code from the concrete classes and adheres to the principle of programming to interfaces, not implementations. 客户端代码调用工厂的createVehicle方法来实例化对象,而不是直接使用new关键字。这将客户端代码与具体类分离开来,并遵循了根据接口而非实现编程的原则。
- The client code calls the
Sample Code
Vehicle Interface
public interface Vehicle {
void manufacture();
}Concrete Classes
public class Car implements Vehicle {
@Override
public void manufacture() {
System.out.println("Manufacturing Car");
}
}
public class Motorcycle implements Vehicle {
@Override
public void manufacture() {
System.out.println("Manufacturing Motorcycle");
}
}
public class Truck implements Vehicle {
@Override
public void manufacture() {
System.out.println("Manufacturing Truck");
}
}Factory Class
public class VehicleFactory {
public Vehicle createVehicle(String type) {
if (type == null) {
return null;
}
if (type.equalsIgnoreCase("CAR")) {
return new Car();
} else if (type.equalsIgnoreCase("MOTORCYCLE")) {
return new Motorcycle();
} else if (type.equalsIgnoreCase("TRUCK")) {
return new Truck();
}
return null;
}
}Using the Factory
public class VehicleManufacturingClient {
public static void main(String[] args) {
VehicleFactory factory = new VehicleFactory();
Vehicle myCar = factory.createVehicle("Car");
myCar.manufacture();
Vehicle myMotorcycle = factory.createVehicle("Motorcycle");
myMotorcycle.manufacture();
Vehicle myTruck = factory.createVehicle("Truck");
myTruck.manufacture();
}
}In this example, the VehicleFactory class takes care of creating vehicle instances. The client code (in VehicleManufacturingClient) does not need to know about the concrete classes that implement the Vehicle interface. This makes it easier to add new types of vehicles in the future without modifying the client code, as only the factory class needs to be updated. This pattern is particularly useful in scenarios where a system needs to be independent from the way its objects are created, composed, and represented.
在本例中, VehicleFactory 类负责创建车辆实例。客户端代码(在 VehicleManufacturingClient 中)不需要知道实现 Vehicle 接口的具体类。这使得将来添加新类型的车辆变得更加容易,而无需修改客户端代码,因为只需更新工厂类即可。在系统需要独立于其对象的创建、组成和表示方式的情况下,这种模式尤其有用。

Any other methods implemented in the abstract Creator are written to operate on products produced by the factory method.
在抽象的 Creator 中实现的任何其他方法,是用来操作工厂方法生成的产品。
Create By GPT-4
评论已关闭