Design pattern Notes-Simple Factory mode

Source: Internet
Author: User
Tags mul

Learning in simple Factory mode

A simple factory model is a factory object that determines which product class instances are created. Simple Factory mode is the simplest and most practical mode in the factory model family, which can be understood as a special implementation of different factory patterns.

For a summary of all design patterns, please refer to:

For a summary of all design principles, please refer to:

first look at the specific

The main consists of two numbers number_x and number_y below are four operations Add (+), Sub (-), Mul (*), DIV (/), and the factory instantiates the specific action type when the user clicks on one of the actions.

The class diagram for the application is as follows:

Specifically, the following code is implemented:

 Operation class public class Operation {//defines two operands number_x with number_y private double number_x = 0;        Private double number_y = 0;            Encapsulates public double number_x {get {return number_x; number_x}        set {number_x = value;}            }//encapsulates the number_y public double number_y {get {return number_y;}        set {number_y = value;}            }//define virtual function public virtual double Result () {double number = 0;        return number;            }}//addition operation public class Operation_add:operation {public override double Result () {            Double number = 0;            Number = number_x + number_y;        return number;            }}//subtraction operation public class Operation_sub:operation {public override double Result () {            Double number = 0;            Number = number_x-number_y;        return number;   } }//multiplication operation public class Operation_mul:operation {public override double Result () {Dou            ble number = 0;            Number = number_x * NUMBER_Y;        return number;            }}//division public class Operation_div:operation {public override double Result () {            Double number = 0;            if (number_y = = 0) return 0;            Number = number_x/number_y;        return number; }}//define a factory class to get the specific operation class public class Factoryofoperation {public static operation Gettheoperation (string            STR) {operation oper = null;                    Switch (str) {case "add": Oper = new Operation_add ();                Break                    Case "Sub": Oper = new operation_sub ();                Break                    Case "Mul": oper = new Operation_mul ();           Break     Case "div": oper = new Operation_div ();                Break            Default:break;        } return oper; }    }

SOURCE program:

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

Design pattern Notes-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.