PHP object-oriented Programming (OOP) learning Notes (iii)-Example of _php and Factory mode

Source: Internet
Author: User

There is no doubt that the design pattern is much more than others in the system. Design patterns make coding truly engineering; Design patterns are the bedrock of software engineering, like the structure of a building.

Single case mode

Singleton mode is useful when you need to guarantee that an object can have only one instance. It delegates control over the creation of objects to a single point, where only one instance of the application will exist. A singleton class should not be instantiated outside of a class. A singleton class should have the following elements.

You must have a constructor that has a private access level that effectively prevents classes from being arbitrarily instantiated.

You must have a static variable that holds an instance of the class.

You must have a public static method that accesses this instance, which is usually named GetInstance ().

You must have a private, empty __clone method to prevent the instance from being cloned.

Here is a simple example of a single example class to illustrate

Copy Code code 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 class at the right time, and if you spread these new to every corner of the project, it will not only confuse the business logic and make the project difficult to maintain. If we introduce the concept of factory model, we can deal with this problem very well. We can also allow the factory class to return the appropriate instance to us through the application configuration or by providing the Parameter form.

Factory class, which puts the process of instantiating a class into a factory class, specifically to create objects of other classes. Factory patterns are often used in conjunction with interfaces so that the application does not have to know the specifics of the instantiated classes, as long as it is convenient to know that the factory is returning a class that supports an interface. The following is a brief example of the use of a factory class.

Copy Code code as follows:

Interface Productinterface
{
Public function showproductinfo ();
}
Class ProductA implements Productinterface
{
function Showproductinfo ()
{
Echo ' This is product A. ';
}
}
Class PRODUCTB implements Productinterface
{
function Showproductinfo ()
{
Echo ' 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);
}
}
}
Here you need a product model A object
$x = productfactory::factory (' A ');
$x-> showproductinfo ();
Here we need a product model B object
$o = productfactory::factory (' B ');
$o-> showproductinfo ();
Can call the Showproductinfo method, because both implementations implement the interface Productinterface.

Summary

The pattern is like the cornerstone of a software project, like the design drawings of a building, where two modes are exposed: Single case mode and engineering mode. A static variable exists in a singleton class that holds an instance of itself and provides a static method for obtaining this static variable. Singleton classes should also mark constructors and clone functions as private to prevent the uniqueness of broken instances. The factory pattern creates different types of instances based on the parameters passed in or the program's configuration, and the factory class returns objects, and the factory class is critical in the practice of polymorphism programming.

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.