├── _config.yml ├── .gitignore ├── Composite ├── README.md ├── Safe │ ├── safe.png │ ├── File.php │ ├── Component.php │ ├── Dir.php │ └── Client.php └── Transparent │ ├── Transparent.png │ ├── File.php │ ├── Dir.php │ ├── Component.php │ └── Client.php ├── Proxy ├── Proxy.png ├── Subject.php ├── RealSubject.php ├── Client.php └── Proxy.php ├── State ├── State.png ├── State.php ├── Client.php ├── ConcreteStateBusy.php ├── ConcreteStateOnline.php ├── Context.php └── QQStateExample.php ├── Bridge ├── Bridge.png ├── Color.php ├── RedColor.php ├── GreenColor.php ├── YellowColor.php ├── Circle.php ├── Square.php ├── Triangle.php ├── Shape.php └── Client.php ├── Facade ├── Facade.png ├── People.php ├── Client.php ├── Package.php └── Facade.php ├── swoolee-logo.png ├── Builder ├── Builder.png ├── Director.php ├── Builder.php ├── CarBuilder.php ├── Client.php └── Car.php ├── Command ├── Command.png ├── CommandInterface.php ├── Receiver.php ├── Invoker.php ├── Client.php └── Command.php ├── Decorator ├── Decorator.png ├── Drinks.php ├── Tea.php ├── MilkTea.php ├── BlackTea.php ├── Decorator.php └── Client.php ├── Flyweight ├── Flyweight.png ├── ConcreteFlyweight.php ├── UnsharedConcreteFlyweight.php ├── Flyweight.php ├── FlyweightFactory.php └── Client.php ├── Prototype ├── Prototype.png ├── Prototype.php ├── Keyboard.php ├── ShallowCopy.php ├── DeepCopy.php └── Client.php ├── Singleton ├── Singleton.png ├── Singleton.php ├── Test.php └── MySQLDB.php ├── Strategy ├── Strategy.png ├── SortStrategy.php ├── Context.php ├── Client.php ├── BubbleSortStrategy.php └── QuickSortStrategy.php ├── RegistryTree ├── RegistryTree.png ├── Demo.php ├── RegistryTree.php └── Demo2.php ├── TemplateMethod ├── TemplateMethod.png ├── ConcreteClass1.php ├── ConcreteClass2.php ├── Client.php └── AbstractClass.php ├── Adapter ├── README.md ├── ClassAdapter │ ├── ClassAdapter.png │ ├── DatabaseTarget.php │ ├── DatabaseAdaptee.php │ ├── DatabaseAdapter.php │ └── Client.php └── ObjectAdapter │ ├── ObjectAdapter.png │ ├── DatabaseTarget.php │ ├── DatabaseAdaptee.php │ ├── Client.php │ └── DatabaseAdapter.php ├── Factory ├── FactoryMethod │ ├── FactoryMethod.png │ ├── createPay.php │ ├── FactoryWeChat.php │ ├── FactoryAli.php │ ├── CreatePayFactoryMethod.php │ ├── AliPay.php │ ├── WeChatPay.php │ └── Client.php ├── SimpleFactory │ ├── SimpleFactory.png │ ├── PaySimpleFactory.php │ ├── AliPay.php │ ├── WeChatPay.php │ ├── Test.php │ ├── Pay.php │ ├── SimpleFactory.php │ └── StaticFactory.php ├── AbstractFactory │ ├── AbstractFactory.png │ ├── WoMan.php │ ├── Monkey.php │ ├── FemaleMonkey.php │ ├── HuMan.php │ ├── Man.php │ ├── ApeMonkey.php │ ├── Factory.php │ ├── WoManFactory.php │ ├── ManFactory.php │ └── Client.php ├── README.md └── OptimizeAbstractFactory │ ├── FactoryWithSimple.png │ ├── FactoryWithReflection.png │ ├── config.php │ ├── config1.php │ ├── WoMan.php │ ├── Monkey.php │ ├── HuMan.php │ ├── Man.php │ ├── WoManMonkey.php │ ├── ManMonkey.php │ ├── Client.php │ ├── Factory.php │ └── FactoryWithReflection.php ├── composer.json ├── LICENSE └── README.md /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | vendor/ 3 | *.DS_Store -------------------------------------------------------------------------------- /Composite/README.md: -------------------------------------------------------------------------------- 1 | ## 目录 2 | 3 | * [x] [透明模式](Transparent) 4 | * [x] [安全模式](Safe) 5 | -------------------------------------------------------------------------------- /Proxy/Proxy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Proxy/Proxy.png -------------------------------------------------------------------------------- /State/State.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/State/State.png -------------------------------------------------------------------------------- /Bridge/Bridge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Bridge/Bridge.png -------------------------------------------------------------------------------- /Facade/Facade.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Facade/Facade.png -------------------------------------------------------------------------------- /swoolee-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/swoolee-logo.png -------------------------------------------------------------------------------- /Builder/Builder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Builder/Builder.png -------------------------------------------------------------------------------- /Command/Command.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Command/Command.png -------------------------------------------------------------------------------- /Composite/Safe/safe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Composite/Safe/safe.png -------------------------------------------------------------------------------- /Decorator/Decorator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Decorator/Decorator.png -------------------------------------------------------------------------------- /Flyweight/Flyweight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Flyweight/Flyweight.png -------------------------------------------------------------------------------- /Prototype/Prototype.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Prototype/Prototype.png -------------------------------------------------------------------------------- /Singleton/Singleton.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Singleton/Singleton.png -------------------------------------------------------------------------------- /Strategy/Strategy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Strategy/Strategy.png -------------------------------------------------------------------------------- /RegistryTree/RegistryTree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/RegistryTree/RegistryTree.png -------------------------------------------------------------------------------- /TemplateMethod/TemplateMethod.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/TemplateMethod/TemplateMethod.png -------------------------------------------------------------------------------- /Adapter/README.md: -------------------------------------------------------------------------------- 1 | 类适配器和对象适配器解决的问题一样的,只不过是不同的实现方式。 2 | 3 | ## 目录 4 | 5 | * [类适配器](ClassAdapter) 6 | * [对象适配器](ObjectAdapter) 7 | -------------------------------------------------------------------------------- /Adapter/ClassAdapter/ClassAdapter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Adapter/ClassAdapter/ClassAdapter.png -------------------------------------------------------------------------------- /Composite/Transparent/Transparent.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Composite/Transparent/Transparent.png -------------------------------------------------------------------------------- /Adapter/ObjectAdapter/ObjectAdapter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Adapter/ObjectAdapter/ObjectAdapter.png -------------------------------------------------------------------------------- /Factory/FactoryMethod/FactoryMethod.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Factory/FactoryMethod/FactoryMethod.png -------------------------------------------------------------------------------- /Factory/SimpleFactory/SimpleFactory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Factory/SimpleFactory/SimpleFactory.png -------------------------------------------------------------------------------- /Factory/AbstractFactory/AbstractFactory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Factory/AbstractFactory/AbstractFactory.png -------------------------------------------------------------------------------- /Factory/README.md: -------------------------------------------------------------------------------- 1 | 根据抽象程度的不同,PHP工厂模式分为三种。 2 | 3 | ## 目录 4 | 5 | * [简单工厂](SimpleFactory) 6 | * [工厂方法](FactoryMethod) 7 | * [抽象工厂](AbstractFactory) 8 | -------------------------------------------------------------------------------- /Factory/OptimizeAbstractFactory/FactoryWithSimple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Factory/OptimizeAbstractFactory/FactoryWithSimple.png -------------------------------------------------------------------------------- /Factory/OptimizeAbstractFactory/FactoryWithReflection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/design-patterns/HEAD/Factory/OptimizeAbstractFactory/FactoryWithReflection.png -------------------------------------------------------------------------------- /Factory/FactoryMethod/createPay.php: -------------------------------------------------------------------------------- 1 | "WoMan" 13 | ]; 14 | -------------------------------------------------------------------------------- /Factory/OptimizeAbstractFactory/config1.php: -------------------------------------------------------------------------------- 1 | "WoMan" 13 | ]; 14 | -------------------------------------------------------------------------------- /Factory/AbstractFactory/WoMan.php: -------------------------------------------------------------------------------- 1 | 6 | * @date 2020/5/6 7 | * @copyright Swoole, Inc. 8 | * @package sy-records/design-patterns 9 | */ 10 | 11 | namespace Luffy\DesignPatterns\Strategy; 12 | 13 | interface SortStrategy 14 | { 15 | public function sort(array $data); 16 | } -------------------------------------------------------------------------------- /Command/CommandInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2020/1/7 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Command; 13 | 14 | interface CommandInterface 15 | { 16 | public function execute(); 17 | } 18 | -------------------------------------------------------------------------------- /Factory/FactoryMethod/FactoryWeChat.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/11/29 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Facade; 13 | 14 | class People 15 | { 16 | public function say() 17 | { 18 | echo "下单寄快递\n"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Proxy/Subject.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/16 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Proxy; 13 | 14 | interface Subject 15 | { 16 | public function get(); 17 | 18 | public function put(); 19 | } 20 | -------------------------------------------------------------------------------- /Prototype/Keyboard.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2020/1/7 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Command; 13 | 14 | class Receiver 15 | { 16 | public function action() 17 | { 18 | echo "执行操作"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Factory/SimpleFactory/PaySimpleFactory.php: -------------------------------------------------------------------------------- 1 | color->output()} 圆形\n"; 19 | } 20 | } -------------------------------------------------------------------------------- /Bridge/Square.php: -------------------------------------------------------------------------------- 1 | color->output()} 正方形\n"; 19 | } 20 | } -------------------------------------------------------------------------------- /Factory/AbstractFactory/Monkey.php: -------------------------------------------------------------------------------- 1 | color->output()} 三角形\n"; 19 | } 20 | } -------------------------------------------------------------------------------- /Factory/AbstractFactory/FemaleMonkey.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/24 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\TemplateMethod; 13 | 14 | class ConcreteClass1 extends AbstractClass 15 | { 16 | protected function startPlay() 17 | { 18 | echo "start play CF\n"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /TemplateMethod/ConcreteClass2.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/24 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\TemplateMethod; 13 | 14 | class ConcreteClass2 extends AbstractClass 15 | { 16 | protected function startPlay() 17 | { 18 | echo "start play LOL\n"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Factory/OptimizeAbstractFactory/Man.php: -------------------------------------------------------------------------------- 1 | driver}.\n"; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Adapter/ObjectAdapter/DatabaseAdaptee.php: -------------------------------------------------------------------------------- 1 | driver}.\n"; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Composite/Safe/File.php: -------------------------------------------------------------------------------- 1 | name . "\n"; 22 | } 23 | } -------------------------------------------------------------------------------- /Decorator/Tea.php: -------------------------------------------------------------------------------- 1 | pay(); 16 | 17 | $factory = new SimpleFactory(); 18 | $pay2 = $factory->pay("AliPay"); 19 | $pay2->pay(); -------------------------------------------------------------------------------- /Facade/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/11/29 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Facade; 13 | 14 | include __DIR__ . '/../vendor/autoload.php'; 15 | 16 | class Client 17 | { 18 | public static function run() 19 | { 20 | $facade = new Facade(); 21 | $facade->send(); 22 | } 23 | } 24 | Client::run(); 25 | -------------------------------------------------------------------------------- /Factory/SimpleFactory/Pay.php: -------------------------------------------------------------------------------- 1 | pay(); 29 | 30 | $ali = new AliPay(); 31 | $ali->pay(); 32 | 33 | -------------------------------------------------------------------------------- /Flyweight/ConcreteFlyweight.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/9 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Flyweight; 13 | 14 | class ConcreteFlyweight extends Flyweight 15 | { 16 | /** 17 | * @param $content 18 | */ 19 | public function get($content) 20 | { 21 | echo "共享的:{$content} {$this->name}\n"; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Factory/AbstractFactory/Factory.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/16 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Proxy; 13 | 14 | class RealSubject implements Subject 15 | { 16 | public function get() 17 | { 18 | echo __CLASS__ . " get\n"; 19 | } 20 | 21 | public function put() 22 | { 23 | echo __CLASS__ . " put\n"; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Decorator/MilkTea.php: -------------------------------------------------------------------------------- 1 | drinks->name(); 19 | } 20 | 21 | public function price() 22 | { 23 | return $this->drinks->price() + 8; 24 | } 25 | } -------------------------------------------------------------------------------- /Decorator/BlackTea.php: -------------------------------------------------------------------------------- 1 | drinks->name(); 19 | } 20 | 21 | public function price() 22 | { 23 | return $this->drinks->price() + 5; 24 | } 25 | } -------------------------------------------------------------------------------- /Proxy/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/16 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Proxy; 13 | 14 | include __DIR__ . '/../vendor/autoload.php'; 15 | 16 | class Client 17 | { 18 | public static function run() 19 | { 20 | $proxy = new Proxy(); 21 | $proxy->get(); 22 | $proxy->put(); 23 | $proxy->send(); 24 | } 25 | } 26 | Client::run(); -------------------------------------------------------------------------------- /Adapter/ClassAdapter/DatabaseAdapter.php: -------------------------------------------------------------------------------- 1 | driver = "pdo"; 19 | } 20 | 21 | public function get() 22 | { 23 | echo "used {$this->driver}.\n"; 24 | } 25 | } -------------------------------------------------------------------------------- /Factory/AbstractFactory/ManFactory.php: -------------------------------------------------------------------------------- 1 | 6 | * @date 2020/4/26 7 | * @copyright Swoole, Inc. 8 | * @package sy-records/design-patterns 9 | */ 10 | 11 | namespace Luffy\DesignPatterns\State; 12 | 13 | 14 | abstract class State 15 | { 16 | /** 17 | * @var Context 18 | */ 19 | protected $context; 20 | 21 | public function setContext(Context $context) 22 | { 23 | $this->context = $context; 24 | } 25 | 26 | abstract public function handle(): void; 27 | 28 | abstract public function handle1(): void; 29 | } -------------------------------------------------------------------------------- /State/Client.php: -------------------------------------------------------------------------------- 1 | 6 | * @date 2020/4/26 7 | * @copyright Swoole, Inc. 8 | * @package sy-records/design-patterns 9 | */ 10 | 11 | namespace Luffy\DesignPatterns\State; 12 | 13 | include __DIR__ . '/../vendor/autoload.php'; 14 | 15 | class Client 16 | { 17 | public static function run() 18 | { 19 | $context = new Context(new ConcreteStateOnline); 20 | $context->request(); 21 | echo "**********" . PHP_EOL; 22 | $context->request1(); 23 | $context->request(); 24 | } 25 | } 26 | Client::run(); -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sy-records/design-patterns", 3 | "description": "php design patterns", 4 | "keywords": ["swoole","php","design-patterns"], 5 | "type": "project", 6 | "homepage": "https://github.com/sy-records/design-patterns", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Luffy", 11 | "email": "52o@qq52o.cn", 12 | "homepage": "https://qq52o.me" 13 | } 14 | ], 15 | "autoload": { 16 | "psr-4": { 17 | "Luffy\\DesignPatterns\\": "" 18 | } 19 | }, 20 | "config": { 21 | "optimize-autoloader": true, 22 | "sort-packages": true 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Flyweight/UnsharedConcreteFlyweight.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/9 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Flyweight; 13 | 14 | class UnsharedConcreteFlyweight extends Flyweight 15 | { 16 | /** 17 | * @param $content 18 | */ 19 | public function get($content) 20 | { 21 | echo "不共享的:{$content} {$this->name}\n"; 22 | } 23 | 24 | public function del() 25 | { 26 | $this->name = ''; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Decorator/Decorator.php: -------------------------------------------------------------------------------- 1 | drinks = $drinks; 29 | } 30 | } -------------------------------------------------------------------------------- /Facade/Package.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/11/29 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Facade; 13 | 14 | class Package 15 | { 16 | public function pickup() 17 | { 18 | echo "上门取件\n"; 19 | } 20 | 21 | public function inspection() 22 | { 23 | echo "验视\n"; 24 | } 25 | 26 | public function bale() 27 | { 28 | echo "打包\n"; 29 | } 30 | 31 | public function sendOut() 32 | { 33 | echo "寄出\n"; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Prototype/ShallowCopy.php: -------------------------------------------------------------------------------- 1 | keyboard = $keyboard; 25 | } 26 | 27 | public function get() 28 | { 29 | echo '我想要个'.$this->keyboard->name. '的键盘' .PHP_EOL; 30 | } 31 | } -------------------------------------------------------------------------------- /TemplateMethod/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/24 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\TemplateMethod; 13 | 14 | require __DIR__ . "/../vendor/autoload.php"; 15 | 16 | class Client 17 | { 18 | public static function run() 19 | { 20 | $obj1 = new ConcreteClass1(); 21 | $obj1->templateMethod(); 22 | 23 | echo "===========\n"; 24 | 25 | $obj2 = new ConcreteClass2(); 26 | $obj2->templateMethod(); 27 | } 28 | } 29 | Client::run(); 30 | -------------------------------------------------------------------------------- /Adapter/ObjectAdapter/Client.php: -------------------------------------------------------------------------------- 1 | set(); 21 | 22 | // 适配器 23 | $adapter = new DatabaseAdapter($adaptee); 24 | $adapter->set(); 25 | 26 | $adapter->get(); 27 | } 28 | } 29 | 30 | Client::run(); -------------------------------------------------------------------------------- /Command/Invoker.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2020/1/7 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Command; 13 | 14 | class Invoker 15 | { 16 | /** 17 | * @var Command 18 | */ 19 | protected $command; 20 | 21 | /** 22 | * @param Command $command 23 | */ 24 | public function setCommand(Command $command) 25 | { 26 | $this->command = $command; 27 | } 28 | 29 | public function executeCommand() 30 | { 31 | $this->command->execute(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Adapter/ClassAdapter/Client.php: -------------------------------------------------------------------------------- 1 | set(); 22 | 23 | // 适配器 24 | $adapter = new DatabaseAdapter(); 25 | $adapter->set(); 26 | 27 | $adapter->get(); 28 | } 29 | } 30 | 31 | Client::run(); -------------------------------------------------------------------------------- /Bridge/Shape.php: -------------------------------------------------------------------------------- 1 | color = $color; 29 | } 30 | 31 | /** 32 | * @return mixed 33 | */ 34 | abstract public function run(); 35 | } -------------------------------------------------------------------------------- /Factory/FactoryMethod/Client.php: -------------------------------------------------------------------------------- 1 | create(); 22 | $ali->pay(); 23 | 24 | $factory = new FactoryWeChat(); 25 | $wechat = $factory->create(); 26 | $wechat->pay(); 27 | } 28 | } 29 | 30 | // 执行 31 | $demo = new Client; 32 | $demo->index(); 33 | -------------------------------------------------------------------------------- /Flyweight/Flyweight.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/9 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Flyweight; 13 | 14 | abstract class Flyweight 15 | { 16 | /** 17 | * @var 18 | */ 19 | protected $name; 20 | 21 | /** 22 | * Flyweight constructor. 23 | * @param $name 24 | */ 25 | public function __construct($name) 26 | { 27 | $this->name = $name; 28 | } 29 | 30 | /** 31 | * @param $content 32 | */ 33 | abstract public function get($content); 34 | } 35 | -------------------------------------------------------------------------------- /Command/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2020/1/7 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Command; 13 | 14 | include __DIR__ . '/../vendor/autoload.php'; 15 | 16 | class Client 17 | { 18 | public static function run() 19 | { 20 | // 创建接收者 21 | $receiver = new Receiver(); 22 | $command = new Command($receiver); 23 | 24 | // 命令接收者调用命令 25 | $invoker = new Invoker(); 26 | $invoker->setCommand($command); 27 | $invoker->executeCommand(); 28 | } 29 | } 30 | Client::run(); 31 | -------------------------------------------------------------------------------- /Composite/Safe/Component.php: -------------------------------------------------------------------------------- 1 | name = $name; 29 | } 30 | 31 | /** 32 | * @return mixed 33 | */ 34 | abstract public function getName(); 35 | } -------------------------------------------------------------------------------- /Strategy/Context.php: -------------------------------------------------------------------------------- 1 | 6 | * @date 2020/5/6 7 | * @copyright Swoole, Inc. 8 | * @package sy-records/design-patterns 9 | */ 10 | 11 | namespace Luffy\DesignPatterns\Strategy; 12 | 13 | class Context 14 | { 15 | private $strategy; 16 | 17 | public function __construct(SortStrategy $strategy) 18 | { 19 | $this->strategy = $strategy; 20 | } 21 | 22 | public function setStrategy(SortStrategy $strategy) 23 | { 24 | $this->strategy = $strategy; 25 | } 26 | 27 | public function sort(array $data) 28 | { 29 | return $this->strategy->sort($data); 30 | } 31 | } -------------------------------------------------------------------------------- /Composite/Transparent/File.php: -------------------------------------------------------------------------------- 1 | name . "\n"; 31 | } 32 | } -------------------------------------------------------------------------------- /State/ConcreteStateBusy.php: -------------------------------------------------------------------------------- 1 | 6 | * @date 2020/4/26 7 | * @copyright Swoole, Inc. 8 | * @package sy-records/design-patterns 9 | */ 10 | 11 | namespace Luffy\DesignPatterns\State; 12 | 13 | 14 | class ConcreteStateBusy extends State 15 | { 16 | public function handle(): void 17 | { 18 | echo "忙碌状态" . PHP_EOL; 19 | } 20 | 21 | public function handle1(): void 22 | { 23 | echo "忙碌状态" . PHP_EOL; 24 | echo __CLASS__ . ":处理 request1" . PHP_EOL; 25 | echo __CLASS__ . ":忙完了... 转回在线状态" . PHP_EOL; 26 | $this->context->transitionTo(new ConcreteStateOnline()); 27 | } 28 | } -------------------------------------------------------------------------------- /State/ConcreteStateOnline.php: -------------------------------------------------------------------------------- 1 | 6 | * @date 2020/4/26 7 | * @copyright Swoole, Inc. 8 | * @package sy-records/design-patterns 9 | */ 10 | 11 | namespace Luffy\DesignPatterns\State; 12 | 13 | 14 | class ConcreteStateOnline extends State 15 | { 16 | public function handle(): void 17 | { 18 | echo "正在线上" . PHP_EOL; 19 | echo __CLASS__ . ":处理 request" . PHP_EOL; 20 | echo __CLASS__ . ":有些事情,要去忙了..." . PHP_EOL; 21 | $this->context->transitionTo(new ConcreteStateBusy()); 22 | } 23 | 24 | public function handle1(): void 25 | { 26 | echo "正在线上" . PHP_EOL; 27 | } 28 | } -------------------------------------------------------------------------------- /Builder/Director.php: -------------------------------------------------------------------------------- 1 | builder = $builder; 21 | } 22 | 23 | public function build() 24 | { 25 | $this->builder->buildPartA(); 26 | $this->builder->buildPartB(); 27 | $this->builder->buildPartC(); 28 | $this->builder->buildOthers(); 29 | return $this->builder->getCar(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Command/Command.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2020/1/7 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Command; 13 | 14 | class Command implements CommandInterface 15 | { 16 | /** 17 | * @var Receiver 18 | */ 19 | protected $receiver; 20 | 21 | /** 22 | * Command constructor. 23 | * @param Receiver $receiver 24 | */ 25 | public function __construct(Receiver $receiver) 26 | { 27 | $this->receiver = $receiver; 28 | } 29 | 30 | public function execute() 31 | { 32 | $this->receiver->action(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Flyweight/FlyweightFactory.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/9 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Flyweight; 13 | 14 | class FlyweightFactory 15 | { 16 | /** 17 | * @var array 18 | */ 19 | private $flyweights = []; 20 | 21 | /** 22 | * @param $name 23 | * @return mixed 24 | */ 25 | public function getFlyweight($name) 26 | { 27 | if (!isset($this->flyweights[$name])) { 28 | $this->flyweights[$name] = new ConcreteFlyweight($name); 29 | } 30 | return $this->flyweights[$name]; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /TemplateMethod/AbstractClass.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/24 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\TemplateMethod; 13 | 14 | abstract class AbstractClass 15 | { 16 | final public function templateMethod() 17 | { 18 | $this->open(); 19 | $this->startPlay(); 20 | $this->endPlay(); 21 | } 22 | 23 | protected function open() 24 | { 25 | echo "open game\n"; 26 | } 27 | 28 | abstract protected function startPlay(); 29 | 30 | protected function endPlay() 31 | { 32 | echo "end game\n"; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Adapter/ObjectAdapter/DatabaseAdapter.php: -------------------------------------------------------------------------------- 1 | adaptee = $adaptee; 21 | $adaptee->driver = "mysqli"; 22 | } 23 | 24 | public function set() 25 | { 26 | echo "use {$this->adaptee->driver}.\n"; 27 | } 28 | 29 | public function get() 30 | { 31 | echo "used {$this->adaptee->driver}.\n"; 32 | } 33 | } -------------------------------------------------------------------------------- /Factory/SimpleFactory/SimpleFactory.php: -------------------------------------------------------------------------------- 1 | name(); 21 | echo $tea->price()."元\n"; 22 | 23 | $blackTea = new BlackTea($tea); 24 | echo $blackTea->name(); 25 | echo $blackTea->price()."元\n"; 26 | 27 | $milkTea = new MilkTea($tea); 28 | echo $milkTea->name(); 29 | echo $milkTea->price()."元\n"; 30 | } 31 | } 32 | 33 | Client::run(); 34 | -------------------------------------------------------------------------------- /Prototype/DeepCopy.php: -------------------------------------------------------------------------------- 1 | keyboard = $keyboard; 27 | } 28 | 29 | public function get() 30 | { 31 | echo '我想要个'.$this->keyboard->name. '的键盘' .PHP_EOL; 32 | } 33 | 34 | public function __clone() 35 | { 36 | $this->keyboard = clone $this->keyboard; 37 | } 38 | } -------------------------------------------------------------------------------- /Builder/Builder.php: -------------------------------------------------------------------------------- 1 | 6 | * @date 2020/5/6 7 | * @copyright Swoole, Inc. 8 | * @package sy-records/design-patterns 9 | */ 10 | 11 | namespace Luffy\DesignPatterns\Strategy; 12 | 13 | include __DIR__ . '/../vendor/autoload.php'; 14 | 15 | class Client 16 | { 17 | public static function run() 18 | { 19 | $data = [13, 5, 49, 34, 25, 89]; 20 | $context = new Context(new BubbleSortStrategy); 21 | var_dump($context->sort($data)); 22 | $context->setStrategy(new QuickSortStrategy); 23 | var_dump($context->sort($data)); 24 | 25 | echo "**********\r\n"; 26 | $context = new Context(new QuickSortStrategy); 27 | var_dump($context->sort($data)); 28 | } 29 | } 30 | 31 | Client::run(); -------------------------------------------------------------------------------- /Composite/Safe/Dir.php: -------------------------------------------------------------------------------- 1 | children[] = $component; 27 | } 28 | 29 | public function getName() 30 | { 31 | $nameStr = $this->name ."\n"; 32 | foreach ($this->children as $k => $v) { 33 | $nameStr .= "--" . $v->getName(); 34 | } 35 | return $nameStr; 36 | } 37 | } -------------------------------------------------------------------------------- /Composite/Transparent/Dir.php: -------------------------------------------------------------------------------- 1 | children[] = $component; 27 | } 28 | 29 | public function getName() 30 | { 31 | $nameStr = $this->name ."\n"; 32 | foreach ($this->children as $k => $v) { 33 | $nameStr .= "--" . $v->getName(); 34 | } 35 | return $nameStr; 36 | } 37 | } -------------------------------------------------------------------------------- /RegistryTree/Demo.php: -------------------------------------------------------------------------------- 1 | index(); 27 | 28 | // 添加进对象树中 29 | RegistryTree::set("demo", $demo); 30 | 31 | // 获取对象 32 | $demo2 = RegistryTree::get("demo"); 33 | var_dump($demo2); 34 | $demo2->index(); 35 | 36 | // 删除对象 37 | RegistryTree::_unset("demo"); 38 | // 再次获取 39 | $demo_obj = RegistryTree::get("demo"); 40 | var_dump($demo_obj); 41 | $demo_obj->index(); -------------------------------------------------------------------------------- /Composite/Transparent/Component.php: -------------------------------------------------------------------------------- 1 | name = $name; 29 | } 30 | 31 | /** 32 | * @param Component $component 33 | * @return mixed 34 | */ 35 | abstract public function add(Component $component); 36 | 37 | /** 38 | * @return mixed 39 | */ 40 | abstract public function getName(); 41 | } -------------------------------------------------------------------------------- /Factory/AbstractFactory/Client.php: -------------------------------------------------------------------------------- 1 | createHuMan(); 19 | $man->say(); 20 | 21 | $monkey = $factory->createMonkey(); 22 | $monkey->say(); 23 | 24 | echo "*****************\n"; 25 | 26 | $factory = new WoManFactory(); 27 | $woman = $factory->createHuMan(); 28 | $woman->say(); 29 | 30 | $woman_monkey = $factory->createMonkey(); 31 | $woman_monkey->say(); 32 | 33 | } 34 | } 35 | 36 | // 执行 37 | $demo = new Client; 38 | $demo->index(); 39 | -------------------------------------------------------------------------------- /Builder/CarBuilder.php: -------------------------------------------------------------------------------- 1 | car = new Car(); 21 | } 22 | 23 | public function buildPartA() 24 | { 25 | $this->car->setPartA(); 26 | } 27 | 28 | public function buildPartB() 29 | { 30 | $this->car->setPartB(); 31 | } 32 | 33 | public function buildPartC() 34 | { 35 | $this->car->setPartC(); 36 | } 37 | 38 | public function buildOthers() 39 | { 40 | $this->car->setOthers(); 41 | } 42 | 43 | public function getCar() 44 | { 45 | return $this->car; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Proxy/Proxy.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/16 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Proxy; 13 | 14 | class Proxy 15 | { 16 | /** 17 | * @var RealSubject 18 | */ 19 | protected $realSubject; 20 | 21 | /** 22 | * Proxy constructor. 23 | */ 24 | public function __construct() 25 | { 26 | $this->realSubject = new RealSubject(); 27 | } 28 | 29 | public function __call($name, $arguments) 30 | { 31 | return $this->realSubject->{$name}(...$arguments); 32 | } 33 | 34 | // public function get() 35 | // { 36 | // $this->realSubject->get(); 37 | // } 38 | 39 | public function send() 40 | { 41 | echo __CLASS__ . " send\n"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Singleton/Singleton.php: -------------------------------------------------------------------------------- 1 | 6 | * @date 2020/5/6 7 | * @copyright Swoole, Inc. 8 | * @package sy-records/design-patterns 9 | */ 10 | 11 | namespace Luffy\DesignPatterns\Strategy; 12 | 13 | class BubbleSortStrategy implements SortStrategy 14 | { 15 | public function __construct() 16 | { 17 | echo __CLASS__ . PHP_EOL; 18 | } 19 | 20 | public function sort(array $data) 21 | { 22 | 23 | $count = count($data); 24 | for ($i = 1; $i < $count; $i++) { 25 | for ($j = 0; $j < $count - $i; $j++) { 26 | if ($data[$j] > $data[$j + 1]) { 27 | $temp = $data[$j]; 28 | $data[$j] = $data[$j + 1]; 29 | $data[$j + 1] = $temp; 30 | } 31 | } 32 | } 33 | return $data; 34 | } 35 | } -------------------------------------------------------------------------------- /Builder/Client.php: -------------------------------------------------------------------------------- 1 | build(); 24 | $newCar->getCar(); 25 | } 26 | 27 | public static function test() 28 | { 29 | $car = new Car(); 30 | $car->setPartA() 31 | ->setPartB() 32 | ->setPartC() 33 | ->setOthers() 34 | ->getCar(); 35 | } 36 | } 37 | 38 | // 使用建造者 39 | Client::index(); 40 | 41 | // 流式操作 42 | Client::test(); -------------------------------------------------------------------------------- /Facade/Facade.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/11/29 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Facade; 13 | 14 | class Facade 15 | { 16 | /** 17 | * @var People 18 | */ 19 | private $people; 20 | 21 | /** 22 | * @var Package 23 | */ 24 | private $package; 25 | 26 | /** 27 | * Facade constructor. 28 | */ 29 | public function __construct() 30 | { 31 | $this->people = new People(); 32 | $this->package = new Package(); 33 | } 34 | 35 | /** 36 | * 寄快递 37 | */ 38 | public function send() 39 | { 40 | $this->people->say(); 41 | $this->package->pickup(); 42 | $this->package->inspection(); 43 | $this->package->bale(); 44 | $this->package->sendOut(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Flyweight/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * @date 2019/12/9 8 | * @copyright Swoole, Inc. 9 | * @package sy-records/design-patterns 10 | */ 11 | 12 | namespace Luffy\DesignPatterns\Flyweight; 13 | 14 | include __DIR__ . '/../vendor/autoload.php'; 15 | 16 | class Client 17 | { 18 | public static function run() 19 | { 20 | $flyweight = new FlyweightFactory(); 21 | $obj1 = $flyweight->getFlyweight("⭐️"); 22 | $obj1->get('第1个'); 23 | 24 | $obj2 = $flyweight->getFlyweight("⭐️"); 25 | $obj2->get('第50个'); 26 | 27 | var_dump($obj1 === $obj2); 28 | 29 | $obj3 = $flyweight->getFlyweight("🍎"); 30 | $obj3->get('第1个'); 31 | 32 | // 不共享的 33 | $obj4 = new UnsharedConcreteFlyweight("🍍"); 34 | $obj4->get("一"); 35 | $obj4->del(); 36 | $obj4->get("一"); 37 | } 38 | } 39 | Client::run(); 40 | -------------------------------------------------------------------------------- /Composite/Safe/Client.php: -------------------------------------------------------------------------------- 1 | add($safe); 23 | 24 | $componentFile = new File("Component.php"); 25 | $dirFile = new File("Dir.php"); 26 | $fileFile = new File("File.php"); 27 | $clientFile = new File("Client.php"); 28 | 29 | $safe->add($componentFile); 30 | $safe->add($dirFile); 31 | $safe->add($fileFile); 32 | $safe->add($clientFile); 33 | 34 | echo $composite->getName(); 35 | } 36 | } 37 | 38 | Client::run(); -------------------------------------------------------------------------------- /RegistryTree/RegistryTree.php: -------------------------------------------------------------------------------- 1 | createHuMan(); 20 | $human->say(); 21 | 22 | $monkey = $factory->createMonkey(); 23 | $monkey->say(); 24 | 25 | echo "----- 使用反射优化 ----- \n"; 26 | $factory = new FactoryWithReflection(); 27 | $human = $factory->createHuMan(); 28 | $human->say(); 29 | 30 | $monkey = $factory->createMonkey(); 31 | $monkey->say(); 32 | } 33 | } 34 | 35 | // 执行 36 | $demo = new Client; 37 | $demo->index(); 38 | -------------------------------------------------------------------------------- /Strategy/QuickSortStrategy.php: -------------------------------------------------------------------------------- 1 | 6 | * @date 2020/5/6 7 | * @copyright Swoole, Inc. 8 | * @package sy-records/design-patterns 9 | */ 10 | 11 | namespace Luffy\DesignPatterns\Strategy; 12 | 13 | class QuickSortStrategy implements SortStrategy 14 | { 15 | public function __construct() 16 | { 17 | echo __CLASS__ . PHP_EOL; 18 | } 19 | 20 | public function sort(array $data) 21 | { 22 | $count = count($data); 23 | if ($count <= 1) { 24 | return $data; 25 | } 26 | $index = $data[0]; 27 | $left = []; 28 | $right = []; 29 | for ($i = 1; $i < $count; $i++) { 30 | if ($data[$i] < $index) { 31 | $left[] = $data[$i]; 32 | } else { 33 | $right[] = $data[$i]; 34 | } 35 | } 36 | $left = self::sort($left); 37 | $right = self::sort($right); 38 | return array_merge($left, [$data[0]], $right); 39 | } 40 | } -------------------------------------------------------------------------------- /State/Context.php: -------------------------------------------------------------------------------- 1 | 6 | * @date 2020/4/26 7 | * @copyright Swoole, Inc. 8 | * @package sy-records/design-patterns 9 | */ 10 | 11 | namespace Luffy\DesignPatterns\State; 12 | 13 | 14 | class Context 15 | { 16 | /** 17 | * @var State 18 | */ 19 | private $state; 20 | 21 | /** 22 | * Context constructor. 23 | * @param State $state 24 | */ 25 | public function __construct(State $state) 26 | { 27 | $this->transitionTo($state); 28 | } 29 | 30 | /** 31 | * 允许在运行时更改State对象 32 | */ 33 | public function transitionTo(State $state): void 34 | { 35 | echo __CLASS__ . ":Transition to " . get_class($state) . PHP_EOL; 36 | $this->state = $state; 37 | $this->state->setContext($this); 38 | } 39 | 40 | public function request(): void 41 | { 42 | $this->state->handle(); 43 | } 44 | 45 | public function request1(): void 46 | { 47 | $this->state->handle1(); 48 | } 49 | } -------------------------------------------------------------------------------- /Builder/Car.php: -------------------------------------------------------------------------------- 1 | _parts[] = "发动机、"; 24 | return $this; 25 | } 26 | 27 | public function setPartB() 28 | { 29 | $this->_parts[] = "底盘、"; 30 | return $this; 31 | } 32 | 33 | public function setPartC() 34 | { 35 | $this->_parts[] = "变速箱、"; 36 | return $this; 37 | } 38 | 39 | public function setOthers() 40 | { 41 | $this->_parts[] ="其他零件"; 42 | return $this; 43 | } 44 | 45 | public function getCar() 46 | { 47 | $str = "这辆车由:"; 48 | foreach ($this->_parts as $item) { 49 | $str .= $item; 50 | } 51 | $str .= "组成\n"; 52 | echo $str; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Composite/Transparent/Client.php: -------------------------------------------------------------------------------- 1 | add($transparent); 23 | 24 | $componentFile = new File("Component.php"); 25 | $dirFile = new File("Dir.php"); 26 | $fileFile = new File("File.php"); 27 | $clientFile = new File("Client.php"); 28 | 29 | $transparent->add($componentFile); 30 | $transparent->add($dirFile); 31 | $transparent->add($fileFile); 32 | $transparent->add($clientFile); 33 | 34 | $testFile = new File("test.php"); 35 | $clientFile->add($testFile); 36 | 37 | echo $composite->getName(); 38 | } 39 | } 40 | 41 | Client::run(); -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Luffy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /State/QQStateExample.php: -------------------------------------------------------------------------------- 1 | 6 | * @date 2020/4/26 7 | * @copyright Swoole, Inc. 8 | * @package sy-records/design-patterns 9 | */ 10 | 11 | namespace Luffy\DesignPatterns\State; 12 | 13 | class QQStateExample 14 | { 15 | public static function switch($state = "") 16 | { 17 | switch ($state) { 18 | case "Online": 19 | echo "正在线上"; 20 | break; 21 | case "Busy": 22 | echo "忙碌状态"; 23 | break; 24 | case "Stealth": 25 | echo "隐身状态"; 26 | break; 27 | default: 28 | echo "不在线"; 29 | } 30 | } 31 | 32 | public static function if($state = "") 33 | { 34 | if ($state == "Online") { 35 | echo "正在线上"; 36 | } elseif ($state == "Busy") { 37 | echo "忙碌状态"; 38 | } elseif ($state == "Stealth") { 39 | echo "隐身状态"; 40 | } else { 41 | echo "不在线"; 42 | } 43 | } 44 | } 45 | 46 | QQStateExample::switch("Busy"); 47 | echo PHP_EOL; 48 | QQStateExample::if("Stealth"); -------------------------------------------------------------------------------- /Factory/OptimizeAbstractFactory/Factory.php: -------------------------------------------------------------------------------- 1 | create = $config['create']; 22 | } 23 | 24 | /** 25 | * @return mixed 26 | */ 27 | public function createHuMan() 28 | { 29 | switch ($this->create) { 30 | case 'Man': 31 | $human = new Man(); 32 | break; 33 | case 'WoMan': 34 | $human = new WoMan(); 35 | break; 36 | default: 37 | throw new \InvalidArgumentException("你是个什么 👿 人类?\n"); 38 | } 39 | return $human; 40 | } 41 | 42 | /** 43 | * @return mixed 44 | */ 45 | public function createMonkey() 46 | { 47 | switch ($this->create) { 48 | case 'Man': 49 | $monkey = new ManMonkey(); 50 | break; 51 | case 'WoMan': 52 | $monkey = new WoManMonkey(); 53 | break; 54 | default: 55 | throw new \InvalidArgumentException("你是个什么 👿 猴子?\n"); 56 | } 57 | return $monkey; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Bridge/Client.php: -------------------------------------------------------------------------------- 1 | run(); 26 | 27 | // 黄色的正方形 28 | $yellowSquare = new Square($yellow); 29 | $yellowSquare->run(); 30 | 31 | // 绿色的正方形 32 | $greenSquare = new Square($green); 33 | $greenSquare->run(); 34 | 35 | // 红色的三角形 36 | $redTriangle = new Triangle($red); 37 | $redTriangle->run(); 38 | 39 | // 黄色的三角形 40 | $yellowTriangle = new Triangle($yellow); 41 | $yellowTriangle->run(); 42 | 43 | // 绿色的三角形 44 | $greenTriangle = new Triangle($green); 45 | $greenTriangle->run(); 46 | 47 | // 红色的圆形 48 | $redCircle = new Circle($red); 49 | $redCircle->run(); 50 | 51 | // 黄色的圆形 52 | $yellowCircle = new Circle($yellow); 53 | $yellowCircle->run(); 54 | 55 | // 绿色的圆形 56 | $greenCircle = new Circle($green); 57 | $greenCircle->run(); 58 | } 59 | } 60 | 61 | $client = new Client(); 62 | $client->run(); -------------------------------------------------------------------------------- /Singleton/Test.php: -------------------------------------------------------------------------------- 1 | content = rand(1, 9999); 43 | } 44 | 45 | private function __clone() 46 | { 47 | } 48 | 49 | private function __wakeup() 50 | { 51 | // TODO: Implement __wakeup() method. 52 | } 53 | 54 | public function test() 55 | { 56 | return $this->content; 57 | } 58 | } 59 | 60 | /** 61 | * 工厂 62 | * Class Factory 63 | */ 64 | class Factory 65 | { 66 | public static function create() 67 | { 68 | // 返回对象 69 | return SingletonDemo::getInstance(); 70 | } 71 | } 72 | 73 | RegistryTree::set("singleton", Factory::create()); 74 | 75 | $obj = RegistryTree::get("singleton"); 76 | var_dump($obj); 77 | echo $obj->test(); 78 | -------------------------------------------------------------------------------- /Factory/OptimizeAbstractFactory/FactoryWithReflection.php: -------------------------------------------------------------------------------- 1 | create = $config['create']; 27 | } 28 | 29 | /** 30 | * @return mixed 31 | */ 32 | public function createHuMan() 33 | { 34 | $className = __NAMESPACE__ .'\\'. $this->create; 35 | try { 36 | $class = new ReflectionClass($className); 37 | $human = $class->newInstance(); 38 | } catch (ReflectionException $Exception) { 39 | throw new \InvalidArgumentException($Exception->getMessage()); 40 | } 41 | return $human; 42 | } 43 | 44 | /** 45 | * @return mixed 46 | */ 47 | public function createMonkey() 48 | { 49 | $className = __NAMESPACE__ .'\\'. $this->create . "Monkey"; 50 | try { 51 | $class = new ReflectionClass($className); 52 | $monkey = $class->newInstance(); 53 | } catch (ReflectionException $Exception) { 54 | throw new \InvalidArgumentException($Exception->getMessage()); 55 | } 56 | return $monkey; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 |
6 | Swoole微课程-PHP设计模式 7 |

