Abstract Factory reflection)

Source: Internet
Author: User
Tags switch case

In my previous article (doubt? Improvement? From the simple factory to the factory method), this paper discusses in detail the evolution process of the simple factory to the factory method in the creation mode, and tries to combine the design of the factory method and. net reflection mechanism, improved a new type of factory-reflection factory, this is of course not my first, the classic petshop will have this factory figure. Based on the ideas in the previous article, this article tries to continue to improve the abstract factory through the evolution process from the factory method to the abstract factory. The ideas in this article only represent the author's views at that time, please kindly advise if you have any defect.

Factory Model

The previous article mentioned that simple factory and factory methods are actually the same thing. They have separated the customer's use of product functions from the responsibility for creating specific products, the difference is only the difference in their implementation methods. The factory method uses more elegant polymorphism to replace the switch case of the ugly... This article will not emphasize the differences in implementation, but more emphasize the commonality of design ideas between the two, this kind of commonality is collectively referred to as the factory model to further compare with the abstract factory.

Factory use, selection process

The use of the factory model is actually the process of product selection by the customer (the consumer of the product). For products with the same functions, the customer is more concerned with the differences between products, the function of the factory is to encapsulate the production process of the product and directly return the product as required by the customer. Note: The factory only hides the production process of the product, but does not deprive the customer of the right to choose. How does the customer's selection process be reflected? In a simple factory, the customer uses parameters to tell the factory what kind of product is needed. In the factory method, the customer replaces the product selection directly by choosing the factory, note that a factory has only one create method in the factory method. That is to say, if a factory is only responsible for producing one product, then you select the corresponding factory, which is equivalent to choosing the corresponding product. Even the improved reflection factory does not eliminate the choice of the product, but externalizes this choice (externalizing it into the configuration file to minimize the changes to the Code ). It can be said that the difference between products gives customers the right to choose. This right should not be replaced by factories. What is the value of the factory model? The answer is abstraction and encapsulation. The factory model minimizes the impact on known things that may be caused by different customer choices by replacing specific products with abstract products, this allows customers to rely on abstraction (more abstract and more stable) and encapsulate customer choices in one place to isolate dependencies with specific products.

Factory model and Abstract Factory

I have mentioned so many irrelevant things in front of this article to pave the way for better understanding of the following. OK, I should finally talk about the transformation from the factory model to the abstract factory, first, we will compare the two class diagrams:
Factory method)

Abstract Factory)

What are the differences?

The most obvious thing is that there is only one type of product in the class Relation Diagram of the factory method. They implement a unified interface, and the abstract factory has multiple classes of products, they implement different functions (multiple interfaces) respectively ). The second difference is that the number of methods in the factory itself is different. This difference is actually caused by the first point. The factory needs to have the function of producing different types of products, if the Abstract Factory Product function is simplified to one, it becomes a factory method.

Introduces the concept of category. category refers to the general term of a product with the same function.

Let's look at the selection process. In the factory method, the customer only cares about the differences between different products that implement the same functions. This is a one-dimensional selection process.

1 ifactory factory = new factorya (); // if the factory is selected, the product is selected.
2 iproduct producta = factory. Create (); // The factory has only one create method and can only produce one product

In the abstract factory, the customer must first consider which functions are required, and then consider the differences between products. That is to say, this is a two-dimensional selection process.

1 ifactory factory = new factorya (); // select a specific factory
2 iproduct producta = factory. createproducta (); // The factory has multiple create methods, one of which is selected here

Due to the increase in product categories, customers must consider product differences while considering product functional differences. This two-dimensional selection process is the essential difference between factory methods and abstract factories.

Take KFC and McDonald's for example. Suppose there is only one fast food restaurant called McDonald's, and the food (specific products) provided is hamburger, cola, and fries. They can satisfy your needs (abstract interface) so when you want to eat fast food, the only choice is what to eat. It is a one-dimensional choice. Now another fast food restaurant called KFC is opened, which also serves hamburgers, cola and fries, now, if you want to eat fast food, in addition to what you want to eat, you also need to consider where to eat-KFC or McDonald's? This is a two-dimensional choice. You can choose either horizontal or vertical to lock your desired product.

Introducing the concept of series, different products of the same category are called different series. For example, KFC and McDonald's are two different series.

