C # Design Model

Source: Internet
Author: User

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

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.