-
-
class SingetonBasic { - private static $instance;
// other vars..
private function __construct() {
- // do construct..
- }
private function __clone() {}
public static function getInstance() {
- if (!(self::$instance instanceof self)) {
- self::$instance = new self();
- }
- return self::$instance;
- }
// other functions..
- }
$a = SingetonBasic::getInstance();
- $b = SingetonBasic::getInstance();
- var_dump($a === $b);< ;/p>
-
Copy code
2. Factory mode
The factory pattern allows you to create a class specifically designed to implement and return instances of other classes based on input parameters or application configuration.
Example of factory pattern:
-
-
- class FactoryBasic {
- public static function create($config) {
}
- }
-
Copy code
For example, here is a factory that describes shape objects. It hopes to create different shapes based on the number of parameters passed in.
-
-
// Define the public functions of the shape: get the perimeter and area.
- interface IShape {
- function getCircum();
- function getArea();
- }
// Define the rectangle class
- class Rectangle implements IShape {
- private $width, $height; p>
public function __construct($width, $height) {
- $this->width = $width;
- $this->height = $height;
- }
- < p>public function getCircum() {
- return 2 * ($this->width + $this->height);
- }
public function getArea() {
- return $this ->width * $this->height;
- }
- }
// Define circle class
- class Circle implements IShape {
- private $radii;
- < p>public function __construct($radii) {
- $this->radii = $radii;
- }
public function getCircum() {
- return 2 * M_PI * $this-> radii;
- }
public function getArea() {
- return M_PI * pow($this->radii, 2);
- }
- }
//Create different shapes based on the number of parameters passed in.
- class FactoryShape {
- public static function create() {
- switch (func_num_args()) {
- case 1:
- return new Circle(func_get_arg(0));
- break;
- case 2:
- return new Rectangle(func_get_arg(0) ), func_get_arg(1));
- break;
}
- }
- }
// Rectangular object
- $c = FactoryShape::create(4 , 2);
- var_dump($c->getArea());
- // Circle object
- $o = FactoryShape::create(2);
- var_dump($o->getArea()); p>
-
Copy code
Using the factory pattern makes it easier to call methods, because it only has one class and one method. If the factory pattern is not used, you have to decide which class and which method should be called when calling; using the factory pattern also makes it easier to call methods in the future. It is easier for the application to make changes. For example, to add support for a shape, you only need to modify the create() method in the factory class. Instead of using the factory pattern, you need to modify the code block that calls the shape.
3. Observer mode
The Observer pattern gives you another way to avoid tight coupling between components. The pattern is very simple: an object makes itself observable by adding a method that allows another object, the observer, to register itself. When an observable object changes, it sends messages to registered observers. These observers use this information to perform operations independent of the observable object. The result is that objects can talk to each other without having to understand why.
A simple example: when a listener is listening to a radio station (i.e. the radio station joins a new listener), it will send out a prompt message, which can be observed by the log observer who sent the message.
-
-
- // Observer interface
- interface IObserver {
- function onListen($sender, $args);
- function getName();
- }
-
// Observable interface
- interface IObservable {
- function addObserver($observer);
- function removeObserver($observer_name);
- }
// Observer class
- abstract class Observer implements IObserver {
- protected $name;
public function getName() {
- return $this->name;
- }
- }
// Observable class
- class Observable implements IObservable {
- protected $observers = array();
public function addObserver($observer) {
- if (!($observer instanceof IObserver)) {
- return;
- }
- $this->observers[] = $observer;
- }
public function removeObserver($observer_name) {
- foreach ($this->observers as $index = > $observer) {
- if ($observer->getName() === $observer_name) {
- array_splice($this->observers, $index, 1);
- return;
- }
- }
- }
- }
// Simulate a class that can be observed: RadioStation
- class RadioStation extends Observable {
public function addListener($listener) {
- foreach ($ this->observers as $observer) {
- $observer->onListen($this, $listener);
- }
- }
- }
// Simulate an observer class
- class RadioStationLogger extends Observer {
- protected $name = 'logger';
public function onListen($sender, $args) {
- echo $args, ' join the radiostation.
';
- }
- }
// Simulate another observer class
- class OtherObserver extends Observer {
- protected $name = 'other';
- public function onListen($sender, $args) {
- echo 'other observer..
';
- }
- }
$rs = new RadioStation();
// Inject observers
- $rs->addObserver(new RadioStationLogger());
- $rs->addObserver(new OtherObserver());
// Remove observers
- $rs ->removeObserver('other');
// You can see the observed information
- $rs->addListener('cctv');
- ?>
-
Copy code
|