Design Model-simple factory Model

Source: Internet
Author: User

Simple factory Mode

In the design mode, when adding, subtracting, multiplication, division, or adding other operations to the calculator, instantiate it and you can use the simple factory mode.

Use a separate class to create an instance. This is the factory.

From the "big design model", the thinking is constantly colliding. Let's review the key changes in the preparation process of the calculator.


For example, if you use object-oriented language to implement a calculator console program, you must enter two numbers and operators to obtain the result"

Change
Advantages
Insufficient

Remarks


Process-oriented

-------


The calculator code for writing windows and so on cannot be reused.


Not easy to maintain, not flexible, not easy to expand, not easy to reuse

Separate computing and display

Another calculator in a windows program can also be reused directly.
If you want to add an on-demand operation to the dish, he can take the opportunity to modify part of his salary code.
Because all operations are combined. Modifying one may affect another
Computation is divided into operation class and sub-class

Subclass inherits the parent class and overwrites the GetResult () method,

When modifying an algorithm, you do not need to provide other algorithm code.


The calculator does not know which algorithm to use.


In the calculation class, there are two Number attributes used to calculate the Number before and after the calculator, and then there is a virtual method GetResult () for obtaining the result

Use a separate class to create an instance

As long as you enter the operator number, the factory will be able to create an appropriate object for instance, and it will be easy to add and modify the algorithm later.

----------------


This is the simple factory model.


UML diagram

Let's use a UML diagram to see its pattern.

Note: I really think that the awareness of the dish is high-_-"|, but in the process of his change, we also experience the beauty of programming art again and again. Everything comes from life, so is learning. Returning to Life will give you an extra charm. Everything is clear and easy.




Simple factory model in life



I thought, if I had such a factory, what would I use it to produce?

Scenario 1: air quality in different regions.

Let's take a look at the quality of Langfang and Beijing. Of course, the air quality in our hometown must also be unique.

Don't worry. I use a factory to "produce" them. I can make them simple when I go to Hainan.

Single extension.

Scenario 2: promotion at the exhibition in stage 11.

We also set up a factory model for them. In a few days, there may be too many students on Monday and Tuesday, so we can arrange a few more.

Publicity. There are only a few students on Wednesday and Thursday. But their processes are all the same. Define a parent class "flow" first, rewrite the method in the definition subclass, and rewrite the method according to their different locations or methods. Use a factory to instantiate it. In this way, the class can be added to the factory when the promotion program of the 10th stage is also attended. And the instance is ready.

Scenario 3: traveling on May 1.

Where are the students preparing to play. Let's also create a factory model for travel agencies.

A travel agency has five routes. One-day tour in Beijing and one-day tour in Tianjin. In terms of the interface, it involves the visitor name and time, so that both windows and web can be reused on mobile phones. In terms of business, they will all involve fares, itineraries, and precautions. Create a base class and rewrite the method in the subclass. Use a simple factory model. If the students all reflect that they want to go to Baiyangdian, the travel agency can add other classes without affecting them. You can change the discount value flexibly.





Note: The following is the artistic embodiment of the final calculator. Let's take a look at its changing and dynamic beauty from beginning to end.



Code

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; // use a separate class to create an instance. public class Operation {private double _ numberA = 0; private double _ numberB = 0; public double NumberA {get {return _ numberA;} set {_ numberA = value ;}} public double NumberB {get {return _ numberB ;}set {_ numberB = value ;}} public virtual double GetResult () {double result = 0; return result ;}////// Rewrite result after inheritance ///Class OperationAdd: Operation {public override double GetResult () {double result = 0; result = NumberA + NumberB; return result ;}} class OperationSub: Operation {public override double GetResult () {double result = 0; result = NumberA-NumberB; return result ;}} class OperationMul: Operation {public override double GetResult () {double result = 0; result = NumberA * NumberB; return result ;}} class OperationDiv: Operation {double result = 0; if (NumberB = 0) throw new Exception ("the divisor cannot be 0. "); result = NumberA/NumberB; retrun result ;)}////// Simple factory mode ///Public class OperationFactory {public static Operation createOperate (string operate) {Operation condition = null; switch (operate) {case "+": condition = new OperationAdd (); break; case "-": condition = new OperationSub (); break; case "*": condition = new OperationMul (); break; case "/": condition = new OperationDiv (); break;} return Token ;}}////// Interface implementation ///Operation condition; condition = OperationFactory. createOperate ("+"); condition. NumberA = 1; condition. NumberB = 2; double result = condition. GetResult ();}


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.