PHP Design-singleton mode and Factory mode

Source: Internet
Author: User

One, the singleton mode is also called the responsibility mode, it is used in the program to create a single function of the access point, in layman's name is the object is unique.
All singleton modes have at least the following three common elements:
1. They must have a constructor and must be marked as private
2. They have a static member variable that holds an instance of the class
3. They have a public static method that accesses this instance
A singleton class cannot be instantiated directly in any other class, but only by its own instantiation. Instead of creating an instance copy, it returns a reference to the instance stored inside the singleton class.

Singleton Pattern Example
<?php
Class Single { private $name;//Declare a private instance variable private function __construct () {//Declare private constructor method in order to prevent external code from using new to create the object. } static public $instance;//declares a static variable (a unique instance saved in the Class) static public function getinstance () {//Declares a getinstance () static method, Used to detect if there is an instance object if (!self:: $instance) Self:: $instance = new self (); Return self:: $instance; } Public Function SetName ($n) {//Assign value to variable $name

   } public function GetName () {//Take variable $name value

}} $a = Single::getinstance (); $b = Single::getinstance (); $a->setname (' Hello World '); $b->setname (' Good Morning '); echo $a->getname ();//good morning echo $b->getname ();//good morning
?>
Factory mode is a class that has some methods for creating objects for you, so you can use the factory class to create objects instead of using new directly.
This way, if you want to change the type of object you create, you can simply change the factory.
<?php
Factory class (Operations on Operators)
Class factory{
public static function dx ($ope) {
Switch ($ope) {
Case "+":
return new Plus ();
Break
Case "-":
return new sub ();
Break
Case "%":
return new REM ();
Break
}
}
}
$ope = Factory::d x ("+");
$ope->a = 20;
$ope->b = 10;
echo $ope->opera ();

Factory mode Instances
Class Factory {//Create a basic factory class static public function FAC ($id) {//Create a static method that returns an instance of an object if (1 = = $id) return new A ();          ElseIf (2== $id) return new B ();          ElseIf (3== $id) return new C ();      return new D ();  }} interface Fetchname {//Create an interface public function getname ();/} class A implements fetchname{private $name =     "AAAAA"; Public Function GetName () {
return $this->name;
}} class C implements fetchname{private $name = "CCCCC"; Public Function GetName () {
return $this->name;
}} class B implements fetchname{private $name = "bbbbb"; Public Function GetName () {
return $this->name;
}} class D implements fetchname{private $name = "ddddd"; Public Function GetName () {
return $this->name;
}
} $o = FACTORY::FAC (6);//Call the method in the factory class if ($o instanceof fetchname) {echo $o->getname ();//ddddd} $p =FACTORY::FAC (3); echo $p->getname ();//CCCCC
? >

PHP Design-singleton mode and Factory mode

Related Article

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.