php作品设计(PHP设计模式(创建型))

wufei123 发布于 2023-11-03 阅读(839)

php零基础入门视频课程

前言随着编程项目经验的增加,从服务于业务逻辑到针对项目的全局设计认识到设计模式在开发过程中 \的重要性,遵循 S.O.L.I.D 五大基准原则它拓展了我的视野,让代码更加灵活,教程看起来更加富有美感.\美是构建万物的哲学思想.。

php作品设计(PHP设计模式(创建型))

我们学习的设计模式分为三类:创建者模式、结构型模式、行为型模式;创建型模式与对象的创建有关;结构型模式处理类或对象的组合;而行为型模式是对类或对象怎样交教程互和怎样分配职责进行描述;内容:本文介绍的是 PHP 设计模式的创建型一篇。

包括:单例模式(Singleton), 多例模式(Multiton), 工厂方法模式(Factory Method), 抽象教程工厂模式(Abstract Factory), 简单工厂模式(Simple Factory), 原型模式(Prototype), 对象池模式(Pool), 建造者模式(Builder)。

推荐:《PHP教程教程》(一)单例模式(Singleton)● 定义保证一个类只有一个实例,并且提供一个访问它的全局访问点系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建销毁的对象,使用单例模式可以提教程高系统性能。

● 代码示例class Singleton{ /** * @var Singleton */ private static $instance; /** * 不允许从外部调用以防止创建多个教程实例 * 要使用单例,必须通过 Singleton::getInstance() 方法获取实例

*/ private function __construct() { } /** * 通过懒加载获得实例教程(在第一次使用的时候创建) */ public static function getInstance(): Singleton

{ if (null === static::$instance) { 教程static::$instance = new static(); } return static::$instance; } /**

* 防止实例被克隆(这会创建实例的副本) */ private f教程unction __clone() { } /** * 防止反序列化(这将创建它的副本) */ private function __wakeup()

{ }}(二)多例模式(Multiton)● 定义教程在多例模式中,多例类可以有多个实例,而且多例类必须自己创建、管理自己的实例,并向外界提供自己的实例1. 通过实例容器保存容器2. 利用私有构造阻止外部构造。

3. 提供getInstantce()方法获教程取实例.● 代码示例 两个对象通过一个类进行多次实例化abstract class Multiton { private static $instances = array();

public stat教程ic function getInstance() { $key = get_called_class() . serialize(func_get_args()); if (!isset(self:教程:$instances[$key])) {

$rc = new ReflectionClass(get_called_class()); self::$instances[$key] = $rc->ne教程wInstanceArgs(func_get_args());

} return self::$instances[$key]; } /** * 该私有对象阻止实例被克隆 */ private func教程tion __clone() { } /** * 该私有方法阻止实例被序列化

*/ private function __wakeup() { }} class Hello extends Multit教程on { public function __construct($string = World) {

echo "Hello $string\n"; } } class GoodBye extends教程 Multiton { public function __construct($string = my, $string2 = darling) {

echo "Goodbye $string $st教程ring2\n"; }}$a = Hello::getInstance(World); $b = Hello::getInstance(bob);

// $a !== $b $c = Hello::ge教程tInstance(World); // $a === $c $d = GoodBye::getInstance(); $e = GoodBye::getInstance();

// $d === $e教程 $f = GoodBye::getInstance(your); // $d !== $f(三)工厂方法模式(Factory Method)● 定义将类的实例化(具体产品的创建)延迟到工厂类的子类(教程具体工厂)中完成,即由子类来决定应该实例化(创建)哪一个类

● 代码示例 : 小成有一间塑料加工厂(仅生产 A 类产品);随着客户需求的变化,客户需要生产 B 类产品改变原有塑料加工厂的配置和变化非常困教程难,假设下一次客户需要再发生变化,再次改变将增大非常大的成本;小成决定置办塑料分厂 B 来生产 B 类产品。

abstract class Product{ public abstract functi教程on Show();}//具体产品A类class ProductA extends Product{ public function Show() {

echo "生产出了产品A"; }}//具体产品B教程类class ProductB extends Product{ public function Show() { echo "生产出了产品B"; }

}abstract class Factory{ 教程public abstract function Manufacture();}//工厂A类 - 生产A类产品class FactoryA extends Factory{

public functio教程n Manufacture() { return new ProductA(); }}//工厂B类 - 生产B类产品class FactoryB extends Factory{

public func教程tion Manufacture() { return new ProductB(); }}(四)抽象工厂模式(Abstract Factory)● 定义在不指定具体类的情况下创建一系列相关或依赖对象教程

