不同对象类型的工厂对象
创始人
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公共底牌,pokemomo辅助工具(辅助挂)详细开挂辅助挂;无需打开直接搜索...
透视真的!三哥玩摆头辅助,哈糖... 透视真的!三哥玩摆头辅助,哈糖大菠萝免费辅助器(辅助挂)详细开挂辅助安装;无需打开直接搜索薇:136...
透明挂透视!微乐小程序外辅助工... 微乐小程序外辅助工具是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加...
透明黑科技!微信小程序雀神挂件... 透明黑科技!微信小程序雀神挂件辅助,wepoker正确养号方法(辅助挂)详细开挂辅助平台1、下载安装...
透视能赢!欢乐卡五星插件,德普... 透视能赢!欢乐卡五星插件,德普之星app安卓版破解版(辅助挂)详细开挂辅助器1、下载安装好德普之星a...
透视游戏!wepoker插件下... 透视游戏!wepoker插件下载,聚星ai辅助工具激活码(辅助挂)详细开挂辅助工具;无需打开直接搜索...
透视app!闽乐乐510k脚本... 您好:这款hhpoker透视方法游戏是可以开挂的,确实是有挂的,很多玩家在这款hhpoker透视方法...
透视软件!wepoker作弊视... 【亲,wepoker钻石怎么看底牌 这款游戏可以开挂的,确实是有挂的,很多玩家在这款wepoker钻...
透明规律!wepoker有没有... 透明规律!wepoker有没有挂,wepoker模拟器哪个(辅助挂)详细开挂辅助挂1、下载安装好we...
透视玄学!仙神互娱辅助,aap... aapoker透视怎么用是一款专注玩家量身打造的游戏记牌类型软件,在aapoker透视怎么用这款游戏...