打赏

抽象工厂模式

抽象工厂模式

<?php
interface PayInterface
{
   public function pay();
}

class AliPay implements PayInterface
{
   public function pay()
   {
       return "支付宝支付";
   }
}

class TenPay implements PayInterface
{
   public function pay()
   {
       return "微信支付";
   }
}

interface LogInterface
{
   public function log();
}

class MysqlLog implements LogInterface
{
   public function log()
   {
       return "记录mysql日志";
   }
}

class RedisLog implements LogInterface
{
   public function log()
   {
       return "记录Redis日志";
   }
}

interface PayFactoryInterface
{
   public function AliPayMake();

   public function TenPayMake();
}

interface LogFactoryInterface
{
   public function MysqlLogMake();

   public function RedisLogMake();
}

class PayFactory implements PayFactoryInterface
{
   public function AliPayMake()
   {
       return new AliPay();
   }

   public function TenPayMake()
   {
       return new TenPay();
   }
}

class LogFactory implements LogFactoryInterface
{
   public function MysqlLogMake()
   {
       return new MysqlLog();
   }

   public function RedisLogMake()
   {
       return new RedisLog();
   }
}

class Order
{
   protected $log;

   protected $pay;

   public function __construct()
   {
       $LogFactory = new LogFactory();
       $PayFactory = new PayFactory();
       $this->log = array(
           'mysql' => $LogFactory->MysqlLogMake(),
           'redis' => $LogFactory->RedisLogMake()
       );
       $this->pay = array(
           'Ali' => $PayFactory->AliPayMake(),
           'Ten' => $PayFactory->TenPayMake()
       );
   }

   public function getLog()
   {
       return $this->log;
   }

   public function getPay()
   {
       return $this->pay;
   }
}

$order = new Order();
var_dump($order->getLog());
var_dump($order->getPay());
exit;

 

 

posted on 2021-08-21 12:27  头大的冯冯  阅读(9)  评论(0编辑  收藏  举报

导航