PHP Object Oriented Programming (oop) Learning Notes of Three Case Mode and Factory Mode

  • 2021-06-29 10:29:03
  • OfStack

There is no doubt that design models are win-win for others and systems.Design patterns make coding truly engineering;Design patterns are the cornerstone of software engineering, just like building structure 1.

Singleton mode

Singleton patterns are useful when you need to ensure that only one instance of an object exists.It delegates control of creating objects to a single point of 1, where only one instance of the application exists at any time.A singleton class should not be instantiated outside of the class A singleton class should have the following elements.

You must have a constructor with an access level of private to prevent the class from being arbitrarily instantiated.

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

You must have a public static method to access this instance, usually named GetInstance().

Must have a private empty_uclone method to prevent instances from being cloned and copied.

Here 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 implementing the same interface as 1, instantiating the appropriate classes at the right time will not only confuse your business logic but also make your project difficult to maintain if you spread these news around the project.At this time, if the concept of factory mode is introduced, this problem can be well handled.We can also have factory classes return appropriate instances for us by configuring the application or providing parameters.

Factory class, which places the process of instantiating a class within each factory class and is designed to create objects of other classes.Factory mode is often used in conjunction with Interface 1, so applications do not have to know the details of these instantiated classes, as long as they know that the factory returns classes that support an interface for easy 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 A.';
    }
}
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);
        }
    }
}
// Need here 1 Product models are  A  Object 
$x = ProductFactory::factory('A');
$x -> showProductInfo();
// Need here 1 Product models are  B  Object 
$o = ProductFactory::factory('B');
$o -> showProductInfo();
// Can be called showProductInfo Method because they both implement interfaces  ProductInterface.

Summary

The pattern is like the cornerstone of software engineering, like the design blueprint of a building, where two patterns are touched: the singleton pattern and the engineering pattern.There is a static variable in the singleton class that holds one instance of itself and provides a static way to get it.Singleton classes should also mark constructors and clone functions as private to prevent breaking the 1-only nature of instances.Factory mode creates different types of instances based on incoming parameters or program configurations, and factory classes return objects, which are critical in polymorphic programming practice.


Related articles: