C # factory model of design model (1)

Source: Internet
Author: User

To use the design pattern, you must first understand the object, inheritance, and interface. Generally, we use the new method when creating the object, which is related to how the program creates the object. Further thinking, we often need to create objects based on different program requirements. Therefore, we can abstract the creation process into a specialized class, so that the program will be more flexible and general. We can use a common base class to fully interact with objects related to these providers. The factory model provides a simple decision class that returns one of multiple subclasses of the base class based on the provided data. 1. Simple factory mode: It generally returns instances in one of the defined classes based on the provided data. Generally, the class it returns has a common parent class and method. However, in each subclass, the tasks to be executed for this method are different and optimized based on different data. The following is the sample code. This is an example of a calculator [csharp] public class Operation {public double NumberA {get; set;} public double NumberB {get; set ;} public virtual double GetResult () {double result = 0; return result ;}} public class OperationAdd: Operation {public override double GetResult () {double result = NumberA + NumberB; return result ;}} public class OperationSub: Operation {public override double GetResult () {double result = NumberA-NumberB; return result ;}} public class OPerationMulti: operation {public override double GetResult () {double result = NumberA * NumberB; return result;} public class OperationDiv: Operation {public override double GetResult () {double result = NumberA/NumberB; return result ;}} create a simple factory [csharp] public static Operation Creatoperation (string operate) {Operation condition = null; switch (operate) {case "+": condition = new OperationAdd (); break; case "-": condition = new OperationSub (); break; case "*": condition = new OPerationMulti (); break; case "/": condition = new OperationDiv (); break; default: break;} return condition ;} call a simple factory [csharp] Operation operator = new Operation (); operator = SimpleFactory01.Creatoperation ("/"); operator. numberA = 10; Numeric. numberB = 20; Console. writeLine (response. getResult (); Console. readLine ();

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.