06 May 2018

TODO 基础信息 [2/2]

TODO 创建型 [9/9]

  • [X]

    简单工厂模式(Simple Factory)

    class Foo
    {
    
    }
    
    class Bar
    {
    
    }
    
    class SimpleFactory
    {
        public function createFoo()
        {
            return new Foo();
        }
    
        public function createBar()
        {
            return new Bar();
        }
    }
    
    $factory = new SimpleFactory();
    $foo = $factory->createFoo();
    $bar = $factory->createBar();
    
    
  • [X]

    抽象工厂模式(Abstract Factory)

    class Text
    {
    
    }
    
    class Json
    {
    
    }
    
    abstract class AbstractFactory
    {
        abstract public function createObj();
    }
    
    class TextFactory extends AbstractFactory
    {
        public function createObj()
        {
            return new Text();
        }
    }
    
    class JsonFactory extends AbstractFactory
    {
        public function createObj()
        {
            return new Json();
        }
    }
    
    // test
    $factory = new AbstractFactory();
    $json = $factory->createJsonObj();
    $text = $factory->createTextObj();
    
  • [X]

    静态工厂模式(Static Factory)

    class Text
    {
    
    }
    
    class Json
    {
    
    }
    
    class StaticFactory
    {
        public static function createTextObj()
        {
            return new Text();
        }
    
        public static function createJsonObj()
        {
            return new Json();
        }
    }
    
    // test
    $json = StaticFactory::createJsonObj();
    $text = StaticFactory::createTextObj();
    
  • [X]

    建造者模式(Builder)
    好处在于隐藏 new 一个类的具体细节

    interface BuilderInterface
    {
        public function createObj();
        public function buildObj();
        public function getObj();
    }
    
    class Builder
    {
        public function build(BuilderInterface $builder)
        {
            $builder->createObj();
            $builder->buildObj();
            return $builder->getObj();
        }
    }
    
    
    class Car
    {
    
    }
    
    class CarBuilder implement BuilderInterface
    {
        protected $car;
    
        public function createObj()
        {
            $this->car = new Car();
        }
    
        public function buildObj()
        {
            $this->car = xxx();
            $this->car = xxx();
        }
    
        public function getObj()
        {
            return $this->car;
        }
    }
    
  • [X]

    工厂方法模式(Factory Method)

    class Text
    {
    
    }
    
    class Json
    {
    
    }
    
    class AbstractFactory
    {
        public function create($type)
        {
            if ($type == 'text') {
                return new Text();
            } else if ($type == 'json') {
                return new Json();
            }
        }
    }
    
    // test
    $factory = new AbstractFactory();
    $json = $factory->create("json");
    $text = $factory->create("text");
    
  • [X]

    单例模式(Singleton)

    class Service
    {
        private $instance;
    
        private function __construct()
        {
            // ...
        }
    
        private function __clone()
        {
            // ...
        }
        private function __wakeup()
        {
            // ...
        }
    
        public static getInstance($tyep)
        {
            if ($instances === null) {
                self::$instance = new self();
            }
    
            return self::$instance;
        }
    }
    
  • [X]

    多例模式(Multiton)

    class Service
    {
        private $instances = [];
    
        private function __construct()
        {
            // ...
        }
    
        private function __clone()
        {
            // ...
        }
    
        private function __wakeup()
        {
            // ...
        }
    
        public static getInstance($tyep)
        {
            if (!isset(self::$instances[$type])) {
                self::$instances[$type] = new self();
            }
    
            return self::$instances[$type];
        }
    }
    
  • [X]

    对象池模式(Pool)

    class SomethingWorker
    {
        public function __construct()
        {
            // ...
        }
    
        public function run()
        {
            // ...
            // ...
        }
    }
    
    class WorkerPool
    {
        private $occupiedWorkers = [];
        private $freeWorkers = [];
    
        public function get()
        {
            if (count($this->freeWorkers) == 0) {
                $worker = new SomethingWorker();
            } else {
                $worker = array_pop($this->freeWorkers);
            }
    
            $this->occupiedWorkers[spl_object_hash($worker)] = $worker;
    
            return $worker;
        }
    
        public function dispose($worker)
        {
            $key = spl_object_hash($worker);
            if (isset($this->occupiedWorkers[$key])) {
                unset($this->occupiedWorkers[$key]);
                $this->freeWorkers[$key] = $worker;
            }
        }
    
        public function count()
        {
            return count($this->occupiedWorkers) + count($this->freeWorkers);
        }
    }
    
  • [X]

    原型模式(Prototype)

    相比正常创建一个对象 (new Foo()) ,首先创建一个原型,然后克隆它会更节省开销。

    class Obj
    {
    
    }
    # a 和 b 指向同一块内存空间
    $a = new Obj();
    $b = $a;
    
    # a 和 b 指向不同的内存空间,直接调用 clone 不会请求 __construct 方法
    $a = new Obj();
    $b = clone $a;
    
    abstract class BookPrototype
    {
        protected $title;
        protected $category;
    
        abstract public function __clone();
    
        public function getTitle()
        {
            return $this->title;
        }
    
        public function setTitle($title)
        {
            $this->title = $title;
        }
    }
    
    class BarBookPrototype extends BookPrototype
    {
        protected $category = 'Bar';
    
        public function __clone()
        {
    
        }
    }
    
    class FooBookPrototype extends BookPrototype
    {
        protected $category = 'Foo';
    
        public function __clone()
        {
    
        }
    }
    
    // test
    $fooPrototype = new FooBookPrototype();
    $barPrototype = new BarBookPrototype();
    
    for ($i = 0; $i < 10; $i++) {
        $book = clone $fooPrototype;
        $book->setTitle('Foo Book No ' . $i);
    }
    
    for ($i = 0; $i < 5; $i++) {
        $book = clone $barPrototype;
        $book->setTitle('Bar Book No ' . $i);
    }
    

TODO 结构型 [2/11]

  • [X]

    适配器模式(Adapter)
    将一个类的接口转换成可应用的兼容接口。适配器使原本由于接口不兼容而不能一起工作的那些类可以一起工作。

    下面的 Kindle 对象 EBookInterface 接口本来是不支持 BookInterface 的,通过 EBookAdapter 就支持了 BookInterface

    interface BookInterface
    {
        public function turnPage();
        public function open();
        public function getPage(): int;
    }
    
    interface EBookInterface
    {
        public function unlock();
        public function pressNext();
        public function getPage(): array;
    }
    
    
    class Book implements BookInterface
    {
        private $page;
    
        public function open()
        {
            $this->page = 1;
        }
    
        public function turnPage()
        {
            $this->page++;
        }
    
        public function getPage()
        {
            return $this->page;
        }
    }
    
    
    class Kindle implements EBookInterface
    {
        private $page = 1;
        private $totalPages = 100;
    
        public function pressNext()
        {
            $this->page++;
        }
    
        public function unlock()
        {
            // ...
        }
    
        public function getPage(): array
        {
            return [$this->page, $this->totalPages];
        }
    }
    
    class EBookAdapter implements BookInterface
    {
        protected $eBook;
    
        public function __construct(EBookInterface $eBook)
        {
            $this->eBook = $eBook;
        }
    
        public function open()
        {
            $this->eBook->unlock();
        }
    
        public function turnPage()
        {
            $this->eBook->pressNext();
        }
    
        public function getPage()
        {
            return $this->eBook->getPage()[0];
        }
    }
    
    // test
    // $book = new EBookAdapter($kindle);
    
  • [ ] 桥梁模式(Bridge)