Design Patterns: Design Patterns

1. Singleton mode

  The Singleton Pattern is a relatively simple pattern, which is defined as follows: Ensure a class has only one instance, and provide a global point of access to it. Provide this instance to the entire system.)

Singleton pattern general class diagram

 

<?php
    class Singleton {
         private static $initial = null;
        
         // Private constructor 
         private  function __construct(){
         }
         // Private clone method 
         private  function __clone(){
         }
         
         public static function  Initialization(){
                if(self::$initial == null){
                    self::$initial = new self();
                }
                return  self::$initial; 
         }
    }

  advantage:

    1. Since the singleton pattern has only one instance in memory, the memory overhead is reduced. When an object needs to be created and destroyed frequently, and the performance cannot be optimized during creation or destruction, the advantages of the singleton pattern are very obvious.

    2. The singleton pattern can avoid multiple occupation of resources

    3. Singleton mode can set global access points in the system, optimize and share resource access

  shortcoming:

    1. The singleton mode generally has no interface and is difficult to expand

    2. The singleton pattern conflicts with the single responsibility principle. A class should only implement one logic, regardless of whether it is a singleton. Whether it is a singleton depends on the environment. The singleton pattern combines "singleton" and business logic in one class

 

Second, the factory method pattern

  Factory method pattern. Defined as: Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. (Define an interface for creating an object, and let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. method defers instantiation of a class to its subclasses.)

Factory pattern generic class diagram

 

<?php
   interface  zoon {
           function introduce();
   }

   class dog implements zoon{
            function introduce(){
             echo "I am a dog, I am barking" . PHP_EOL ;
        }
   }

   class pig implements zoon{
            function introduce(){
             echo "I am a pig, I am humming" . PHP_EOL ;
        }
   }

   class sheep implements zoon{
            function introduce(){
             echo "I am a sheep, I baa baa baa" . PHP_EOL ;
        }
   }
  
   interface createZoon{
           function create();
   }
   
   class FactoryDod implements createZoon{
       function create(){
               return new dog();
       }
           
   }

   class FactoryPig implements createZoon{
            function create(){
               return new pig();
       }
   }


   class FactorySheep implements createZoon{
            function create(){
               return new sheep();
       }
   }


  class Factory{
          function __construct(){
            $d = new FactoryDod();
            $d->create()->introduce();
      
            $p = new FactoryPig();
            $d->create()->introduce();
      
            $s = new Factorysheep();
            $s->create()->introduce();
        }
  }

  $t = new Factory();
  

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325126848&siteId=291194637