php design mode FlyWeight of enjoy mode

  • 2020-05-09 18:16:23
  • OfStack

Enjoy mode is called "Flyweight Pattern" in English. I am very grateful to the strong man who translated Flyweight Pattern into enjoy mode, because this word clearly expresses the way this mode is used. If translated into feather-order mode or flyorder mode, etc., it can implicitly show the purpose of using this mode, but it still fails to grasp the key of this mode.

The enjoy pattern is defined as using one share to avoid the overhead of having a large number of the same content objects. The most common and intuitive of these costs is memory loss. The enjoy pattern effectively supports a large number of fine-grained objects in a Shared way.

The core concept of sharing is embodied in both the name and the definition, so how to realize sharing? We should know that every thing is different, but there is a definite commonality. If only the same thing can be Shared, then the mode of enjoying yuan is not feasible. Therefore, we should try our best to share the commonality of things, while retaining its individuality. To do this, there is a distinction between intrinsic and extrinsic states in the enjoy mode. The intrinsic state is the generality, and the extrinsic state is the individuality.

Note: Shared objects must be immutable, or 1 change will be completely changed (except for this requirement).

The inherent state is stored in the enjoyment element, which is not different with the change of the environment, and can be Shared. The externality state is not Shared, it changes as the environment changes, so the externality state is maintained by the client (because the change in the environment is caused by the client). In each specific environment, the client passes the externalized state to the browser to create a different object.

Take a look at the following procedures, a general understanding of the next enjoy mode.

 
<?php 
/** 
*  The flyweight pattern  
* 
*  Use the enjoy technique to effectively support a large number of fine-grained objects  
*/ 
class CD 
{ 
private $_title = null; 
private $_artist = null; 
public function setTitle($title) 
{ 
$this->_title = $title; 
} 
public function getTitle() 
{ 
return $this->_title; 
} 
public function setArtist($artist) 
{ 
$this->_artist = $artist; 
} 
public function getArtist($artist) 
{ 
return $this->_artist; 
} 
} 
class Artist 
{ 
private $_name; 
public function __construct($name) 
{ 
echo "construct ".$name."<br/>"; 
$this->_name = $name; 
} 
public function getName() 
{ 
return $this->_name; 
} 
} 
class ArtistFactory 
{ 
private $_artists = array(); 
public function getArtist($name) 
{ 
if(isset($this->_artists[$name])) 
{ 
return $this->_artists[$name]; 
} else { 
$objArtist = new Artist($name); 
$this->_artists[$name] = $objArtist; 
return $objArtist; 
} 
} 
} 
$objArtistFactory = new ArtistFactory(); 
$objCD1 = new CD(); 
$objCD1->setTitle("title1"); 
$objCD1->setArtist($objArtistFactory->getArtist('artist1')); 
$objCD2 = new CD(); 
$objCD2->setTitle("title2"); 
$objCD2->setArtist($objArtistFactory->getArtist('artist2')); 
$objCD3 = new CD(); 
$objCD3->setTitle("title3"); 
$objCD3->setArtist($objArtistFactory->getArtist('artist1')); 

Enjoy the essence of the mode has three points:

Objects used by the system of fine particle size, particle size should have more fine, about what amount the flyweight pattern, which is used to see jdk knew, jdk, Integer, Character, String etc. Use the flyweight pattern, they are the most basic data types, it is fine, they frequently involved in operation, not is not a lot. Categorize the intrinsic properties/states of objects and extrinsic properties/states; So-called intrinsic state, it is the objects of the internal, not as the state of environmental change, one netizen said is very good, is the condition of no difference, namely remove aggregates properties outside after 1 class object with no difference between intrinsic state is the object of the yuan god, as long as no more yuan yuan god, then the object is no difference, and there is no difference between yuan god only these can be Shared, I think this is why Flyweight been translated into the flyweight. The externalized state is the state specified by the client and will change with the environment. In the case of Integer, its intrinsic property is actually its value(it has no external property, of course); Control yuan creation with 1 factory; Because the enjoy object cannot be created by the client at will, otherwise it is meaningless. Factories usually provide a caching mechanism to hold the privileges that have been created.

Although object-oriented is a good solution to the problem of abstraction, but for a real running software system, we still need to consider the cost of object-oriented problem, enjoy the mode is to solve the cost of object-oriented problem. The sharing of objects is used to reduce the number of objects in the system so as to reduce the memory pressure brought by fine-grained objects.

The enjoy mode is not commonly used in 1-like project development, but is often applied to the development of the system bottom layer in order to solve the performance problems of the system. The String types in Java and.Net use the meta mode. If a string object s1 has been created in Java or.NET, the next time the same string s2 is created, the system simply points the reference to s2 to the specific object referenced by s1, thereby enabling the same string to be Shared in memory. If you create a new string object every time you perform the s1= "abc" operation, the memory overhead can be significant.


Related articles: