Simple factory mode and factory Mode

Source: Internet
Author: User

Simple factory mode and factory Mode

In my opinion, the design pattern is for software engineering. It reduces program coupling through design, including encapsulation, inheritance, and polymorphism, so that software engineering is easy to modify and take easily. Although as a manufacturer, there is no need to deal with the client interface architecture, but the design pattern can be found at the beginning of daily code work, unlike the code structure that acm ignores.
Let's talk about the simple factory model this time. After all, this is easier to understand. In the first article of the big talk design model, I read a book and read it back and forth to see the most.

What is a factory and what can be produced in batches according to the mold. What is a model? A model refers to a core knowledge system extracted from the production experience and life experience through abstraction and sublimation. Pattern is actually a methodology for solving a certain type of problems. Summarize the methods for solving a certain type of problem to the theoretical level, that is, the mode.

Therefore, the factory model is to send our "Mold" to the factory, and then provide us with a "product" after the factory is processed ". The core of the simple factory model is the separation of business logic.
The previous code:

class Factory {    public static E createObject(E object) {        Object ob = null;        swtich(object) {            case "a":                ob = new makeObjectA();                break;            case "b":                ob = new makeObjectB();                break;        }        return ob;

The above is a factory class. When we pass in an object, the factory generates an object based on our mold and processes the data based on this object.

public class makeObjectA(){    private E segA;    private E segB;    public E cal() {        return segA*segB;    }}
Object ob;ob = Factory.createObject("a");ob.segA = tmpObject;ob.segB = tmpObject;ob.cal();

The general framework is like this. When there are many mold types, the business logic is separated, and the modification and reuse are simpler when the demand changes.
You can also define interfaces to regulate the behavior of objects produced by the factory and ensure the behavior of objects to achieve standardized production.

interface makeObject{    public E cal();}

Then let

makeObjectA implements makeObject{}

A simple factory model that separates business logic from customer logic becomes.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.