zoukankan      html  css  js  c++  java
  • 设计模式之抽象工厂模式

    有些情况下我们需要根据不同的选择逻辑提供不同的构造工厂,而对于多个工厂而言需要一个统一的抽象工厂
      1 class System{}
      2 class Soft{}
      3 
      4 class MacSystem extends System{}
      5 class MacSoft extends Soft{}
      6 
      7 class WinSystem extends System{}
      8 class WinSoft extends Soft{}
      9 
     10 /**
     11  * AbstractFactory class[抽象工厂模式]
     12  */
     13 interface AbstractFactory {
     14     public function CreateSystem();
     15     public function CreateSoft();
     16 }
     17 
     18 class MacFactory implements AbstractFactory{
     19     public function CreateSystem()
     20     {
     21         // TODO: Implement CreateSystem() method.
     22         return new MacSystem();
     23     }
     24     public function CreateSoft()
     25     {
     26         // TODO: Implement CreateSoft() method.
     27         return new MacSoft();
     28     }
     29 }
     30 有些情况下我们需要根据不同的选择逻辑提供不同的构造工厂,而对于多个工厂而言需要一个统一的抽象工厂
     31 class System{}
     32 class Soft{}
     33 
     34 class MacSystem extends System{}
     35 class MacSoft extends Soft{}
     36 
     37 class WinSystem extends System{}
     38 class WinSoft extends Soft{}
     39 
     40 /**
     41  * AbstractFactory class[抽象工厂模式]
     42  */
     43 interface AbstractFactory {
     44     public function CreateSystem();
     45     public function CreateSoft();
     46 }
     47 
     48 class MacFactory implements AbstractFactory{
     49     public function CreateSystem()
     50     {
     51         // TODO: Implement CreateSystem() method.
     52         return new MacSystem();
     53     }
     54     public function CreateSoft()
     55     {
     56         // TODO: Implement CreateSoft() method.
     57         return new MacSoft();
     58     }
     59 }
     60 
     61 class WinFactory implements AbstractFactory{
     62     public function CreateSystem()
     63     {
     64         // TODO: Implement CreateSystem() method.
     65         return new WinSystem();
     66     }
     67 
     68     public function CreateSoft()
     69     {
     70         // TODO: Implement CreateSoft() method.
     71         return new WinSoft();
     72     }
     73 }
     74 
     75 //test
     76 $MacFactory_obj = new MacFactory();
     77 var_dump($MacFactory_obj->CreateSystem());
     78 var_dump($MacFactory_obj->CreateSoft());
     79 
     80 $winFactory_obj = new WinFactory();
     81 var_dump($winFactory_obj->CreateSystem());
     82 var_dump($winFactory_obj->CreateSoft());
     83 
     84 
     85 class WinFactory implements AbstractFactory{
     86     public function CreateSystem()
     87     {
     88         // TODO: Implement CreateSystem() method.
     89         return new WinSystem();
     90     }
     91 
     92     public function CreateSoft()
     93     {
     94         // TODO: Implement CreateSoft() method.
     95         return new WinSoft();
     96     }
     97 }
     98 
     99 //test
    100 $MacFactory_obj = new MacFactory();
    101 var_dump($MacFactory_obj->CreateSystem());
    102 var_dump($MacFactory_obj->CreateSoft());
    103 
    104 $winFactory_obj = new WinFactory();
    105 var_dump($winFactory_obj->CreateSystem());
    106 var_dump($winFactory_obj->CreateSoft());   
  • 相关阅读:
    Docker build Dockerfile 构建镜像
    Docker 容器启动 查看容器状态
    Docker 获取镜像
    Docker 容器状态查看
    windows 检测进程pid
    bzoj 1083 最小生成树
    bzoj 2039 最小割模型
    bzoj 2749 杂题
    bzoj 2748 DP
    bzoj 3190 维护栈
  • 原文地址:https://www.cnblogs.com/caohongchang/p/11537559.html
Copyright © 2011-2022 走看看