The content is personal thoughts. I just want to convert professional things into common things, and I don't want to contain wordy components.
1. Preschool stories:
After reading 3 or 4 simple factory examples, the calculator is the one with the deepest image (it seems like Microsoft's interview question "Calculator, implemented with OOP ")
To be honest, before talking about the mode, remember two formulas: (interface naming conventions, adding "I" before the name ")
IA Ic = new C;
IA id = new D;
When the parameter is IA type, the IC and ID can be passed in. When the method and attribute in the IC are called in C, when ID calls the methods and attributes in it, it is in D.
This is a polymorphism. If you can't remember it, remember the fuzzy feeling. You know, it doesn't matter if you can't say it.
Parent Class A = new subclass 1;
Parent Class A = new subclass 2;
The same principle as above
There is a professional term called "The Lee's replacement principle", which hates professional
This is the legend of their polymorphism, master this, Java OOP is exactly the same, remember this feeling, learning design mode is very important.
Related Knowledge points: C # oop c # Interface C # inherit C # polymorphism C # Encapsulation
2. Start learning (the second formula is used below) Examples of not wanting to use a calculator ~
Yes ~ Yes ~
Let's build blocks. I miss a game in kindergarten when I was a child ~
Component: Wood of various shapes
Action: Pick up building blocks
Create a console application
① Create a new wood class, the general term of wood, to basically determine the appearance of its subclass. If you do not understand the virtual keyword, it is recommended to first learn OOP
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace simplefactorydemo {public class wood {// <summary> // wood shape /// </Summary> Public String woodshape {Get; set ;} /// <summary> // wood color // </Summary> Public String woodcolor {Get; set;} Public Virtual void startbuild () {console. writeline ("you picked up the" + woodcolor + "+ woodshape +" building block... "); console. writeli Ne ("successful! ");}}}
Then we start to make the first piece of wood, the yellow round wood.
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace simplefactorydemo {public class roundwood: Wood {public override void startbuild () {woodcolor = "yellow"; woodshape = "Circular"; base. startbuild ();}}}
In the same way, we can do a few more.
White square wood
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace simplefactorydemo {public class squarewood: Wood {public override void startbuild () {woodcolor = "white"; woodshape = "square"; base. startbuild ();}}}
Grey arched wood
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace simplefactorydemo {public class Archwood: Wood {public override void startbuild () {woodcolor = "gray"; woodshape = "arch"; base. startbuild ();}}}
Well, we have all the raw materials for building blocks. Now we have three blocks of wood.
But in fact, we have more than three pieces of wood, and there may be other shapes of wood in the future. To make it easier to have more wood, we need a factory to make the wood. To put it bluntly, in fact, this is to make it easier and easier for us to build wood. You will know when we take wood.
Let's build a wood factory.
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace simplefactorydemo {public class factory {public static wood createwood (string shape) {wood Newwood = NULL; Switch (SHAPE) {Case "circle": Newwood = new roundwood (); break; Case "square": Newwood = new squarewood (); break; Case "arch": Newwood = new Archwood (); break; default: break ;} return Newwood ;}}}
Here I am only producing wood based on the shape. There is no color. If you want to control the color, you can add a color to make it look like our second formula.
Parent Class A = new subclass 1;
Parent Class A = new subclass 2;
Okay, so far, there is wood. Oh, wrong. It's a building block;
The building blocks are available, and the factories that produce the building blocks are also available. Now let's build what you want.
This is what I wrote in the program. CS file.
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace simplefactorydemo {class program {static void main (string [] ARGs) {wood factory = factory. createwood ("square"); factory. startbuild (); console. readline ();}}}
Effect
If we don't consider the fixed color, now we just need to change the square to the existing shape around us, and the factory will help you produce the building blocks of the shape, saying that production is too good, you just need to create a building block object with shapes for you, and then you can just drop this building block method,
Using a simple factory model can facilitate the expansion of a certain situation, but you do not know the total number of such cases. In this case, there must be something in common.
Check it out.
Wood factory = factory. createwood ("square"); factory. startbuild (); wood factory1 = factory. createwood ("arch"); factory1.startbuild (); wood factory2 = factory. createwood ("Circular"); factory2.startbuild (); console. readline ();}
Other examples: for example, a two-digit calculator, using a factory to create an operation method, for example, +-x calculator /...
My application expansion: There are many checkout methods. I use the factory to create various checkout methods. when calling the front-end, I only need to pass the name of the Checkout Method: I can create the objects of the checkout method, you can call the method or attribute of this Checkout Method, and add a new Checkout Method in the future.
Also, it is safer to separate writing classes. For example, if you don't want some people to see the checkout algorithm you write in some way, a simple factory is a good choice. If you want others to create a new class, just write his method, and then you can add a Checkout Method to the class in the factory.
Show a project structure.
Demo download: http://download.csdn.net/detail/yangyanghaoran/4327225