Another consequence of the difference is that the difference between products (the difference between series) becomes the customer's secondary choice, the customer mainly focuses on the selection of functions (Category Selection ).

Let's take a look at the general design and implementation of the abstract factory with examples.


Customer Call Code

1 Export actfactory factory = new mcdfactory (); // select to go to McDonald's
2 ihamburger hamburger = factory. createhamburger (); // select a hamburger.

We can see that the choice between series is replaced by the choice of factory, and the choice between categories is actually the choice of different product categories in the factory. Is there a basis for such a design? Why don't we design the factory as a hamburger, cola, and fries factory? This is actually a misunderstanding that people who are new to abstract factories often fall into. The key to this problem lies in their needs! The requirement comes from the customer. The design of the factory depends on how the customer uses the product. In this example, hamburger, cola, and fries are designed as categories rather than series because they have different features for customers, such as Hamburg, fries can be used to quench the thirst, Cola can be used to quench the thirst, and any customer who wants to enter a store (either KFC or McDonald's) can get the above three features, which is the real demand of the customer! If the customer chooses to enter McDonald's, then he will never eat KFC's food. Here, you may need to think about it. Let's give another example to help you understand it. For example, in Starcraft games, there are three races in it, and three troops can be produced in each barracks. How can we select a category and series? Aside from the design of the game, from the user's point of view, we will still map the series to race, because after any player enters the game, all hope to be able to experience the different capabilities of the three different arms, rather than having a barracks capable of creating all three race-level soldiers. In turn, each time a user enters the game, he can only choose one race. If he chooses the Zerg family this time, then he will never generate a gunshots.

Finally, I will summarize the concepts mentioned above.

Category = interface = abstract Product = common functions of a specific product, which reflect the choice through a specific factory method.

Series = Abstract Factory = differences between different products of the same category, which are embodied by instantiating a specific factory.

Reflection mechanism implementation selection logic, about the factory

Some people have complaints, while others have complaints. People are rivers and lakes. How do you quit?

If there is a choice, there will be a factory, and there will be reflection from the factory. The choice is reflection. How do you design it?

Haha... Joke: in fact, the above-mentioned nonsense is used to demonstrate this point of view. reflection can be reflected if there is a choice. Let's see how I improved the factory method to a reflection factory. Since the factory method is a one-dimensional choice that can be reflected, the two-dimensional choice of the abstract factory is naturally more difficult, carefully observe the factory selection process.

1 Export actfactory factory = new mcdfactory (); // select to go to McDonald's
2 ihamburger hamburger = factory. createhamburger (); // select a hamburger.

It is found that the selection is for the factory and the method selection. However, the selection of internal methods in the factory is not applicable to the reflection mechanism, because different methods implement different functions, there is no uniform interface between each other. If we talk about reflection, reflection will naturally only be placed on the choice of the factory-they all inherit from the abstract factory. So we set up a reflection factory to dynamically generate the desired factory, that is, the factory. It's awkward to say, or look at the picture.

 

The newly added ffacloud is the reflection factory. Abstract Factory is an abstract product for the reflection factory. factorya and factoryb are the corresponding products.

1 <deleetask>
2 <add key = "factoryname" value = "factorya"/>
3 </appsettings>

Reflection Factory

1 public static ifacloud createfactory ()
2 {
3 // read the factory to be instantiated from the configuration file
4 string factoryname = configurationsettings. receivettings ["factoryname"]. tostring ();
5
6. abstractfactory factory;
7 factory = (abstractfactory) Assembly. Load ("Abstract Factory"). createinstance ("abstract_factory.improvefactory." + factoryname );
8 return factory;
9}

Customer use

1 Public void run ()
2 {
3 Export actfactory factory = ffacloud. createfactory (); // factory of the production factory, using the reflection mechanism
4 iproduct1 product1 = factory. createproduct1 ();
5 iproduct2 product2 = factory. createproduct2 ();
6
7 product1.dotask1 ();
8 product2.dotask2 ();
9}

Supplement

In addition to multi-series and multi-category applications, abstract factory also has a very important principle, that is, its series of products are always used together, only in this way can we better reflect the value of the abstract factory. In this regard, I will explain the similarities and differences between the two through comparison with the builder model in the next 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.