Coordination of incompatible structures-adapter mode (4)

Source: Internet
Author: User

9.6 default Adapter

Default adapter ModeIt is a variant of the adapter mode and is widely used. The default adapter mode is defined as follows:

Default adapter pattern: When you do not need to implement all the methods provided by an interface, you can first design an abstract class to implement this interface, and provide a default implementation (empty method) for each method in the interface, then the subclass of this abstract class can selectively overwrite some methods of the parent class to achieve the demand, it is applicable when you do not want to use all the methods in an interface, also known as the single interface adapter mode.

The default adapter mode structure is 9-7:

 

Figure 9-7 default adapter mode structure

The default adapter mode contains the following three roles:

● Serviceinterface (adapter Interface ):It is an interface, which usually declares a large number of methods.

● Abstractserviceclass (default adapter class ):It is the core class of the default adapter mode and implements the methods declared in the serviceinterface interface in the form of null methods. It is usually defined as an abstract class because it does not make any sense to instantiate it.

● Concreteserviceclass (specific service class ):It is a subclass of the default adapter class. Before the adapter is introduced, it needs to implement the adapter interface. Therefore, it needs to implement all the methods defined in the adapter interface, some unused methods have to provide empty implementations. With the default adapter, you can directly inherit the adapter class and selectively overwrite the methods defined in the adapter class as needed.

The default adapter mode is widely used in Java. AWT. event, such as windowadapter, keyadapter, and mouseadapter. In Java, we can use two methods to implement the window event processing class. One is to implement the windowlistener interface, the other is to inherit the windowadapter class. If the first method is used to directly implement the windowlistener interface, the event processing class needs to implement the seven methods defined in this interface. For most of the requirements, you may only need to implement one or two methods, other methods do not need to be implemented, but because of the language features, we have to provide a simple implementation for other methods (usually empty implementation), which brings trouble to use. The default adapter mode can be used to solve this problem. In JDK, an adapter-type windowadapter is provided to implement the windowlistener interface, this adapter class provides an empty implementation for each method in the interface. In this case, the event processing class can inherit the windowadapter class, without implementing every method in the interface. As shown in 9-8:

 

Figure 9-8 Structure of windowlistener and windowadapter

 

9.7 adapter mode Summary

The adapter mode converts an existing interface to the interface expected by the customer class to achieve reuse of the existing class. It is a design mode that is frequently used, it is widely used in software development and also uses the adapter mode in spring and other open-source frameworks and driver designs (such as database drivers in JDBC.

 

1. Main advantages

Both the Object Adapter mode and the class adapter mode have the following advantages:

(1)Decouples the target class and the adaptation class.By introducing an adapter class to reuse the existing adapter class, you do not need to modify the original structure.

(2) Added class transparency and reusability.The specific business implementation process is encapsulated in the adapter class, which is transparent to the client class and improves the reusability of the adapter, the same adapter class can be reused in multiple different systems.

(3)High flexibility and scalabilityBy using the configuration file, you can easily change the adapter, or add a new adapter class without modifying the original code, fully complying with the "Open and Close principle ".

Specifically, the class adapter mode has the following advantages:

Because the adapter class is a subclass of the adapter classYou can replace some adaptors In the adapter class.Makes the adapter more flexible.

The Object Adapter mode also has the following advantages:

(1) An Object AdapterMultiple different adaptors can adapt to the same target.;

(2)It can adapt to a subset of an adapter.Because the adapter is associated with the adaptation, according to the "Lee's replacement principle", the Child class of the adapter can also be adapted through the adapter.

 

2. Main disadvantages

Class adapter ModeDisadvantages:

(1) For languages that do not support multi-class inheritance such as Java and C #, only one adaptor class can be adapted at a time,Cannot adapt to multiple adaptors at the same time;

(2)The adapter class cannot be the final class.For example, in Java, it cannot be a final class, and in C # It cannot be a sealed class;

(3) in Java, C #, and other languages,The target abstract class in class adapter mode can only be an interface, not a classIts usage has certain limitations.

Object Adapter ModeDisadvantages:

Compared with the class adapter mode,It is troublesome to replace the adapter class in the adapter.. If you must replace one or more methods of the adapter class, you can first create a subclass of the adapter class and replace the methods of the adapter class, then, the sub-classes of the adaptor class are used as the real adaptors to adapt. The implementation process is complicated.

 

3. Applicable scenarios

You can consider using the adapter mode in the following situations:

(1) The system needs to use some existing classes, and the interfaces of these classes (such as method names) do not meet the requirements of the system, or even do not have the source code of these classes.

(2) create a reusable class for some classes that are not highly correlated with each other, including some classes that may be introduced in the future.

 

Exercise

Sunny software company OA system needs to provide an encryption module that encrypts user confidential information (such as passwords and mailboxes) and then stores them in the database. The system has defined database operation classes. To improve development efficiency, we need to reuse existing encryption algorithms. These algorithms are encapsulated in classes provided by third parties, and some do not even have source code. Design the encryption module in the adapter mode to reuse the third-party encryption method without modifying the existing classes.

 

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.