The design pattern can make our code more flexible, easier to expand, and easier to maintain. Various Object-Oriented Programming Languages provide basically the same mechanism, such as classes, inheritance, derivation, and polymorphism. However, they have their own characteristics. The reflection mechanism in C # is a very important tool, which can play a major role in practice.
Let's look at an example:
My program needs a series of objects, such as apple, orange ..., To use them, we must generate them in the program based on user requirements and call the new operator one by one so that the client program will know the information of the corresponding class, the generated code is obviously not flexible enough. We can not use specific classes in the code, but simply describe what we need, and then we can get the object we want?
We all look at the design patterns, where many people advocate how they are doing well, and let's look at how to use them to solve the problem. If the goal is clear, let's see which one meets our requirements. The "design patterns" seem to have understood some of them, so let's see if we can "Get together? Well, our program considers how to create objects. The creation mode should meet the requirements. Then let's look at the "intention" section of each model. The first one seems to have hit the color. Abstract Factory. Let's take a look at it and "provide an interface for creating a series of related or mutually dependent objects without specifying their specific classes ", at least "no need to specify their specific classes" meets our requirements. Let's take a look at its structure:
Our problems don't seem to be so complicated. Only orange, apple, and so on (should be the product). They are obviously one type, all of which are fruit, we only need a factory that produces fruit. The inheritance hierarchy on the left is not. There is only one FruitFactroy. Don't worry about it. It's just practical.
The following things are clearly what we need:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> Public interface IFruit {} public class Orange: IFruit {public Orange () {Console. WriteLine ("An orange is got! ") ;}} Public class Apple: IFruit {public Apple () {Console. WriteLine (" An apple is got! ");}}What should we do with our FruitFactory? In the above structure diagram, it gives CreateProductA. Well, I will make orange, and there will be a CreateProductB. Can I make orange ??
public class FruitFactory{ public Orange MakeOrange() { return new Orange(); } public Apple MakeApple() { return new Apple(); }}
How can we use this factory? Let's write down the following code:
string FruitName = Console.ReadLine();IFruit MyFruit = null;FruitFactory MyFruitFactory = new FruitFactory();switch (FruitName){ case "Orange": MyFruit = MyFruitFactory.MakeOrange(); break; case "Apple": MyFruit = MyFruitFactory.MakeApple(); break; default: break;}
Compile and run the program, and enter what you want on the console. Immerse yourself in happiness.
-- Some materials are organized in online articles