Java design patterns of the three: adapter mode

introduction

This paper describes the content of the Adapter pattern. Defined primarily relates to an adapter mode, and code examples are summarized usage scenarios.
springboot @Value Could not resolve placeholder application.properties

  • Adapter mode Introduction
  • The sample code
  • to sum up

I. Introduction adapter mode

We first give the definition of the adapter mode, the adapter so that we have a perceptual awareness.

Converting the interface of a class as a client interface to expect another, so that the two classes to work with because of the interface mismatch otherwise unable to work together

The above is the original definition of adaptation mode, foreigners things translated on the inevitably some statements going around feeling. According to their own understanding, by definition, the adapter is compatible with previously incompatible things sides. For chestnuts, if we used the Hong Kong version of the Iphone, then there experience. Hong Kong version of the Iphone charging plug is a three-plug, plug the charger but three in the Mainland is not take. As follows:
Here Insert Picture Description
if we want to use the Hong Kong version of the charger will need a converter as shown in the following figure, through this converter to convert into a three-prong plug in line with the habits of the mainland two-phase plug, so that we can stay in the play it with a new phone.
Here Insert Picture Description
By chestnut above, we can know the Adapter pattern is actually a converter, it can not directly use the original target by the middle of the converter, the object can be turned into mutually compatible use, then the code corresponding to the world of how we should use the adapter pattern? we continue to look down.

Second, the sample code

Composition point adapter mode as follows:

  • Customer (Client): Client class;
  • Target Interface (Target): Interface customers want to use. Target may be specific or abstract class, an interface may be. As above Target Port chestnuts here is a three-phase version of the plug;
    fitter (Adaptee): adapter required classes or categories are adapted, as in the above two-phase plug.
    Adapter (Adapter): by wrapping the object that needs adapted to convert the interface into the original target interface.

Here Insert Picture Description
The code is written to the adapter, there are two modes is a mode-based class adapter, the adapter and the other is based on the object model. This article is based on the way an object adapter.
Goal-phase charger

public interface ThreePhaseCharger {
    void threePhaseCharger ();
}

Fitter (Adaptee)

public interface TwoPhaseCharger {
    void twoPhaseCharger ();
}

Fitter (Adaptee) to achieve

public class TwoPhaseChargerImpl implements TwoPhaseCharger  {
    @Override
    public void twoPhaseCharger () {
        System.out.println("start to two phase charger!");
    }
}

adapter

public class ChargerAdapter implements ThreePhaseCharger {
    TwoPhaseCharger  twoPhaseCharger;
	
	public ChargerAdapter(TwoPhaseCharger  twoPhaseCharger){
		this.twoPhaseCharger = twoPhaseCharger;
	} 

	@Override
    public void threePhaseCharger () {
        twoPhaseCharger.twoPhaseCharger ();
    }
}

Client customers

public class Client {
    public static void main(String[] args) {
	    TwoPhaseChargerImpl  twoPhaseCharger = new TwoPhaseChargerImpl();
	    ChargerAdapter chargerAdapter = new ChargerAdapter (twoPhaseCharger );
	    chargerAdapter.threePhaseCharger ();

}
}

Third, the summary

These are the Adapter pattern of relevant content, we make a brief summary of it on the adapter mode.
1, when we want to use an existing interface, but it does not comply with existing interface specification data, making it impossible to access directly, then we can create an adapter will be able to go directly to the access methods in this class;
2, when docking business data between different systems, like if you want to call itself using the same system interface interfaces to other systems, we need to be converted by the adapter layer;

Published 88 original articles · won praise 49 · Views 100,000 +

Guess you like

Origin blog.csdn.net/Diamond_Tao/article/details/102409260