通常创建的类都实现相同的接口 抽象工厂的客户并不关心这些对象是如何创建的,它只是知道它们是如何一起运行的● 代码示例 : 有两个工厂,A 工厂负责运输,B 工厂生产数码产品.interface Pr教程oduct

{ public function calculatePrice(): int;}class ShippableProduct implements Product{ /** * @var 教程float */

private $productPrice; /** * @var float */ private $shippingCosts; public function __constru教程ct(int $productPrice, int $shippingCosts)

{ $this->productPrice = $productPrice; $this->shippingCosts教程 = $shippingCosts; } public function calculatePrice(): int

{ return $this->productPrice + $this->ship教程pingCosts; }}class DigitalProduct implements Product{ /** * @var int

*/ private $price; public functi教程on __construct(int $price) { $this->price = $price; } public function calculatePrice(): int

{ return 教程$this->price; }}class ProductFactory{ const SHIPPING_COSTS = 50; public function createShippableProd教程uct(int $price): Product

{ return new ShippableProduct($price, self::SHIPPING_COSTS); } public functi教程on createDigitalProduct(int $price): Product

{ return new DigitalProduct($price); }}(五)简单工厂模式(Simple 教程Factory)● 定义简单工厂模式是一个精简版的工厂模式工厂角色-具体产品-抽象产品● 代码示例 :

一个农场,要向市场销售水果农场里有三种水果 苹果、葡萄,我们设想:1、水果有多种属性,每个属性都有教程不同,但是,他们有共同的地方 | 生长、种植、收货、吃将来有可能会增加新的水果、我们需要定义一个接口来规范他们必须实现的方法.。

interface fruit{ /** * 生长 */ public 教程function grow(); /** * 种植 */ public function plant(); /** * 收获 */

public function harvest(); /** * 吃 教程*/ public function eat();}class apple implements fruit{ //苹果树有年龄 private $treeAge;

//苹果有颜色 private $c教程olor; public function grow(){ echo "grape grow"; } public function plant(){ echo "grape plant";

} pub教程lic function harvest(){ echo "grape harvest"; } public function eat(){ echo "grape eat"; } //取苹果树的年龄教程

public function getTreeAge(){ return $this->treeAge; } //设置苹果树的年龄 public function setTreeAge($age){ 教程$this->treeAge = $age;

return true; }}class grape implements fruit{ //葡萄是否有籽 private $seedLess; publi教程c function grow(){ echo "apple grow";

} public function plant(){ echo "apple plant"; } public functio教程n harvest(){ echo "apple harvest"; } public function eat(){

echo "apple eat"; } //有无籽取值 public functi教程on getSeedLess(){ return $this->seedLess; } //设置有籽无籽 public function setSeedLess($seed){

$this->seedL教程ess = $seed; return true; }}class farmer{ //定义个静态工厂方法 public static function factory($fruitName){

swi教程tch ($fruitName) { case apple: return new apple(); break; case grape: return new grape(); break; def教程ault:

throw new badFruitException("Error no the fruit", 1); break; } }}class badFruitException extend教程s Exception

{ public $msg; public $errType; public function __construct($msg = , $errType = 1){ $this教程->msg = $msg;

$this->errType = $errType; } }/** * 获取水果实例化的方法 */try{ $appleInstance = farmer::factory(教程apple); var_dump($appleInstance);

}catch(badFruitException $err){ echo $err->msg . "_______" . $err->教程errType;}(六)原型模式(Prototype)● 定义相比正常创建一个对象 (new Foo () ),首先创建一个原型,然后克隆它会更节省开销。

● 代码示例 : 为每一本书设置标题abstr教程act class BookPrototype{ /** * @var string */ protected $title = 0; /** * @var string

*/ protected $c教程ategory; abstract public function __clone(); public function getTitle(): string { return $this->titl教程e;

} public function setTitle($title) { $this->title = $title; }}class BarBookPrototype extends BookP教程rototype

{ /** * @var string */ protected $category = Bar; public function __clone() { }}class FooBoo教程kPrototype extends BookPrototype

{ /** * @var string */ protected $category = Foo; public function __教程clone() { }}$fooPrototype = new FooBookPrototype();

$barPrototype = new BarBookPrototype();for ($i = 教程5; $i setTitle(Foo Book No . $i);

var_dump(new FooBookPrototype == $book);}for ($i = 0; $i setTitle(B教程ar Book No . $i);

var_dump(new BarBookPrototype == $book);}(七)对象池模式(Pool)● 定义对象池可以用于构造并且存放一系列的对象并在需要时教程获取调用在初始化实例成本高,实例化率高,可用实例不足的情况下,对象池可以极大地提升性能。

