这里写目录标题
前言
工厂模式是一种常用的设计模式,它可以帮助我们更好地组织和管理代码,将对象的创建和使用分离开来,提高代码的可维护性和扩展性。
在软件开发中,我们经常会遇到需要创建多个不同类型的对象的情况。如果每次都使用 new
关键字来实例化对象,代码会变得冗长、难以维护,并且不易于扩展。为了解决这个问题,我们可以使用工厂模式来封装对象的创建过程,使得客户端只需要关心接口而不需要关心具体的实现。
工厂模式包括三种变体:简单工厂模式、工厂方法模式和抽象工厂模式。
简单工厂模式
简单工厂模式通过一个工厂类来创建所有产品的实例。客户端只需要提供给工厂类一个参数,工厂类根据这个参数决定实例化哪个具体产品类的对象并返回给客户端。
例如,我们有一个形状接口 Shape 和三个实现类 Circle、Square 和 Rectangle。我们可以创建一个 ShapeFactory 工厂类来根据客户端传递过来的参数来实例化相应的对象。
java
interface Shape {
void draw();
}
class Circle implements Shape {
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
class Square implements Shape {
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
class Rectangle implements Shape {
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
class ShapeFactory {
public Shape getShape(String shapeType) {
if (shapeType == null) {
return null;
}
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("SQUARE")) {
return new Square();
} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
}
return null;
}
}
客户端可以通过 ShapeFactory 工厂类来获取 Shape 对象,而不需要关心具体的实现。
java
ShapeFactory shapeFactory = new ShapeFactory();
Shape circle = shapeFactory.getShape("CIRCLE");
circle.draw();
Shape square = shapeFactory.getShape("SQUARE");
square.draw();
Shape rectangle = shapeFactory.getShape("RECTANGLE");
rectangle.draw();
例2:
cpp
#include<iostream>
using namespace std;
class animal {
public:
virtual void eat() = 0;
virtual void drink() = 0;
virtual ~animal() {}
};
class lion :public animal{
public:
void eat() {
cout << "lion eat meat" << endl;
}
void drink() {
cout << "lion drink hongniu" << endl;
}
~lion() {
cout << "animal lion delete" << endl;
}
};
class panda : public animal {
public:
void eat() {
cout << "panda eat bamboo" << endl;
}
void drink() {
cout << "panda drink water" << endl;
}
~panda() {
cout << "animal panda delete" << endl;
}
};
class factory {
public:
virtual animal* getanimal() = 0;
virtual ~factory() {}
};
class lionfactory :public factory {
public:
animal* getanimal() {
return new lion;
}
~lionfactory() {
cout << "lion delete" << endl;
}
};
class pandafactory : public factory {
public:
animal* getanimal() {
return new panda;
}
~pandafactory() {
cout << "panda delete" << endl;
}
};
int main()
{
factory * base = new pandafactory;
animal* test = base->getanimal();
test->drink();
test->eat();
delete test;
delete base;
return 0;
}
工厂方法模式
工厂方法模式定义了一个创建对象的接口,但将具体的对象创建延迟到子类中实现。每个子类都可以通过实现工厂方法来创建属于自己的具体产品。
例如,我们有一个披萨接口 Pizza 和两个实现类 CheesePizza 和 PepperoniPizza。我们可以创建一个 PizzaStore 抽象工厂类来定义一个 createPizza 的抽象方法,而具体的实现由不同类型的 PizzaStore 子类来实现。
java
interface Pizza {
void prepare();
void bake();
void cut();
void box();
}
class CheesePizza implements Pizza {
public void prepare() {
System.out.println("Preparing Cheese Pizza");
}
public void bake() {
System.out.println("Baking Cheese Pizza");
}
public void cut() {
System.out.println("Cutting Cheese Pizza");
}
public void box() {
System.out.println("Packing Cheese Pizza");
}
}
class PepperoniPizza implements Pizza {
public void prepare() {
System.out.println("Preparing Pepperoni Pizza");
}
public void bake() {
System.out.println("Baking Pepperoni Pizza");
}
public void cut() {
System.out.println("Cutting Pepperoni Pizza");
}
public void box() {
System.out.println("Packing Pepperoni Pizza");
}
}
abstract class PizzaStore {
public Pizza orderPizza(String type) {
Pizza pizza = createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
protected abstract Pizza createPizza(String type);
}
class NYPizzaStore extends PizzaStore {
protected Pizza createPizza(String type) {
if (type.equals("cheese")) {
return new CheesePizza();
} else if (type.equals("pepperoni")) {
return new PepperoniPizza();
}
return null;
}
}
客户端可以通过不同类型的 PizzaStore 子类来获取 Pizza 对象,而具体的实现由子类来决定。
java
PizzaStore nyStore = new NYPizzaStore();
Pizza cheesePizza = nyStore.orderPizza("cheese");
Pizza pepperoniPizza = nyStore.orderPizza("pepperoni");
抽象工厂模式
抽象工厂模式提供了一种创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。通过使用抽象工厂,客户端可以创建多个产品家族的对象。
例如,我们有一个形状接口 Shape 和两个实现类 RedCircle 和 BlueSquare,以及一个颜色接口 Color 和两个实现类 Red 和 Blue。我们可以创建一个抽象工厂 AbstractFactory 接口来定义两个抽象方法 getShape 和 getColor,由不同类型的子类来实现。
java
interface Shape {
void draw();
}
class RedCircle implements Shape {
public void draw() {
System.out.println("Inside RedCircle::draw() method.");
}
}
class BlueSquare implements Shape {
public void draw() {
System.out.println("Inside BlueSquare::draw() method.");
}
}
interface Color {
void fill();
}
class Red implements Color {
public void fill() {
System.out.println("Inside Red::fill() method.");
}
}
class Blue implements Color {
public void fill() {
System.out.println("Inside Blue::fill() method.");
}
}
interface AbstractFactory {
Shape getShape(String shapeType);
Color getColor(String colorType);
}
class ColorFactory implements AbstractFactory {
public Shape getShape(String shapeType) {
return null;
}
public Color getColor(String colorType) {
if (colorType.equalsIgnoreCase("RED")) {
return new Red();
} else if (colorType.equalsIgnoreCase("BLUE")) {
return new Blue();
}
return null;
}
}
class ShapeFactory implements AbstractFactory {
public Shape getShape(String shapeType) {
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new RedCircle();
} else if (shapeType.equalsIgnoreCase("SQUARE")) {
return new BlueSquare();
}
return null;
}
public Color getColor(String colorType) {
return null;
}
}
客户端可以通过不同类型的 AbstractFactory 子类来获取 Shape 和 Color 对象。
java
AbstractFactory shapeFactory = new ShapeFactory();
Shape redCircle = shapeFactory.getShape("CIRCLE");
redCircle.draw();
AbstractFactory colorFactory = new ColorFactory();
Color blue = colorFactory.getColor("BLUE");
blue.fill();
例2:
cpp
#include<iostream>
#include<string>
using namespace std;
class shipbody {
public:
virtual string getshipbody() = 0;
virtual ~shipbody(){}
};
class woodbody : public shipbody{
public:
string getshipbody() override{
return "wood made shipbody";
}
};
class steelbody : public shipbody {
public:
string getshipbody()override {
return "steel made shipbody";
}
};
class shipmove {
public:
virtual string getshipmove() = 0;
virtual ~shipmove(){}
};
class humanmove :public shipmove {
public:
string getshipmove() {
return "human move ship";
}
};
class fuelmove :public shipmove{
public:
string getshipmove() {
return "fuel move ship";
}
};
class ship {
public:
ship(shipbody* body,shipmove* move):m_body(body),m_move(move){}
~ship() {
delete m_move;
delete m_body;
}
string getship() {
string info = m_move->getshipmove() + m_body->getshipbody();
return info;
}
private:
shipbody* m_body;
shipmove* m_move;
};
class abstractfactory {
public:
virtual ship* createship() = 0;
virtual ~abstractfactory(){}
};
class standardfactory :public abstractfactory {
public:
ship* createship() {
cout << "standard finish create " << endl;
return new ship(new woodbody, new humanmove);
}
};
class enhancefactory : public abstractfactory {
public:
ship* createship() {
cout << "enhance finish create" << endl;
return new ship(new steelbody, new fuelmove);
}
};
int main()
{
abstractfactory* abs = new enhancefactory;
ship* obj = abs->createship();
cout<<obj->getship();
return 0;
}
总结
总的来说,工厂模式是一种非常有用的设计模式,它可以帮助我们更好地组织和管理代码,提高代码的可维护性和扩展性。在实际开发中,我们应该根据具体的场景选择不同类型的工厂模式,以便更好地满足需求。