创建型设计模式-工厂方法 Factory Method
1.创建型设计模式-工厂方法 Factory Method
简介
工厂方法中,每一个具体工厂类都对应创建一个具体产品类,所有具体工厂类都实现抽象工厂,所有具体产品类都实现抽象产品。
抽象工厂定义了创建抽象产品的方法签名,具体工厂类各自实现各自逻辑,来创建具体的产品。
角色
-
抽象工厂 Abstract Factory
定义创建产品的方法签名,即Factory Method
-
抽象产品 Abstract Product
定义产品的基本属性
-
具体工厂 Concrete Factory
实现自抽象工厂,并实现 Factory Method,实现如何创建具体产品。
-
具体产品 Concrete Product
实现具体产品基本属性
类图
如图所示,Dialog抽象工厂可以创建Button抽象产品,WindowsDialog和WebDialog都是具体工厂,负责创建WindownsButton和HTMLButton。
代码
abstract class Creator
{
abstract public function factoryMethod(): Product;
public function someOperation(): string
{
$product = $this->factoryMethod();
$result = "Creator: The same creator's code has just worked with " . $product->operation();
return $result;
}
}
class ConcreteCreator1 extends Creator
{
public function factoryMethod(): Product
{
return new ConcreteProduct1();
}
}
class ConcreteCreator2 extends Creator
{
public function factoryMethod(): Product
{
return new ConcreteProduct2();
}
}
interface Product
{
public function operation(): string;
}
class ConcreteProduct1 implements Product
{
public function operation(): string
{
return "{Result of the ConcreteProduct1}";
}
}
class ConcreteProduct2 implements Product
{
public function operation(): string
{
return "{Result of the ConcreteProduct2}";
}
}
function clientCode(Creator $creator)
{
echo "Client: I'm not aware of the creator's class, but it still works.\n" . $creator->someOperation() . "\n";
}
echo "App: Launched with the ConcreteCreator1.\n";
clientCode(new ConcreteCreator1());
echo "App: Launched with the ConcreteCreator2.\n";
clientCode(new ConcreteCreator2());
output
App: Launched with the ConcreteCreator1.
Client: I'm not aware of the creator's class, but it still works.
Creator: The same creator's code has just worked with {Result of the ConcreteProduct1}
App: Launched with the ConcreteCreator2.
Client: I'm not aware of the creator's class, but it still works.
Creator: The same creator's code has just worked with {Result of the ConcreteProduct2}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)