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 );
II. factory modelThe factory mode allows you to create a class dedicated to implementation and return other classes based on input parameters or application configurations. Example of Factory mode:
Class FactoryBasic {
- Public static function create ($ config ){
}
- }
For example, this is a factory that describes shape objects. it wants to create different shapes based on the number of input parameters.
// Define the common function of the shape: obtain the length and area of the week.
- Interface IShape {
- Function getCircum ();
- Function getArea ();
- }
// Define the rectangle class
- Class Rectangle implements IShape {
- Private $ width, $ height;
Public function _ construct ($ width, $ height ){
- $ This-> width = $ width;
- $ This-> height = $ height;
- }
Public function getCircum (){
- Return 2 * ($ this-> width + $ this-> height );
- }
Public function getArea (){
- Return $ this-> width * $ this-> height;
- }
- }
// Define the circle class
- Class Circle implements IShape {
- Private $ radii;
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 input parameters.
- 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 ());
Using the factory mode makes it easier to call a method because it only has one class and one method. if the factory mode is not used, it is necessary to decide which class and method should be called during the call; using the factory mode makes it easier to change the application in the future, such as adding a shape of support, you only need to modify the create () method in the factory class, instead of using the factory mode, you need to modify the code block that calls the shape. 3. Observer ModeThe Observer Mode provides another way to avoid close coupling between components. This mode is very simple: an object becomes observability by adding a method (this method allows another object, that is, the Observer registers itself. When an observed object changes, the message is sent to the registered observer. The operations performed by these observers using this information are irrelevant to the observed objects. The result is that objects can communicate with each other without understanding the cause. A simple example: when a listener is listening to a station (that is, a new listener is added to the station), it sends a prompt message, which can be observed by the log observer who sends the message.
// Observer interface
- Interface IObserver {
- Function onListen ($ sender, $ args );
- Function getName ();
- }
// Interface to be observed
- Interface IObservable {
- Function addObserver ($ observer );
- Function removeObserver ($ observer_name );
- }
// Observer class
- Abstract class Observer implements IObserver {
- Protected $ name;
Public function getName (){
- Return $ this-> name;
- }
- }
// Class to be observed
- 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 the Observer
- $ Rs-> addObserver (new RadioStationLogger ());
- $ Rs-> addObserver (new OtherObserver ());
// Remove the Observer
- $ Rs-> removeObserver ('other ');
// You can see the observed information.
- $ Rs-> addListener ('CCTV ');
- ?>
|