Simple Factory mode PHP implementation C # Simple Factory mode Factory mode Java Java Simple Factory mode instance

Source: Internet
Author: User
The simple Factory mode is also called the Static factory method pattern, and the main function is to instantiate (create) objects of each class through a simple factory class, without having to instantiate the object through new. The advantage is that the factory class contains certain logical judgments that dynamically instantiate related classes based on the client's selection criteria. The downside is that when you need to add a new feature class, you need to modify the factory class.

The following is a case study of a simple calculator program. The first step, defining operation, is a parent class with two properties representing the two parameters used for the calculation.

 
  Numa= $numA;} Public Function Getnuma () {return $this->numa;} Public Function Setnumb ($numB) {$this->numb= $numB;} Public Function Getnumb () {return $this->numb;}}? >

The second step is to define an interface that declares the method of implementing the Operation

 
  

The third step is an addition operation class (Omit subtraction class, multiplication class, trigger class, etc.)

 
  Getnuma () + $this->getnumb (); return $result;}}? >

Finally, you define a simple factory class that is used to create object instances of various classes. Normally the objects returned by the simple factory class have a common parent class. In this example, the public parent class is the Operation class, and the addition and subtraction classes are subclasses of the Operation class.

 
  

The client code is as follows:

 
  Setnuma (2); $op->setnumb (4); Echo $op->getresult (); $om =simplefactory::createminus (); $om->setnuma (45); $ Om->setnumb, echo "
"; Echo $om->getresult ();? >

The above introduces the simple factory model of PHP implementation, including the simple Factory mode content, I hope the PHP tutorial interested in a friend helpful.

  • 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.