PackageShipeiqidemo;/** A common design pattern in Java: Adapter * Source: A subclass to implement the interface, then you have to implement the interface of all the abstract methods, sometimes the abstract method * a lot, but do not need to implement all, so at this time define an abstract class to implement this interface, and then through the subclass to inherit * In this abstract class, this abstract class is referred to as the adapter*/ Public classShipeiqidemo { Public Static voidMain (string[] args) {//Zhuozi zz = new Zhuozi (); //Zz.open (); //zz.close ();Windows W =NewZhuozi (); W.open (); W.close (); W.stop ();}}//write an interface with more abstract methodsInterfacewindows{ Public Abstract voidopen (); Public Abstract voidClose (); Public Abstract voidstart (); Public Abstract voidstop ();}//write an abstract class to implement this interface is the adapterAbstract classDoorImplementswindows{//all rewrite the abstract method inside windows, but the method body is empty, let subclass to implement Public voidopen () {}; Public voidclose () {}; Public voidstart () {}; Public voidstop () {};}//write a subclass to inherit doorclassZhuoziextendsdoor{ Public voidOpen () {System.out.println ("hello!!"); } Public voidClose () {System.out.println ("world!!"); }}
Adapter design patterns used in Java development