标签:简单工厂模式 而且 extend 依赖 imp The ima 简单工厂 工厂模式
一、工厂方法模式
工厂方法模式。定义为:Define an interface for creating an object,but let subclasses decide which class toinstantiate.Factory Method lets a class defer instantiation to subclasses.(定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。)
工厂模式通用类图
<?php interface zoon { function introduce(); } class dog implements zoon{ function introduce(){ echo "我是小狗,我汪汪汪叫" . PHP_EOL; } } class pig implements zoon{ function introduce(){ echo "我是小猪,我哼哼哼叫" . PHP_EOL; } } class sheep implements zoon{ function introduce(){ echo "我是小羊,我咩咩咩叫" . PHP_EOL; } } interface createZoon{ function create(); } class FactoryDod implements createZoon{ function create(){ return new dog(); } } class FactoryPig implements createZoon{ function create(){ return new pig(); } } class FactorySheep implements createZoon{ function create(){ return new sheep(); } } class Factory{ function __construct(){ $d = new FactoryDod(); $d->create()->introduce(); $p = new FactoryPig(); $d->create()->introduce(); $s = new Factorysheep(); $s->create()->introduce(); } } $t = new Factory();
二、简单工厂模式
<?php interface zoon { function introduce(); } class dog implements zoon{ function introduce(){ echo "我是小狗,我汪汪汪叫" . PHP_EOL; } } class pig implements zoon{ function introduce(){ echo "我是小猪,我哼哼哼叫" . PHP_EOL; } } class sheep implements zoon{ function introduce(){ echo "我是小羊,我咩咩咩叫" . PHP_EOL; } } class FactoryDod { static function create(){ return new dog(); } } class FactoryPig{ static function create(){ return new pig(); } } class FactorySheep{ static function create(){ return new sheep(); } } class Factory{ function __construct(){ $d = FactoryDod::create(); $d->introduce(); $p = FactoryPig::create(); $d->introduce(); $s = Factorysheep::create(); $s->introduce(); } } $t = new Factory();
三、抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)定义:Provide an interface for creating families of related or dependent objects without specifyingtheir concrete classes.(为创建一组相关或相互依赖的对象提供一个接口,而且无须指定它们的具体类。)
<?php interface Human { function getColor(); function talk(); function getSex(); } abstract class abstractWhiteHuman implements Human{ public function getColor(){ echo "皮肤颜色:白色" . PHP_EOL; } public function talk(){ echo "语言:白人语言" . PHP_EOL; } } abstract class abstractBlackHuman implements Human{ public function getColor(){ echo "皮肤颜色:黑色" . PHP_EOL; } public function talk(){ echo "语言:黑人语言" . PHP_EOL; } } abstract class abstractYellowHuman implements Human{ public function getColor(){ echo "皮肤颜色:黄色" . PHP_EOL; } public function talk(){ echo "语言:黄人语言" . PHP_EOL; } } class FemaleYellowHuman extends abstractYellowHuman{ function getSex(){ echo "性别:女性" . PHP_EOL; } } class MaleYellowHuman extends abstractYellowHuman{ function getSex(){ echo "性别:男性" . PHP_EOL; } } class MaleBlackHuman extends abstractYellowHuman{ public function getSex(){ echo "性别:男性" . PHP_EOL; } } class MaleWhiteHuman extends abstractYellowHuman{ public function getSex(){ echo "性别:男性" . PHP_EOL; } } interface HumanFactory{ function createYellowHuman(); function createWhiteHuman(); function createBlackHuman(); } class FemaleFactory implements HumanFactory{ function createBlackHuman(){ return new FemaleBlackHuman(); } function createWhiteHuman(){ return new FemaleWhiteHuman(); } function createYellowHuman(){ return new FemaleYellowHuman(); } } class MaleFactory implements HumanFactory{ function createBlackHuman(){ return new MaleBlackHuman(); } function createWhiteHuman(){ return new MaleWhiteHuman(); } function createYellowHuman(){ return new MaleYellowHuman(); } } class main{ public function create(){ $maleHumanFactory = new MaleFactory(); $femaleHumanFactory = new FemaleFactory(); $maleYellowHuman = $maleHumanFactory->createYellowHuman(); $maleYellowHuman->getColor(); $maleYellowHuman->talk(); $maleYellowHuman->getSex(); } } $t = new main(); $t->create();
标签:简单工厂模式 而且 extend 依赖 imp The ima 简单工厂 工厂模式
原文地址:https://www.cnblogs.com/onlycat/p/9164529.html