├── Creational ├── Builder │ └── Builder.php ├── FactoryMethod │ └── FactoryMethod.php └── Singleton │ └── Singleton.php ├── LICENSE └── README.md /Creational/Builder/Builder.php: -------------------------------------------------------------------------------- 1 | walls) . " walls, " . count($this->doors) . " doors, " . count($this->windows) . " windows, and a " . ($this->roof ? "$this->roof roof" : "no roof") . ".\n"; 41 | } 42 | } 43 | 44 | /** 45 | * The Builder interface declares the methods necessary to build each part of the product. 46 | * These methods will be implemented by concrete builder classes. 47 | */ 48 | interface HouseBuilder 49 | { 50 | public function buildWalls(): void; 51 | public function buildDoors(): void; 52 | public function buildWindows(): void; 53 | public function buildRoof(): void; 54 | public function getHouse(): House; 55 | } 56 | 57 | /** 58 | * Concrete Builder for building a Wooden House. 59 | * This class implements the HouseBuilder interface and defines specific steps to 60 | * construct a house with wooden components. 61 | */ 62 | class WoodenHouseBuilder implements HouseBuilder 63 | { 64 | private House $house; 65 | 66 | /** 67 | * Initialize a new House object. 68 | */ 69 | public function __construct() 70 | { 71 | $this->house = new House(); 72 | } 73 | 74 | /** 75 | * Build wooden walls for the house. 76 | */ 77 | public function buildWalls(): void 78 | { 79 | $this->house->walls = array_fill(0, 4, 'wooden wall'); 80 | } 81 | 82 | /** 83 | * Build wooden doors for the house. 84 | */ 85 | public function buildDoors(): void 86 | { 87 | $this->house->doors = array_fill(0, 2, 'wooden door'); 88 | } 89 | 90 | /** 91 | * Build windows with wooden frames for the house. 92 | */ 93 | public function buildWindows(): void 94 | { 95 | $this->house->windows = array_fill(0, 4, 'wooden-framed window'); 96 | } 97 | 98 | /** 99 | * Add a wooden roof to the house. 100 | */ 101 | public function buildRoof(): void 102 | { 103 | $this->house->roof = 'wooden'; 104 | } 105 | 106 | /** 107 | * Return the fully constructed house object. 108 | * 109 | * @return House The constructed house. 110 | */ 111 | public function getHouse(): House 112 | { 113 | return $this->house; 114 | } 115 | } 116 | 117 | /** 118 | * Concrete Builder for building a Brick House. 119 | * This class implements the HouseBuilder interface and defines specific steps to 120 | * construct a house with brick and metal components. 121 | */ 122 | class BrickHouseBuilder implements HouseBuilder 123 | { 124 | private House $house; 125 | 126 | /** 127 | * Initialize a new House object. 128 | */ 129 | public function __construct() 130 | { 131 | $this->house = new House(); 132 | } 133 | 134 | /** 135 | * Build brick walls for the house. 136 | */ 137 | public function buildWalls(): void 138 | { 139 | $this->house->walls = array_fill(0, 4, 'brick wall'); 140 | } 141 | 142 | /** 143 | * Build a metal door for the house. 144 | */ 145 | public function buildDoors(): void 146 | { 147 | $this->house->doors = array_fill(0, 1, 'metal door'); 148 | } 149 | 150 | /** 151 | * Build windows with brick frames for the house. 152 | */ 153 | public function buildWindows(): void 154 | { 155 | $this->house->windows = array_fill(0, 6, 'brick-framed window'); 156 | } 157 | 158 | /** 159 | * Add a metal roof to the house. 160 | */ 161 | public function buildRoof(): void 162 | { 163 | $this->house->roof = 'metal'; 164 | } 165 | 166 | /** 167 | * Return the fully constructed house object. 168 | * 169 | * @return House The constructed house. 170 | */ 171 | public function getHouse(): House 172 | { 173 | return $this->house; 174 | } 175 | } 176 | 177 | /** 178 | * The Director class is responsible for controlling the building process. 179 | * It accepts a builder object and calls its methods in a specific sequence 180 | * to construct the complex object. 181 | */ 182 | class HouseDirector 183 | { 184 | private HouseBuilder $builder; 185 | 186 | /** 187 | * Set the builder object that will be used for constructing the house. 188 | * 189 | * @param HouseBuilder $builder The builder responsible for construction. 190 | */ 191 | public function setBuilder(HouseBuilder $builder): void 192 | { 193 | $this->builder = $builder; 194 | } 195 | 196 | /** 197 | * Direct the construction process by calling each build step in sequence. 198 | * 199 | * @return House The completed house. 200 | */ 201 | public function constructHouse(): House 202 | { 203 | $this->builder->buildWalls(); 204 | $this->builder->buildDoors(); 205 | $this->builder->buildWindows(); 206 | $this->builder->buildRoof(); 207 | return $this->builder->getHouse(); 208 | } 209 | } 210 | 211 | // Client Code 212 | // Demonstrates the usage of the Builder pattern to construct different types of houses. 213 | 214 | $director = new HouseDirector(); 215 | 216 | // Building a wooden house 217 | $woodenHouseBuilder = new WoodenHouseBuilder(); 218 | $director->setBuilder($woodenHouseBuilder); 219 | $woodenHouse = $director->constructHouse(); 220 | $woodenHouse->show(); // Outputs: House with 4 walls, 2 doors, 4 windows, and a wooden roof. 221 | 222 | // Building a brick house 223 | $brickHouseBuilder = new BrickHouseBuilder(); 224 | $director->setBuilder($brickHouseBuilder); 225 | $brickHouse = $director->constructHouse(); 226 | $brickHouse->show(); // Outputs: House with 4 walls, 1 door, 6 windows, and a metal roof. 227 | -------------------------------------------------------------------------------- /Creational/FactoryMethod/FactoryMethod.php: -------------------------------------------------------------------------------- 1 | otherAction(); 114 | } 115 | 116 | // Example usage of the Factory Method Design Pattern. 117 | // Create an instance of ConcreteCreator and pass it to the creator function. 118 | // Print the result of the otherAction method called on the ConcreteCreator instance. 119 | print_r(creator(new ConcreteCreator())); 120 | -------------------------------------------------------------------------------- /Creational/Singleton/Singleton.php: -------------------------------------------------------------------------------- 1 |