wrjc1hod 发表于 2024-10-4 19:36:58

PHP 平常设计模式——工厂模式


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">近期</span>参加了几次面试,<span style="color: black;">针对</span>应用<span style="color: black;">平常</span>的几种设计模式问题,深有感触。为加强<span style="color: black;">自己</span>理解,<span style="color: black;">同期</span><span style="color: black;">亦</span><span style="color: black;">期盼</span>能给<span style="color: black;">有些</span>初级<span style="color: black;">研发</span>者<span style="color: black;">必定</span>的参考学习,决定<span style="color: black;">起始</span>就PHP<span style="color: black;">平常</span>的设计模式写下几篇博文,工作<span style="color: black;">原由</span>,会不<span style="color: black;">定时</span>更新,感谢您的耐心阅读!</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">今天这篇<span style="color: black;">文案</span><span style="color: black;">重点</span>是描述一下PHP<span style="color: black;">平常</span>设计模式之工厂模式。工厂模式其实<span style="color: black;">能够</span>划分为:简单工厂模式、工厂<span style="color: black;">办法</span>模式、抽象工厂模式等。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.简单工厂模式</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">简介:简单工厂模式又被<span style="color: black;">叫作</span>为静态工厂<span style="color: black;">办法</span>模式,属于设计模式中的类创建模式。官方解释为:定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂模式使一个类的实例化延迟到其子类。<span style="color: black;">通常</span>用在<span style="color: black;">详细</span><span style="color: black;">制品</span>很少扩展的<span style="color: black;">状况</span>下,<span style="color: black;">不消</span>经常修改,且不修改代码的话是<span style="color: black;">不可</span>实现扩展的。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">优缺点:优点在于实现对象的创建和对象的<span style="color: black;">运用</span>分离,将对象的创建交给专门的工厂类负责;缺点在于工厂类<span style="color: black;">不足</span>灵活,<span style="color: black;">增多</span>新的<span style="color: black;">制品</span>时需要修改工厂类的代码,一旦<span style="color: black;">制品</span>较多时,工厂类将会变得<span style="color: black;">反常</span><span style="color: black;">繁杂</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">详细</span>代码实例:</p><span style="color: black;"><span style="color: black;">&lt;?php</span>

      <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">A</span>
      </span>{
      <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">__construct</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">echo</span> <span style="color: black;">"我是A类"</span>;
      }
      }<span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">B</span>
      </span>{
      <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">__construct</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">echo</span> <span style="color: black;">"我是B类"</span>;
      }
      }

      <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Factory</span>
      </span>{
      <span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">create</span><span style="color: black;">($name)</span>
      </span>{
      <span style="color: black;">if</span> ($name == <span style="color: black;">A</span>) {
      <span style="color: black;">return</span> <span style="color: black;">new</span>A();
      }<span style="color: black;">elseif</span> ($name == <span style="color: black;">B</span>) {
      <span style="color: black;">return</span> <span style="color: black;">new</span> B();
      }
      }
      }

      $a = Factory::create(<span style="color: black;">A</span>);
      $b = Factory::create(<span style="color: black;">B</span>);</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.工厂<span style="color: black;">办法</span>模式</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">简介:工厂<span style="color: black;">办法</span>模式<span style="color: black;">经过</span>定义了一个抽象出来的核心工厂类,并<span style="color: black;">同期</span>定义创建<span style="color: black;">制品</span>对象的接口,而创建<span style="color: black;">详细</span>实例的工作延迟到工厂子类中去完成。这种设计方式好处是核心类只需要关注工厂类的接口定义,<span style="color: black;">详细</span>的实例交由<span style="color: black;">详细</span>的工厂子类去创建。当需要新增一个<span style="color: black;">制品</span>时,无需修改现有的代码,只需要添加一个新的<span style="color: black;">制品</span>类和对应的工厂子类<span style="color: black;">就可</span>,<span style="color: black;">促进</span>整体的扩展性更好。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">优缺点:基于其简介<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">认识</span>到,工厂<span style="color: black;">办法</span>模式是在简单工厂模式的<span style="color: black;">基本</span>上进一步的抽象,<span style="color: black;">保存</span>了简单工厂模式的优点,且<span style="color: black;">处理</span>了它的缺点。在工厂<span style="color: black;">办法</span>模式中,核心类仅仅负责给出<span style="color: black;">详细</span>工厂必须实现的接口,而无需再负责<span style="color: black;">详细</span>的<span style="color: black;">制品</span>类实例化,使得系统<span style="color: black;">能够</span>在不修改整体代码的<span style="color: black;">状况</span>下方便的引进新的<span style="color: black;">制品</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">具体代码实例:</p><span style="color: black;"><span style="color: black;">&lt;?php</span>

      <span style="color: black;"><span style="color: black;">interface</span> <span style="color: black;">Basic</span>
      </span>{
      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">actionOne</span><span style="color: black;">()</span></span>;

      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">actionTwo</span><span style="color: black;">()</span></span>;
      }

      <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">A</span> <span style="color: black;">implements</span> <span style="color: black;">Basic</span>
      </span>{
      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">actionOne</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">echo</span> <span style="color: black;">"我是A类—<span style="color: black;">办法</span>1"</span>;
      }

      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">actionTwo</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">echo</span> <span style="color: black;">"我是A类—<span style="color: black;">办法</span>2"</span>;
      }
      }

      <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">B</span> <span style="color: black;">implements</span> <span style="color: black;">Basic</span>
      </span>{
      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">actionOne</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">echo</span> <span style="color: black;">"我是B类—<span style="color: black;">办法</span>1"</span>;
      }

      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">actionTwo</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">echo</span> <span style="color: black;">"我是B类—<span style="color: black;">办法</span>2"</span>;
      }
      }

      <span style="color: black;">abstract</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Factory</span>
      </span>{
      <span style="color: black;">abstract</span> <span style="color: black;">static</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">createBasic</span><span style="color: black;">()</span></span>;
      }

      <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">A_Factory</span> <span style="color: black;">extends</span> <span style="color: black;">Factory</span>
      </span>{
      <span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">createBasic</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">return</span> <span style="color: black;">new</span> A();
      }
      }

      <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">B_Factory</span> <span style="color: black;">extends</span> <span style="color: black;">Factory</span>
      </span>{
      <span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">createBasic</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">return</span> <span style="color: black;">new</span>B();
      }
      }

      $a = A_Factory::createBasic();
      $a-&gt;actionOne();
      $a-&gt;actionTwo();

      $b = B_Factory::createBasic();
      $b-&gt;actionOne();
      $b-&gt;actionTwo();</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3.抽象工厂模式</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">简介</span></strong><span style="color: black;">:抽象工厂模式<span style="color: black;">供给</span>一个创建一系列<span style="color: black;">关联</span>或相互依赖对象的接口,而无须指定它们<span style="color: black;">详细</span>的类。此模式是在工厂<span style="color: black;">办法</span>模式上的进一步扩展。在工厂<span style="color: black;">办法</span>模式中,一个<span style="color: black;">详细</span>工厂负责一类<span style="color: black;">详细</span><span style="color: black;">制品</span>的处理(一对一关系),但当<span style="color: black;">咱们</span>需要一个工厂负责生成多种类型<span style="color: black;">制品</span>时,就要抽象<span style="color: black;">全部</span>工厂类了。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">举一个<span style="color: black;">详细</span>生活实例</span></strong><span style="color: black;">:</span></p><span style="color: black;"><span style="color: black;">制品</span>结构:假设一个抽象类是手机,其子类有小米手机、华为手机、荣耀手机,<span style="color: black;">那样</span>抽象的手机和<span style="color: black;">详细</span>品牌的手机之间则构<span style="color: black;">成为了</span>一个<span style="color: black;">制品</span>结构,抽象手机是父类,<span style="color: black;">详细</span>品牌手机是其子类。</span><span style="color: black;"><span style="color: black;">制品</span>组:<span style="color: black;">制品</span>组<span style="color: black;">指的是</span>由同一工厂生产,<span style="color: black;">位置于</span><span style="color: black;">区别</span><span style="color: black;">制品</span>结构中的一组<span style="color: black;">制品</span>。如华为厂家生产的华为手机、华为手环,华为手机属于手机<span style="color: black;">制品</span>结构中,华为手环则属于手环<span style="color: black;">制品</span>结构中中。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">详细</span>代码实例</span></strong><span style="color: black;">:</span></p><span style="color: black;"><span style="color: black;">&lt;?php</span>

      <span style="color: black;"><span style="color: black;">interface</span> <span style="color: black;">Phone</span>
      </span>{
      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">open</span><span style="color: black;">()</span></span>;

      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">operate</span><span style="color: black;">()</span></span>;
      }

      <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">HuaweiPhone</span> <span style="color: black;">implements</span> <span style="color: black;">Phone</span>
      </span>{
      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">open</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">echo</span> <span style="color: black;">"打开华为手机"</span>;
      }

      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">operate</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">echo</span> <span style="color: black;">"操作华为手机"</span>;
      }
      }

      <span style="color: black;"><span style="color: black;">interface</span> <span style="color: black;">Bracelet</span>
      </span>{
      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">look</span><span style="color: black;">()</span></span>;

      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">click</span><span style="color: black;">()</span></span>;
      }

      <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">XiaomiBracelet</span> <span style="color: black;">implements</span> <span style="color: black;">Bracelet</span>
      </span>{
      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">look</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">echo</span> <span style="color: black;">"查看小米手环"</span>;
      }

      <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">click</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">echo</span> <span style="color: black;">"点击小米手环"</span>;
      }
      }

      <span style="color: black;">abstract</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Factory</span>
      </span>{
      <span style="color: black;">abstract</span> <span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">createPhone</span><span style="color: black;">()</span></span>;

      <span style="color: black;">abstract</span> <span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">createBracelet</span><span style="color: black;">()</span></span>;
      }

      <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">ProductFactory</span> <span style="color: black;">extends</span> <span style="color: black;">Factory</span></span>{
      <span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">createPhone</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">return</span> <span style="color: black;">new</span> HuaweiPhone();
      }

      <span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">createBracelet</span><span style="color: black;">()</span>
      </span>{
      <span style="color: black;">return</span> <span style="color: black;">new</span>XiaomiBracelet();
      }
      }

      $newPhone = ProductFactory::createPhone();
      $newPhone-&gt;open();
      $newPhone-&gt;operate();

      $newBracelet = ProductFactory::createBracelet();
      $newBracelet-&gt;look();
      $newBracelet-&gt;click();</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上<span style="color: black;">便是</span>我对工厂模式的<span style="color: black;">有些</span>学习见解,<span style="color: black;">期盼</span>对部分同行人在理解上有<span style="color: black;">必定</span>的<span style="color: black;">帮忙</span>,感谢你们的关注!</span></p>




j8typz 发表于 2024-10-18 20:29:31

你的见解独到,让我受益匪浅,期待更多交流。

7wu1wm0 发表于 2024-10-30 03:47:36

期待与你深入交流,共探知识的无穷魅力。
页: [1]
查看完整版本: PHP 平常设计模式——工厂模式