适配器设计模式实现(Java)"/>
适配器设计模式实现(Java)
适配器设计模式设计思路: 例如:凳子的功能具有:setup功能椅子的功能具有:setup功能,leanon功能沙发的功能具有:来自凳子的setup功能,leanon功能,lie功能创建一个更舒适的产品:具有以上的功能,也可能是所有也可能是一部分,也可能超过这些功能有自己独有的功能创建对象为:床,按摩椅采用适配器设计模式定义凳子接口类,沙发接口类,椅子实现凳子接口类,并增加自有功能的方法,椅子也可以实现沙发接口类,只实现自有的功能床实现沙发的接口类,并可以实现自己自有的睡觉功能按摩椅实现凳子和沙发的功能,并增加自有的功能
废话不多少,直接上代码:
/*** Created with IntelliJ IDEA.* Description: 椅子接口类,继承类凳子类* 不仅有父类的坐的功能,还有自己的靠的功能** @PackageName com.feixiang.platform.stu.design.adapter* @Author: ldwtxwhspring* @Date: 2020-05-25 下午11:00* @Version: 1.0*/ public interface Chair extends Stool {public void leanon(); }
/*** Created with IntelliJ IDEA.* Description: 沙发接口类** @PackageName com.feixiang.platform.stu.design.adapter* @Author: ldwtxwhspring* @Date: 2020-05-25 下午11:02* @Version: 1.0*/ public interface Sofa {//靠public void leanon();//躺public void lie();}
/*** Created with IntelliJ IDEA.* Description: 邓牌沙发** @PackageName com.feixiang.platform.stu.design.adapter* @Author: ldwtxwhspring* @Date: 2020-05-25 下午11:06* @Version: 1.0*/ public class DengPaiSofa implements Sofa{@Overridepublic void leanon() {System.out.println("邓牌沙发靠着都能打瞌睡");}@Overridepublic void lie() {System.out.println("邓牌沙发躺着不做梦,15秒内能睡着");} }
/*** Created with IntelliJ IDEA.* Description: TODO** @PackageName com.feixiang.platform.stu.design.adapter* @Author: ldwtxwhspring* @Date: 2020-05-25 下午11:36* @Version: 1.0*/ public class LiPaiChair extends SofaAdapter{@Overridepublic void lie(){System.out.println("我选择性继承适配器拥有的功能吧");} }
/*** Created with IntelliJ IDEA.* Description: TODO** @PackageName com.feixiang.platform.stu.design.adapter* @Author: ldwtxwhspring* @Date: 2020-05-25 下午11:33* @Version: 1.0*/ public abstract class SofaAdapter implements Sofa{@Overridepublic void leanon() {System.out.println("我可以靠着");}@Overridepublic void lie() {System.out.println("我可以躺着");} }
/*** Created with IntelliJ IDEA.* Description: 沙发床** @PackageName com.feixiang.platform.stu.design.adapter* @Author: ldwtxwhspring* @Date: 2020-05-25 下午11:21* @Version: 1.0*/ public class SofaBed implements Stool,Chair{StoolAdapter stoolAdapter;@Overridepublic void setup() {System.out.println("可以当板凳坐");stoolAdapter = new StoolAdapter();stoolAdapter.other();System.out.println("展开后有惊喜哦!");}@Overridepublic void leanon() {System.out.println("来自凳子的所有特性");} }
/*** Created with IntelliJ IDEA.* Description: 凳子接口类* 只要实现了凳子类就必须要能坐** @PackageName com.feixiang.platform.stu.design.adapter* @Author: ldwtxwhspring* @Date: 2020-05-25 下午10:57* @Version: 1.0*/ public interface Stool {public void setup(); }
/*** Created with IntelliJ IDEA.* Description: 凳牌实现** @PackageName com.feixiang.platform.stu.design.adapter* @Author: ldwtxwhspring* @Date: 2020-05-25 下午11:13* @Version: 1.0*/ public class StoolAdapter implements Stool{Sofa sofa;/*** 重写无参构造* */public StoolAdapter(){this.sofa = new DengPaiSofa();}@Overridepublic void setup() {System.out.println("当凳子坐");}/*** 具有沙发的特性* */public void other(){sofa.leanon();sofa.lie();}}
测试一下吧:
public class SofaBedTest {public static void main(String[] args) {SofaBed sofaBed = new SofaBed();sofaBed.setup();sofaBed.leanon();LiPaiChair liPaiChair = new LiPaiChair();liPaiChair.leanon();liPaiChair.lie();System.out.println("是不是不需要强制实现了");} }
代码虽然粗鲁了点,思路还是很清晰的,大家可以根据自己的场景来应用适配器模式会有意想不到的收获
更多推荐
适配器设计模式实现(Java)
发布评论