Adapter mode (Adapter): transforms the interface of one class into another interface that the customer wants.
The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.
The following is the sample code:
/** * Adapter Mode * @author Admin * */public class Adapter {public static void start (Powera p) { //Only accept call to interface A, this will use POWERADAPT ER adapter to allow interface B to also call P.chong ();} public static void Main (string[] args) {//TODO auto-generated method Stubstart (New Poweraimpl ()); Start (new Poweradapter (New Powerbimpl ()));}} Class Poweradapter implements powera{ //In order to implement Interface B can be converted into interface a called private Powerb p1;public Poweradapter (Powerb p) {p1 = P;} @Overridepublic void Chong () {//TODO auto-generated method Stubp1.check ();}} Interface powera{public void Chong ();} Class Poweraimpl implements powera{@Overridepublic void Chong () {System.out.println ("Powera charging");}} interface powerb{public void check (); Class Powerbimpl implements powerb{@Overridepublic void Check () {System.out.println ("Powerb charging Check");}}
Example results:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java design mode-Adapter mode