mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-09-25 13:59:08 +02:00
added flyweight pattern
This commit is contained in:
36
Structural/Flyweight/CharacterFlyweight.php
Normal file
36
Structural/Flyweight/CharacterFlyweight.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Structural\Flyweight;
|
||||
|
||||
/**
|
||||
* Implements the flyweight interface and adds storage for intrinsic state, if any.
|
||||
* Instances of concrete flyweights are shared by means of a factory.
|
||||
*/
|
||||
class CharacterFlyweight implements FlyweightInterface {
|
||||
|
||||
/**
|
||||
* Any state stored by the concrete flyweight must be independent of its context.
|
||||
* For flyweights representing characters, this is usually the corresponding character code.
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param string $name
|
||||
*/
|
||||
public function __construct($name) {
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clients supply the context-dependent information that the flyweight needs to draw itself
|
||||
* For flyweights representing characters, extrinsic state usually contains e.g. the font
|
||||
* @param string $font
|
||||
*/
|
||||
public function draw($font)
|
||||
{
|
||||
print_r("Character {$this->name} printed $font \n");
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user