不同对象类型的工厂对象
创始人
2025-01-08 23:00:25
0

使用抽象工厂模式,根据不同的条件返回不同的工厂对象。

实现代码如下:

// 定义一个抽象工厂接口
interface AbstractFactory {
   public function createProductA();
   public function createProductB();
}

// 实现具体工厂类
class ConcreteFactoryForType1 implements AbstractFactory {
   public function createProductA() {
      return new ProductType1A();
   }
   public function createProductB() {
      return new ProductType1B();
   }
}

class ConcreteFactoryForType2 implements AbstractFactory {
   public function createProductA() {
      return new ProductType2A();
   }
   public function createProductB() {
      return new ProductType2B();
   }
}

// 定义抽象产品接口
interface AbstractProductA {
   public function usefulFunctionA(): string;
}

interface AbstractProductB {
   public function usefulFunctionB(): string;
}

// 实现具体产品类
class ProductType1A implements AbstractProductA {
   public function usefulFunctionA(): string {
      return "The result of the product A1.";
   }
}

class ProductType1B implements AbstractProductB {
   public function usefulFunctionB(): string {
      return "The result of the product B1.";
   }
}

class ProductType2A implements AbstractProductA {
   public function usefulFunctionA(): string {
      return "The result of the product A2.";
   }
}

class ProductType2B implements AbstractProductB {
   public function usefulFunctionB(): string {
      return "The result of the product B2.";
   }
}

// 客户端代码,根据不同条件调用不同的工厂来获取不同类型的对象
$clientCode = function (AbstractFactory $factory) {
    $productA = $factory->createProductA();
    $productB = $factory->createProductB();

    echo $productA->usefulFunctionA() . "
"; echo $productB->usefulFunctionB() . "
"; }; // 调用客户端代码 $clientCode(new ConcreteFactoryForType1()); $clientCode(new ConcreteFactoryForType2());

相关内容

热门资讯

据文件显示!aapoker安装... 据文件显示!aapoker安装包怎么使用,同乡游辅助软件,真是存在有挂(详情线上教程)-哔哩哔哩小薇...
透视脚本!agpoker辅助,... 透视脚本!agpoker辅助,wepoker科技辅助器,教程书教程(竟然是有挂)-哔哩哔哩1、不需要...
透视代打ai!红龙poker辅... 透视代打ai!红龙poker辅助指令(透视)开挂辅助技巧(切实是真的挂)-哔哩哔哩1、在红龙poke...
经核实!拱趴大菠萝挂怎么安装,... 经核实!拱趴大菠萝挂怎么安装,哈糖大菠萝助手,真是存在有挂(教你玩家教你)-哔哩哔哩1、拱趴大菠萝挂...
透视工具!epoker透视底牌... 透视工具!epoker透视底牌,哈糖大菠萝开挂,操作教程(确实真的是有挂)-哔哩哔哩暗藏猫腻,小编详...
透视苹果版本!hhpoker辅... 透视苹果版本!hhpoker辅助挂(透视)开挂辅助工具(切实是有挂)-哔哩哔哩1、每一步都需要思考,...
透视好友!wepoker轻量版... 透视好友!wepoker轻量版有透视吗,浙江宝宝游戏辅助器,竟然有挂(详情详细教程)-哔哩哔哩1、完...
透视苹果版!aapoker辅助... 透视苹果版!aapoker辅助怎么用,hhpoker一直输有挂吗,课程教程(一直真的有挂)-哔哩哔哩...
透视系统!悦扑克脚本(透视)开... 透视系统!悦扑克脚本(透视)开挂辅助器(确实真的是有挂)-哔哩哔哩1、悦扑克脚本透视辅助简单,悦扑克...
此事引发广泛关注!wepoke... 您好,拱趴大菠萝辅助方法这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多...