PHP Part---Object-oriented, design patterns (singleton mode, Factory mode), OOP six principles;

Source: Internet
Author: User

I. Singleton mode
Purpose: In order to control the number of objects (only one, equivalent to the class of family planning)
Practice
1. Make the constructor of a class private
2. A public function is created in the class to make the object
3. Change the function to static
4. Add control inside the function

class ren{public$name; Static  Public $DX ;                   Static member variables are used to store the object
 private  function   __construct ()//Set the constructor to private, the outside of the class cannot use new to create the object 
{
}

static function Duixiang ()//Do a static method that is used to create objects within the class
{

if (empty (Self:: $DX {
Self :: $dx = new Ren (); Because it is a static variable, only the class name can be called
}
return self:: $DX }
}

$r = ren::$r->name = "Zhang San"$r 1 = ren::$r 1->name = " John Doe "var_dump($r);              Since an object has been created above, $r 1 is the same object when recreating $R1

Second, the factory model

Abstract classYunsuan//Use the abstract keyword to define a class as an abstraction class { Public $a; Public $b;//The first Suan function is a normal classfunctionSuan () {}}classJiaextendsyunsuan{functionSuan () {return $this->a+$this-b;}}classJianextendsyunsuan{functionSuan () {return $this->a-$this-b;}}classChengextendsyunsuan{functionSuan () {return $this->a*$this-b;}}//do a factory classclassgongchang{Static functionShengchan ($fuhao//Give it a variable, and the values returned with different functions of the variable are different {Switch($fuhao){ Case"+":return NewJia (); Break; Case"-":return NewJian (); Break; Case"*":return NewCheng (); Break;}}}//count Addition$suan= Gongchang::shengchan ("*");$suan->a = 10;$suan->b = 5;Echo $suan->suan ();

Static methods can adjust static variables, but cannot adjust normal variables

Normal methods can adjust static variables, or normal variables

OOP basically has 6 principles, which are actually complementary, that is to say, some principles need to use other principles to achieve their own. The 6 main principles are as follows:

1) open-close Principle (OCP), open-close principle, the design to the extension has good support, and the changes are strictly limited. This is the most important and most abstract principle, and basically what we call reusable software is developed based on this principle. Other principles also provide a path to its implementation.

2) Liskov substituition Principle (LSP), the Richter substitution principle, very strict principle, the rule is "the subclass must be able to replace the base class, otherwise it should not be designed as a subclass." In other words, subclasses can only extend the base class, not hide or overwrite the base class.


3) Dependence inversion Principle (DIP), dependent on the switching principle, "design relies on abstraction rather than materialization". In other words, when designing, we need to think in abstractions, rather than start dividing what classes I need from the first, because these are specific. What good does it do? People's thinking itself is actually very abstract, we analyze the problem is not a moment to consider the details, but very abstract the whole problem is conceived out, so oriented to abstract design is in line with human thinking. In addition, this principle will be well supported by the OCP, and the abstraction-oriented design allows us to not rely too much on implementation, so that the extension becomes possible, and this principle is the cornerstone of another article, design by contract.

4) Interface segregation Principle (ISP), interface isolation principle, "breaking large interfaces into multiple small interfaces", the benefits are obvious, I do not know if there is any need to continue to describe, in order to save space, In fact, I just made a small summary of these principles, if there is a need for more in-depth understanding of the recommendation to see "Java and Mode", MS MVP one: this masterpiece! ^_^

5) Single duty: One class functions as single as possible, reducing coupling

6) Law of Demeter or Least knowlegde Principle (LoD or LKP), Dimitri rule or least-knowledge principle, this principle was first formally applied in the Demeter system, so it is defined as the Dimitri rule. It says that "one object should be as little as possible to understand other objects." This is another law about how to loosely couple (loosely-coupled).

PHP Part---Object-oriented, design patterns (singleton mode, Factory mode), OOP six principles;

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.