<?php/*** Created by PhpStorm.* User: insgeek* Date: 2017年2月10日* Time: 10:45* 抽象工厂模式*/namespace DesignPattern\AbstractFactoryPattern;interface Shape{public function draw();}class Rectangle implements Shape{public function draw(){// TODO: Implement draw() method.print_r("Inside Rectangle::draw() method.");}}class Square implements Shape{public function draw(){// TODO: Implement draw() method.print_r("Inside Square::draw() method.");}}class Circle implements Shape{public function draw(){// TODO: Implement draw() method.print_r("Inside Circle::draw() method.");}}interface Color{public function fill();}class Red implements Color{public function fill(){// TODO: Implement fill() method.print_r("Inside Red::fill() method.");}}class Green implements Color{public function fill(){// TODO: Implement fill() method.print_r("Inside Green::fill() method.");}}class Blue implements Color{public function fill(){// TODO: Implement fill() method.print_r("Inside Blue::fill() method.");}}abstract class AbstractFactory{abstract public function getColor(string $color);abstract public function getShape(string $shape);}class ShapeFactory extends AbstractFactory{public function getColor(string $color){// TODO: Implement getColor() method.return null;}public function getShape(string $shape){// TODO: Implement getShape() method.if ($shape == null) {return null;}$type = strtoupper($shape);if ($type == "CIRCLE") {return new Circle();} else if ($type == "RECTANGLE") {return new Rectangle();} else if ($type == "SQUARE") {return new Square();}return null;}}class ColorFactory extends AbstractFactory{public function getShape(string $shape){// TODO: Implement getShape() method.return null;}public function getColor(string $color){// TODO: Implement getColor() method.if ($color == null) {return null;}$type = strtoupper($color);if ($type == "RED") {return new Red();} else if ($type == "BLUE") {return new Blue();} else if ($type == "GREEN") {return new Green();}return null;}}class FactoryProducer{public static function getFactory(string $factory){$type = strtoupper($factory);if ($type == "SHAPE") {return new ShapeFactory();} else if ($type == "COLOR") {return new ColorFactory();}return null;}}$shapeFactory = FactoryProducer::getFactory('shape');$shapeFactory->getShape("CIRCLE")->draw();echo PHP_EOL;$colorFactory = FactoryProducer::getFactory('color');$colorFactory->getColor("blue")->fill();
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。