Java design mode-----Adapter mode

Source: Internet
Author: User


Problems in the real world

There is a socket, this socket is three mouth, but we have only two tentacles of the plug, a stupid method is to directly force the two tentacles bent, inserted into (this kind of thing I have done), there is another way is to find a conversion head.

Look at the following figure is probably clear, it is obvious that the existing system is difficult to match with vendors, how to do it? Well, the adapter is handy.

Defined

Transforms the interface of a class into another interface that the customer expects. Adapters allow classes that are incompatible with the original interface to work together.

Then the above question

This mode allows the interface to be transformed by creating an adapter to make incompatible interfaces compatible. This allows the customer to decouple from the implemented interface. If, after a period of time, we want to change the interface, the adapter can encapsulate the changed parts, and the customer does not have to follow the changes each time to deal with the different interfaces.

The class diagram is as follows:


For example, the above adapter class diagram contains the OO design principles: Using object combinations to modify the interface including the adapter: This approach has the added advantage that any subclass of the adapter can be used with an adapter

Code specific implementation

1, first we need a target interface, that is, we mentioned above the three port plug

Target targets

package  Adapter;public  interface  threeinterface  { public  void  Offerpowerforthree (); //three-port plug has a  function to provide power to three tentacles of the plug}  

2, next we need to be the adapter, that is, two tentacles of the plug

 PackageAdapter; Public  interface twointerface {     Public voidOfferpowerfortwo ();//Two-port plug has afunction, capable of providing power to two tentacles plugs: Package Adapter;public class Realtwo implements twointerface{@Override public void Offerpower ForTwo () {//TODO auto-generated method Stub System.out.println ("I can supply power to the two-tentacles plugs!"    "); }}

3. Need an Adapter

 PackageAdapter; Public  class threetotwoadapter implements threeinterface {Twointerface Twointerface; PublicThreetotwoadapter (Twointerface twointerface) { This. twointerface=twointerface; }@Override     Public voidOfferpowerforthree () {//TODO auto-generated method stubTwointerface.offerpowerfortwo (); }}

4, need a scene, under this scenario, is not an adapter can be used directly by the resources, need to convert adapters, in the above example, there is a two tentacles plug 30 useless can use the two-port socket.

package  Adapter; public  class  test  { //Now this fast place has only three ports in the  socket, but the plug is two tentacles and requires an adapter public STA tic void Main (String []args) {//1 , familiar with the definition of a two-horn plug, which was originally brought Twointerface twointerface=new realtwo (); 2, define an adapter, so that three port sockets can be two tentacles of the plug to provide power threeinterface threeinterface=new threetotwoadapter (twointerface); 3 , providing power threeinterface.offerpowerforthree (); }}

Class Adapter Mode

The above mentioned actually become the object adapter pattern, there is also a kind of called Class Adapter mode, below to look at the class adapter mode:

Class diagram



Class diagrams and object adapters are a bit similar, the only difference is that class adapters inherit the target and Adaptee

Advantages and disadvantages of adapter mode

Advantages

    • Decouple the target class from the adapter class by introducing an adapter class to reuse the existing adapter class without modifying the original code.
    • It increases the transparency and reusability of the class, encapsulates the specific implementation in the adapter class, is transparent to the client class, and improves the reusability of the adapter.
    • Flexibility and scalability are very good, by using the configuration file, you can easily replace the adapter, you can not modify the original code based on the addition of new adapter classes, fully comply with the "open and close principle."

The class adapter pattern also has the following advantages:

    • Because the adapter class is a subclass of the adaptive class, it is possible to displace some of the adapters ' methods in the adapter class, making the adapter more flexible.

The object adapter pattern also has the following advantages:

    • An object adapter can adapt multiple different adapters to the same target, that is, the same adapter can match the matching class and its subclasses to the target interface.

Disadvantages

The disadvantages of the class adapter pattern are as follows: for Java, C # and other languages that do not support multiple inheritance, a maximum of one adapter class at a time, and the target abstract class can only be abstract class, cannot be a concrete class, its use has some limitations, it is not possible to match an adapter class and its subclasses to the target interface.

The disadvantages of the object adapter pattern are as follows: It is not easy to displace the method of the adapter class compared to the class adaptor pattern. If we must replace one or more methods of the adaptive class, we have to do a subclass of the appropriate ligand class, replace the method of the adaptive class, and then adapt the sub-class of the adaptive class as the true adapter, and the process is more complicated.

Java design mode-----Adapter mode

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.