Simple Factory mode

Source: Internet
Author: User

The design pattern in my opinion is for software engineering, through the design including encapsulation, inheritance, polymorphism, etc. to reduce the degree of coupling, making software engineering easy to modify and easy to take. Although as a vendor worker, there is no need to face the client to develop a customer interface architecture of the work, but the design pattern in the daily code work is a hand to know there is no, unlike the brush ACM disregard code structure.
This time to talk about the simple factory model, after all this is easier to understand in the big talk design mode first, turn a book to go back and forth to see the most.


Therefore, the factory model is to send our "mold" to the factory, factory processing to provide us with a "product." The core of the simple factory model is the separation of business logic.
Previous section of code:

class Factory {    publicstaticcreateObjectobject) {        null;        swtich(object) {            case"a":                new makeObjectA();                break;            case"b":                new makeObjectB();                break;        }        return ob;

Above is a factory class, when we pass an object, factory according to our mold, the output of an object to us, we based on this object processing data.

publicclassmakeObjectA(){    private E segA;    private E segB;    publiccal() {        return segA*segB;    }}
Object ob;ob = Factory.createObject("a");ob.segA = tmpObject;ob.segB = tmpObject;;

The general framework is this, when the type of mold to separate the business logic, when the need to change the time to modify and reuse easier.
Interfaces can also be defined to standardize the behavior of factory-produced objects and to ensure that the object's behavior is standardized in order to achieve standardised production.

interface makeObject{    publiccal();}

Then let


A simple factory model that separates business and customer logic.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Simple 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: 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.