PHP design pattern – meta pattern

Time:2022-5-9

 

definition:

Flyweight mode: use sharing technology to effectively support a large number of fine-grained objects.

 

Structure:

  • Flyweight: shared meta abstract class, the interface of all specific shared meta classes. Through this interface, flyweight can accept and act on the external state.
  • ConcreteFlyweight: a specific shared meta class that implements the flyweight interface.
  • UnsharedConcreteFlyweight: non shared concrete meta class.
  • FlyweightFactory: enjoy meta factory is used to create and manage flyweight objects. It is mainly used to ensure reasonable sharing of flyweight. When a user requests a flyweight, the flyweightfactory object provides a created instance or creates one (if it does not exist).
  • Client: client code.

 

Code example:

/**
 *Abstract class
 * Class Flyweight
 */
abstract class Flyweight
{
    protected $name;
 
    public function __construct($name)
    {
        $this->name = $name;
    }
 
 
    abstract public function show($content);
}
 
 
/**
 *Shared concrete classes
 * Class ConcreteFlyweight
 */
class ConcreteFlyweight extends Flyweight
{
 
    /**
     * @param $content
     */
    public function show($content)
    {
        // TODO: Implement show() method.
        Echo "shared sharing element: {$this - > name} {$content}";
    }
 
}
 
 
/**
 *Concrete classes that are not shared
 * Class UnsharedConcreteFlyweight
 */
class UnsharedConcreteFlyweight extends Flyweight
{
 
    /**
     * @param $content
     */
    public function show($content)
    {
        // TODO: Implement show() method.
        Echo "shared elements not shared: {$this - > name} {$content}";
    }
 
}
 
 
/**
 *Xiangyuan factory
 * Class FlyweightFactory
 */
class FlyweightFactory
{
 
    /**
     * @var array
     */
    private $flyweights = [];
 
 
    /**
     * @param $name
     * @return mixed
     */
    public function getFlyweight($name)
    {
        if (!isset($this->flyweights[$name])) {
            $this->flyweights[$name] = new ConcreteFlyweight($name);
        }
        return $this->flyweights[$name];
    }
 
}
 
 
##Client code
//Create a yuan factory
$flyweightFactory = new FlyweightFactory();
 
//Shared sharing element
$flyweight = $flyweightFactory->getFlyweight('state A');
$flyweight->show('other state A');
$flyweight = $flyweightFactory->getFlyweight('state B');
$flyweight->show('other state B');
 
//Objects that are not shared, called separately
$uflyweight = new UnsharedConcreteFlyweight('state A');
$uflyweight->show('state A');
 
 
##Test results
Shared share: state a other state a
Shared share: State B other state B
Unshared share: state a state a

 

 

Summary:

  • The purpose of meta sharing mode is to reduce the memory consumption when instantiating a large number of classes.
  • If an application uses a large number of objects, and a large number of these objects cause great storage overhead, it should be considered; In addition, most states of objects can be external states. If you delete the external states of objects, you can replace many groups of objects with relatively few shared objects. At this time, you can consider using the shared element mode.