PHP singleton mode and Factory mode

Source: Internet
Author: User

One, single case mode

A class can only instantiate one object

Class Ren
{
private static $DX; used to store generated objects, static methods call static members
Public $name;

Private Constructors
Private Function __construct ()
{

}
methods for generating objects
public static function Duixiang ()
{
if (Empty (self:: $DX))// determines if NULL, if NULL, new object
{
Self:: $DX = new Ren ();
}

Return self:: $DX;
}
}

$r = Ren::D Uixiang ();
$r->name = "Zhang San";

$r 1 = Ren::D Uixiang ();

Var_dump ($r 1);

Second, the factory model

The first way: not High Security

Class Suan

{

public $a;

Public $b;

function Jia ()

{

return $this->a+ $this->b;

}

Function Jian ()

{

return $this->a-$this->b;

}

}

$s =new Suan ();

$s->a=10;

$s->b=5;

echo $s->jia ();

The second way: scalability is strong

Class Yunsuan

{

public $a;

Public $b;

function Suan ()

{

}

}

Class Jia extends Yunsuan

{

function Suan ()

{

return $this->a+ $this->b;

}

}

Class Jian extends Yunsuan

{

function Suan ()

{

return $this->a-$this->b;

}

}

$s = new Jia ();

$s->a=10;

$s->b=5;

echo $s->suan ();

 The Third Way: Factory mode

Objective: The disadvantage of solving the second approach: when there are many subclasses, the method name may be heavy, and thus different objects can be determined by different operators

Class Yunsuan

{

public $a;

Public $b;

function Suan ()

{

}

}

Class Jia extends Yunsuan

{

function Suan ()

{

return $this->a+ $this->b;

}

}

Class Jian extends Yunsuan

{

function Suan ()

{

return $this->a-$this->b;

}

}//To this end is the same as the second way; The purpose is to solve when there are many subclasses, the method name may be heavy, so that different classes can be determined by different operators

Class Gongchang

{

static function Shengchan ($f)

{

Switch ($f)

{

Case "+":

return new Jia ();

Break

Case "-":

return new Jian ();

Break

}

}

}

$r = Gongchang::shengchan ("-");

$r->a=10;

$r->b=5;

echo $r->suan ();

  

PHP singleton mode and Factory mode

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.