Adapter Mode Discussion

Source: Internet
Author: User

Key Points

2.A ClassProgramThe interface design is not very convenient for a specific scenario;

2.There are many classes with common features, that is, there are common features, and they are not applicable to the same proprietary occasions;

2.The program design interface of a class can be converted into another interface, which is the adapter mode;

2.The adapter mode can be implemented through two methods: first, a new class is derived from the original class, and then a new consistent method is added to make the new class conform to the required interface; the second method is to combine objects, that is, the adapter class has a data member of the original type, and package it into another group of APIs. This method hides the interfaces of the original type;

2.The adapter mode, as a structural mode, features that each type has an adapter, whether derived or combined, is a type packaged into another type interface, rather than combining multiple types of interfaces into one interface, therefore, the adapter is a simple scenario in the structural mode.

Purpose

For example, for the winform control ListBox provided by C #, operations on the set data must be performed through the items attribute. For example, items. Add (), items. Clear (), and so on. For direct and convenient use, instead of using items, we can convert these interfaces into another set of interfaces in the adapter mode.

The adapter mode is similar to the commonly used adapter, such as the network adapter and keyboard adapter frequently used by computers. Also, for example, the original large-mouth connector keyboard and small-port interface use a transpose wiring, itself is a good example of the adapter.

Figure 1
1As shown in, an adapter is constructed based on two methods. It is called an Object Adapter through a combination of inherited and constructed class adapters. The advantage of Object Adapter is that it can hide interfaces of the original type, because it is better to hide interfaces of the original type as another interface group. However, when only some interfaces are encapsulated, the original interface type cannot be hidden.

Figure 2
As shown in figure 2, it is a further method for creating the adapter type. In some cases, it may be complicated. If you use the method shown in figure 1, it will be detrimental to the scalability of the system and the coupling will be too tight. Define an interface through an abstract method. This is the abstract layer. Then, the demowinform of the client program is coupled with the abstract layer, that is, the interface is directly used instead of the actual type.

In this way, it is very convenient to add a new adapter, and if the adapter changes, you do not need to modify the customer program. The client program eliminates coupling with actual types through the abstraction layer.

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.