PHP object-oriented programming (oop) learning notes (III)-Single sample mode and factory mode _ PHP Tutorial

Source: Internet
Author: User
PHP object-oriented programming (oop) learning notes (III)-Singleton mode and factory mode. 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, without a doubt, 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.

The code is as follows:


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.

The code is as follows:


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 );
}
}
}
// An object with the product Model A is required.
$ X = ProductFactory: factory ('A ');
$ X-> showProductInfo ();
// An object with the product Model B is required.
$ O = ProductFactory: factory ('B ');
$ O-> showProductInfo ();
// You can call the showProductInfo method.

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.

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.