在创建对象(尤其是通过网络)时间花销不确定的情况下,通过对象池在短期时间内就可以获得所需的对象● 代码示例cla教程ss Factory { protected static $products = array();

public static function pushProduct(Product $produc教程t) { self::$products[$product->getId()] = $product;

} public static function getProduct($id) { return教程 isset(self::$products[$id]) ? self::$products[$id] : null;

} public static function removeProduct($i教程d) { if (array_key_exists($id, self::$products)) { unset(self::$products[$id]);

} }}Factory::pushProd教程uct(new Product(first));Factory::pushProduct(new Product(second));print_r(Factory::getProduct(first)教程->getId());

// firstprint_r(Factory::getProduct(second)->getId());// second(八)建造者模式(Builder)● 定义将一个复杂教程对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示

● 2)代码示例 建造相同标准的卡车和汽车类似于变形金刚,相同的零件进行不同的组合.● 分为 Director 导演者,负责构建、Buil教程derInterface 构建接口,规范建造标准、TruckBuilder 构建卡车类 CarBuilder 构建汽车类。

Vehicle 零部件公共类、Truck Car Engine Wheel D教程oor 零部件类、DirectorTest 测试类class Director{ public function build(BuilderInterface $builder): Vehicle

{ 教程$builder->createVehicle(); $builder->addDoors(); $builder->addEngine(); $builder->addWheel(); return教程 $builder->getVehicle();

}}interface BuilderInterface{ public function createVehicle(); public functi教程on addWheel(); public function addEngine();

public function addDoors(); public function getVehicle():教程 Vehicle;}class TruckBuilder implements BuilderInterface

{ /** * @var Truck */ private $truck; public教程 function addDoors() { $this->truck->setPart(rightDoor, new Door());

$this->truck->setPart(leftDoor, 教程new Door()); } public function addEngine() { $this->truck->setPart(truckEngine, new Engine());

} publ教程ic function addWheel() { $this->truck->setPart(wheel1, new Wheel()); $this->truck->setPart(wheel2, n教程ew Wheel());

$this->truck->setPart(wheel3, new Wheel()); $this->truck->setPart(wheel4, new Wheel()); 教程$this->truck->setPart(wheel5, new Wheel());

$this->truck->setPart(wheel6, new Wheel()); } public func教程tion createVehicle() { $this->truck = new Truck();

} public function getVehicle(): Vehicle { return $教程this->truck; }}class CarBuilder implements BuilderInterface

{ /** * @var Car */ private $car; public 教程function addDoors() { $this->car->setPart(rightDoor, new Door());

$this->car->setPart(leftDoor, new D教程oor()); $this->car->setPart(trunkLid, new Door()); } public function addEngine()

{ $this->car->setPar教程t(engine, new Engine()); } public function addWheel() { $this->car->setPart(wheelLF, new Wheel());

$t教程his->car->setPart(wheelRF, new Wheel()); $this->car->setPart(wheelLR, new Wheel()); $this->car->setP教程art(wheelRR, new Wheel());

} public function createVehicle() { $this->car = new Car(); } public funct教程ion getVehicle(): Vehicle {

return $this->car; }}abstract class Vehicle{ /** * @var object[] */ priva教程te $data = []; /** * @param string $key

* @param object $value */ public function setPart($key, $valu教程e) { $this->data[$key] = $value; }}class Truck extends Vehicle

{}class Car extends Vehicle{}class Eng教程ine extends Vehicle{}class Wheel extends Vehicle{}class Door extends Vehicle

{}class DirectorTest{ pu教程blic function testCanBuildTruck() { $truckBuilder = new TruckBuilder(); return (new Director())->bui教程ld($truckBuilder);

} public function testCanBuildCar() { $carBuilder = new CarBuilder(); return (new 教程Director())->build($carBuilder);

}}$directorTest = new DirectorTest();var_dump($directorTest->testCan教程BuildTruck());var_dump($directorTest->testCanBuildCar());

以上就是PHP设计模式(创建型)的详细内容,更多请关注其它相关文章!更多技巧请《转发 教程+ 关注》哦!

亲爱的读者们,感谢您花时间阅读本文。如果您对本文有任何疑问或建议,请随时联系我。我非常乐意与您交流。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

河南中青旅行社综合资讯 奇遇综合资讯 盛世蓟州综合资讯 综合资讯 游戏百科综合资讯 新闻51700