什么是工厂
这种模式的存在意义,其实就是,把创建对象的权利从程序员手里转换到一个全局的类里面,也就是我们所说的静态工厂。静态工厂存在的意义,其实就是可以直接通过方法去创建对象,而不用去实例化一个具体的对象。其实还有一种叫动态工厂,其实无非是我们new一个工厂类,然后再由这个工厂类去创建对象。
下面我们分别来看,在C加加和JAVA里面的实现。😏
C++ STATIC Factory Pattern
#include <iostream>
#include <memory>
// 产品基类
class Product {
public:
virtual void display() = 0;
};
// 具体产品类 A
class ConcreteProductA : public Product {
public:
void display() override {
std::cout << "This is Concrete Product A." << std::endl;
}
};
// 具体产品类 B
class ConcreteProductB : public Product {
public:
void display() override {
std::cout << "This is Concrete Product B." << std::endl;
}
};
// 静态工厂类
class Factory {
public:
static std::shared_ptr<Product> createProduct(int type) {
if (type == 1) {
return std::make_shared<ConcreteProductA>();
} else if (type == 2) {
return std::make_shared<ConcreteProductB>();
} else {
return nullptr;
}
}
};
int main() {
// 使用静态工厂创建产品对象
std::shared_ptr<Product> productA = Factory::createProduct(1);
std::shared_ptr<Product> productB = Factory::createProduct(2);
if (productA) {
productA->display();
}
if (productB) {
productB->display();
}
return 0;
}
C++动态工厂模式
#include <iostream>
#include <memory>
#include <map>
#include <functional>
// 产品基类
class Product {
public:
virtual void display() = 0;
virtual ~Product() = default;
};
// 具体产品类 A
class ConcreteProductA : public Product {
public:
void display() override {
std::cout << "This is Concrete Product A." << std::endl;
}
};
// 具体产品类 B
class ConcreteProductB : public Product {
public:
void display() override {
std::cout << "This is Concrete Product B." << std::endl;
}
};
// 动态工厂类
class DynamicFactory {
public:
using Creator = std::function<std::shared_ptr<Product>()>;
std::map<int, Creator> creators;
void registerProduct(int id, Creator creator) {
creators[id] = creator;
}
std::shared_ptr<Product> createProduct(int id) {
if (creators.find(id) != creators.end()) {
return creators[id]();
} else {
return nullptr;
}
}
};
int main() {
DynamicFactory factory;
// 注册具体产品类的创建函数
factory.registerProduct(1, []() -> std::shared_ptr<Product> { return std::make_shared<ConcreteProductA>(); });
factory.registerProduct(2, []() -> std::shared_ptr<Product> { return std::make_shared<ConcreteProductB>(); });
// 创建具体产品对象
std::shared_ptr<Product> productA = factory.createProduct(1);
std::shared_ptr<Product> productB = factory.createProduct(2);
if (productA) {
productA->display();
}
if (productB) {
productB->display();
}
return 0;
}
Java 静态工厂模式
// 产品接口
interface Product {
void display();
}
// 具体产品类 A
class ConcreteProductA implements Product {
public void display() {
System.out.println("This is Concrete Product A.");
}
}
// 具体产品类 B
class ConcreteProductB implements Product {
public void display() {
System.out.println("This is Concrete Product B.");
}
}
// 静态工厂类
public class StaticFactory {
public static Product createProduct(String type) {
if ("A".equals(type)) {
return new ConcreteProductA();
} else if ("B".equals(type)) {
return new ConcreteProductB();
}
return null;
}
public static void main(String[] args) {
Product productA = StaticFactory.createProduct("A");
Product productB = StaticFactory.createProduct("B");
if (productA != null) {
productA.display();
}
if (productB != null) {
productB.display();
}
}
}
Java 动态工厂模式
import java.util.HashMap;
import java.util.Map;
// 产品接口
interface Product {
void display();
}
// 具体产品类 A
class ConcreteProductA implements Product {
public void display() {
System.out.println("This is Concrete Product A.");
}
}
// 具体产品类 B
class ConcreteProductB implements Product {
public void display() {
System.out.println("This is Concrete Product B.");
}
}
// 动态工厂类
class DynamicFactory {
private Map<String, Class<? extends Product>> productMap = new HashMap<>();
public void registerProduct(String type, Class<? extends Product> productClass) {
productMap.put(type, productClass);
}
public Product createProduct(String type) throws IllegalAccessException, InstantiationException {
Class<? extends Product> productClass = productMap.get(type);
if (productClass != null) {
return productClass.newInstance();
}
return null;
}
}
public class DynamicFactoryExample {
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
DynamicFactory factory = new DynamicFactory();
// 注册具体产品类的类型
factory.registerProduct("A", ConcreteProductA.class);
factory.registerProduct("B", ConcreteProductB.class);
// 创建具体产品对象
Product productA = factory.createProduct("A");
Product productB = factory.createProduct("B");
if (productA != null) {
productA.display();
}
if (productB != null) {
productB.display();
}
}
}
这就是工厂模式了
其实这种模式的用途在java里的AOC里面体现的淋漓尽致,而C++使用这种模式来做安全的内存访问