PHP Advanced Programming Study Notes
Design pattern is a set of summary of code Design experiences that are repeatedly used, known to most people, classified and catalogued. The design pattern is used to make code reusable, make it easier for others to understand, and ensure code reliability. There is no doubt that the design pattern is win-win for others and the system; The design pattern enables code compilation to be truly engineered; The design pattern is the cornerstone of the software engineering, just like the structure of the building.
Singleton Mode
The Singleton mode is useful when you need to ensure that an object can only have one instance. It delegates the control of the created object to a single point. At any time, the application only has one instance. A singleton class should not be instantiated outside the class. A singleton class should have the following elements.
You must have an access-level private constructor to effectively prevent classes from being instantiated at will.
You must have a static variable for the instance that saves the class.
You must have a public static method to access this instance. This method is usually named GetInstance ().
You must have a private, empty _ clone method to prevent the instance from being cloned and copied.
The following is an example of a simple Singleton class.
class ClassName{ public static $_instance; private function __construct() { # code... } private function __clone() { # empty } public static function GetInstance() { if(!(self::$_instance instanceof self)) { self::$_instance = new self(); } return self::$_instance; } public function SayHi() { echo "Hi boy!"; }}$App= ClassName::GetInstance();$App->SayHi();/** * * Output * * Hi boy! * */
Simple factory Mode
When you have a large number of classes that implement the same interface, instantiate the appropriate classes at the right time. If you scatter these new classes to every corner of the project, this will not only make the business logic messy, but also make the project difficult to maintain. At this time, if the concept of factory model is introduced, this problem can be well handled. We can also configure the application or provide parameters so that the factory class can return an appropriate instance for us.
Factory class, which puts the process of Instantiation class into various factory classes and is used to create objects of other classes. The factory mode is often used together with the interface, so that the application does not need to know the specific details of the classes to be instantiated, as long as it knows that the factory returns a class that supports an interface, it is very convenient to use. The following is a simple example of the use of the factory class.
Interface ProductInterface {public function showProductInfo ();} class ProductA implements ProductInterface {function showProductInfo () {echo 'This is product. ';}} class ProductB implements ProductInterface {function showProductInfo () {echo' This is product B. ';}} class ProductFactory {public static function factory ($ ProductType) {$ ProductType = 'product '. strtoupper ($ ProductType); if (class_exists ($ ProductType) {return new $ ProductType ();} else {throw new Exception ("Error Processing Request ", 1) ;}}// A product model $ x = ProductFactory: factory ('A'); $ x-> showProductInfo () is required here ();
// Here we need an object with the product model B $ o = ProductFactory: factory ('B'); $ o-> showProductInfo ();
// You can call the showProductInfo method because all of them implement the interface ProductInterface.?>
Summary
The model is like the cornerstone of software engineering, like the design drawings of the building. There are two models available: singleton and engineering. There is a static variable in the singleton class that stores its own instance and provides a static method to obtain the static variable. Singleton classes should also mark constructor and clone function as private to prevent the uniqueness of the instance from being broken. The factory mode creates different types of instances based on input parameters or program configurations. The factory class returns objects, and the factory class is crucial in multi-lingual programming practices.