8 |

9 | 10 | 11 | 12 | 13 |

14 | 15 | ## 下载 16 | 17 | ```bash 18 | composer create-project sy-records/design-patterns:dev-master 19 | ``` 20 | 21 | ## 目录 22 | 23 | * [x] [单例模式](Singleton) 24 | * [x] [工厂模式](Factory) 25 | * [x] [简单工厂/静态工厂](Factory/SimpleFactory) 26 | * [x] [工厂方法](Factory/FactoryMethod) 27 | * [x] [抽象工厂](Factory/AbstractFactory) 28 | * [x] [优化抽象工厂](Factory/OptimizeAbstractFactory) 29 | * [x] [注册树模式](RegistryTree) 30 | * [x] [建造者模式](Builder) 31 | * [x] [原型模式](Prototype) 32 | * [x] [适配器模式](Adapter) 33 | * [x] [类适配器模式](Adapter/ClassAdapter) 34 | * [x] [对象适配器模式](Adapter/ObjectAdapter) 35 | * [x] [桥接模式](Bridge) 36 | * [x] [装饰器模式](Decorator) 37 | * [x] [组合模式](Composite) 38 | * [x] [透明模式](Composite/Transparent) 39 | * [x] [安全模式](Composite/Safe) 40 | * [x] [外观模式](Facade) 41 | * [x] [享元模式](Flyweight) 42 | * [x] [代理模式](Proxy) 43 | * [x] [模板方法模式](TemplateMethod) 44 | * [x] [命令模式](Command) 45 | * [x] [状态模式](State) 46 | * [x] [策略模式](Strategy) 47 | * [ ] 迭代器模式 48 | * [ ] 责任链模式 49 | * [ ] 观察者模式 50 | -------------------------------------------------------------------------------- /Singleton/MySQLDB.php: -------------------------------------------------------------------------------- 1 | name = "ikbc"; 24 | $shallowCopy->setKeyboard($keyboard); 25 | $shallowCopy->get(); 26 | 27 | echo "-----\n"; 28 | // clone不会调用构造函数 29 | $cloneShallow = clone $shallowCopy; 30 | $cloneShallow->get(); 31 | } 32 | 33 | public static function shallowCopy2() 34 | { 35 | $shallowCopy = new ShallowCopy(); 36 | $keyboard = new Keyboard(); 37 | $keyboard->name = "ikbc"; 38 | $shallowCopy->setKeyboard($keyboard); 39 | $shallowCopy->get(); 40 | 41 | echo "-----\n"; 42 | // clone不会调用构造函数 43 | $cloneShallow = clone $shallowCopy; 44 | $cloneShallow->get(); 45 | 46 | echo "=====\n"; 47 | $keyboard->name = "hhkb"; 48 | $shallowCopy->get(); 49 | $cloneShallow->get(); 50 | } 51 | 52 | public static function deepCopy() 53 | { 54 | $keyboard = new Keyboard(); 55 | $keyboard->name = "ikbc"; 56 | 57 | $deepCopy = new DeepCopy(); 58 | $deepCopy->setKeyboard($keyboard); 59 | $deepCopy->get(); 60 | 61 | echo "-----\n"; 62 | $cloneDeep = clone $deepCopy; 63 | $cloneDeep->get(); 64 | 65 | echo "=====\n"; 66 | $keyboard->name = "hhkb"; 67 | $deepCopy->get(); 68 | 69 | $cloneDeep->get(); 70 | } 71 | } 72 | Client::shallowCopy(); 73 | echo "***************".PHP_EOL; 74 | Client::shallowCopy2(); 75 | echo "***************".PHP_EOL; 76 | Client::deepCopy(); 77 | --------------------------------------------------------------------------------