Use code and examples to understand the simple factory Mode

Source: Internet
Author: User

Brief Introduction The essence of simple factory mode is that a factory class dynamically decides which product class to create (these product classes inherit from a parent class or interface) based on input parameters. Does not belong to 23 GOF modes, but it can be used as a guide for Factory Method. it is a creation mode, also known as the StaticFactory Method. It is the simplest and most practical mode among many factory models. Role composition abstract Product role definition simple factory created object parent class, can be a class, or an interface. [Csharp] <span style = "font-size: 18px;"> // declare the parent class of the created object public class Operation {privat double _ numberA = 0; // declare a double-precision variable private double _ numberB = 0; public double NumberA {get {return _ numberA;} // when reading, get _ numberA = 0 set {_ numberA = value;} // You can dynamically assign a public double numberA {get {return_numberB;} set {_ numberB = value ;}} public virtual double GetResult () // override GetResult <span> </span> {double result = 0; Return result ;}</span> the specific product role is the specific processing object of the factory [csharp] <span style = "font-size: 18px; "> // declare an addition operation class that inherits the operation class OperatinAdd: operation {public override double GetResult () {double result = 0; result = numberA + numberB; return result ;}</span> above is an addition specific product role, there are also subtraction and multiplication, division is similar. The core of the simple factory role, including the internal logic of all instances, can be called by clients to create the objects required for the product. It defines the parent class of an object created in a simple factory and will inherit this class when the object is created. It can be an interface and an abstract class. First, construct an OperationFactory to create the logical relationship between operators. For example, an addition operation is created when I enter the addition symbol. The same applies to other operators. The Code is as follows [csharp] <span style = "font-size: 18px;"> // simple factory class, define the logic between operators Public class OperateFactory {public static Operation createoperate (string operate) {operation operator = null; // declare a non-referenced variable switch (operate) // branch statement to process the logical relationship {case "+": condition = new OperationAdd (); // create the break for addition operations; case "-": condition = new OperationSub (); break; case "*": break; case "/": condition = new OperationDiv (); break;} return condition ;}} </span> It is the entire factory role and the core of the whole simple factory model. It processes the logical relationship between operators and can be directly called to process the input operators and numbers. To sum up this example, the example is divided into three parts. We need to start from the simple factory model to make it complete. Part 1: Construct a factory class to process the logical relationship between operators. Part 2: Create an object parent class. The class attributes numberA and number B are declared. This attribute is called directly when an object is created in the next step, and operators are used to calculate the two attribute values. Part 3: Create an object that inherits operation and reduces code duplication.

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.