Home  >  Article  >  Backend Development  >  Implementation method and introduction of PHP prototype mode (with code)

Implementation method and introduction of PHP prototype mode (with code)

不言
不言forward
2019-02-18 14:29:182387browse

This article brings you the implementation method and introduction of PHP prototype mode (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Use prototype instances to specify the types of objects to be created, and create new objects by copying these prototypes

The emergence of the parallel inheritance hierarchy is brought about by the factory method pattern a question. This is a coupling that makes some programmers uncomfortable. Every time you add a product family, you are forced to create a related specific creator. In a rapidly growing system that includes more and more products, maintaining these relationships can quickly become tiresome.
The way to avoid this dependence is to use PHP's clone keyword to copy an existing specific product. Then, the specific product categories themselves become the basis for their own generation. This is prototype mode. Using this pattern we can use composition instead of inheritance. Such a shift promotes code runtime flexibility and reduces the number of classes that must be created.

Suppose there is a "Civilization" style online game where combat units can be operated in a grid composed of blocks. Each block represents an ocean, a plain, and a forest. The type of terrain restricts the fighting ability of the unit occupying the block. We could have an terrainFactory object that provides Sea, Forest and Plains objects. We decided to allow the user to choose between completely different environments, so Sea could be an abstract parent class for Marssea and Earthsea. Forest and P1ains objects will have similar implementations. The branches here constitute the abstract factory pattern. We have completely different product systems (Sea, Plains, Forests), and these product families have close connections beyond inheritance, such as Earth and Mars, which both have oceans, forests, and plains terrains. The class diagram shown in Figure 9-10 shows how to apply the Abstract Factory and Factory Method patterns to these products.

As you can see, we rely on inheritance to combine the terrain (terrain) family products generated by the factory. This is indeed a feasible solution, but it requires a large inheritance system and is relatively unwieldy. So flexible. When you don't want a parallel integration system but need to maximize runtime flexibility, you can use a powerful variant of the abstract factory pattern - the prototype pattern

<?php

//海洋
class Sea{
	//可导航性
	private $navigability=0;
	function __construct($navigability){
		$this->navigability=$navigability;
	}
}
//地球海洋
class EarthSea extends Sea{}

//火星海洋
class MarsSea extends Sea{}
//平原
class Plains{}
//地球平原
class EarthPlains extends Plains{}

//火星平原
class MarsPlains extends Plains{}

//森岭
class Forest{}
//地球森林
class EarthForest extends Forest{}

//火星森林
class MarsForest extends Forest{}

//地形工厂
class TerrainFactory{
	private $sea;
	private $forest;
	private $plains;
	
	function __construct(Sea $sea,Plains $plains,Forest $forest){
		$this->sea=$sea;
		$this->plains=$plains;
		$this->forest=$forest;
	}
	
	function getSea(){
		return clone $this->sea;
	}
	
	function getPlains(){
		return clone $this->plains;
	}
	
	function getForest(){
		return clone $this->forest;
	}
}

class Contained{}
class Container{
	public $contained;
	
	function __construct(){
		$this->contained=new Contained();
	}
	function __clone(){
		//确保被克隆的对象持有的是self::$contained的克隆而不是引用
		$this->contained=clone $this->contained;
	}
}
$factory=new TerrainFactory(new EarthSea(-1), new EarthPlains(), new EarthForest());

print_r($factory->getSea());

print "<hr>";

print_r($factory->getPlains());

print "<hr>";

print_r($factory->getForest());

Output results:

EarthSea Object ([navigability:Sea:private] => -1 )


EarthPlains Object ( )


EarthForest Object ( )

The above is the detailed content of Implementation method and introduction of PHP prototype mode (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete