适配器设计模式实现(Java)

编程入门 行业动态 更新时间:2024-10-25 00:24:51

<a href=https://www.elefans.com/category/jswz/34/1770284.html style=适配器设计模式实现(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)

本文发布于:2024-02-11 05:55:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1679500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适配器   模式   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!