Dependency injection events (4)

Source: Internet
Author: User

The related design mode of incomplete replica set...

Composite Mode

Extend the analogy of an electrical device. Now, if you want the ups to be connected not only to the power supply, but also to the fan and terminal, but the UPS has only two power supply outlets. How can this problem be solved?

We can connect a source extension to the ups. In this way, the fan, cell phone, and computer can all be plugged into the extended long distance socket at the same time. In this example, the source extension is similar to composite pattern ), the reason is that the power source extension itself can be connected to the extension cards of other different brands (this is because the sockets all use the same interface ), in this case, we will continue.

Er .... There is a small problem in the metaphor of extended growth: It looks like a serial connection on the external server, which is easy to be confused with the decorator mode. In fact, these two design patterns are indeed similar in the structure. The following figure shows the structure of the composite mode.

From this perspective, we can see that the composite mode is actually a dynamic component that shows the relationship between the whole-body-containing (whole-part. Each vertex (leaf) on the vertex has the same interface, and each vertex can contain multiple subvertex; just like the final structure of the case, each item can have as many items as possible. In contrast, the decorator mode allows the operator to grow as long as the operator is being called, but in fact it adds the extra repair.

Adapter Mode

When your phone has no power and needs to be powered, it will not work even if there is a power extension, the reason is that the power of the phone is not the 110 volt AC power of the household computer. At this time, we usually use an adapter attached to the phone to insert the electrical source plug-in of the browser into the electrical source outlet on the wall, then, Connect the other end of the robot to the phone. In this way, an interface is converted into another specification, which is called the adapter pattern ). The following figure shows the structure of the adapter mode.

We still use the previous logging examples to describe. Instead of using our own logging APIs, we use third-party components directly. However, it is likely that another logging component will be used in the future, so it is decided to use the adapter mode to keep your program running. First, you must first export the interface of the logging API so that the application can only upload logs to this interface. This interface only defines a daily logon method, called log, which is used to test the following program snippets.

public interface ILogger{    void Log(string msg);}


Then design the adapter type. This type of method must be implemented as the ilogger interface and called by third-party components in the log method. Program example:


Public class commonlogger: ilogger {private thirdpartylogger logger = new thirdpartylogger (); Public void log (string MSG) {logger. writeentry (MSG); // notify method for calling third-party components. }}


In this case, if another logging component needs to be used in the future, the variable handler modified by the program is limited to the commonlogger type.

Factory Mode

Chapter 1 has mentioned that every time we use the new operator in a program to create a different runtime user, our program was connected to that kind of Fixed sequence during the runtime. In fact, there is still a need to use new to create objects: the C # constructor name category is a type of alias and cannot be named arbitrarily; when there are several redundant (overloaded) constructor libraries, just pull them into the constructor library, sometimes it's hard to understand the meaning of a program. For example:


var user1 = new User("Mike", 101, true);var user2 = new User("Jane", 102, flase);


It is not as clear as the following program:


var user1 = UserFactory.CreateAdministrator("Mike", 101);var user2 = UserFactory.CreateDomainUser("Jane", 102);


Here, userfactory is the role of an object worker. It is a static object, and the only task is to generate a specific object. The program example is as follows.


Public static class userfactory {public user createadministrator (string name, int ID) {// omitted} public user createdomainuser (string name, int ID) {// omitted }}


Generally, the factory mode refers to various types of workers that can generate objects. There are usually three modes: Factory method, simple factory) and Abstract Factory (Abstract worker ). Userfactory is a simple factory.


Note:We do not know DI at all, or do not think Di is necessary, however, it is expected that the Program program should not be too similar to the specific practice (rather than simply creating an object). At this time, the factory mode is usually a worthwhile solution.


In actual factory method mode, the abstract method of creating an object is usually defined in a base class (called the "worker method 」), then, each sub-class will implement the method. If you change the previous userfactory to the factory method for implementation, the following table describes its structure.

Abstract Factory is a little more complex than the previous two working modes. It is used to establish a multi-family correlated or dependent object, and does not have to indicate the specific type of the object. In this mode, an object creation method is defined as an interface, representing an abstract worker. Then, you can create multiple types to implement the interface, and these types of roles are like real-world engineers, in other words, each worker method is a production item in a real worker. When an object of the family needs to be created on the terminal, an object is generated using a concrete factory. In addition, the Representative workers all implement the same interface, so the slave end can even be switched to different workers during the busy hours, to create a set of related objects.

If you are like me, you often mix these three factory modes, I found that refactoring to patterns has a slightly optimized structure in 6.2 of this article. As shown in the following example, the indicates the function of creating an object. When you forget about it next time, you may wish to take a look at the bottom of the story, which may help you recall the difference between them.

OK! The design mode section briefly describes this. If you encounter other models in the later chapter, it will also introduce (such as strategy, repository, service locator, and so on ).

「 I used to at this crossroads: I tried to learn different models and wanted to be a better software design. But now, in order to become a more elegant software design, I must reduce the reliance on the opposite model. 」
-- Joshua kerievsky. Author of refactoring to patterns

Waiting for renewal ....

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: 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.