In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要介绍"Java开闭原则实例分析"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"Java开闭原则实例分析"文章能帮助大家解决问题。
定义
开闭原则( Open Close Principle ),又称为OCP原则,即一个软件实体如类,模块和函数应该对扩展开放,对修改关闭。其中,对扩展开放是针对提供方来说的,对修改关闭是针对调用方来说的。
案例需求
购买东西的时候,根据支付类型的不同使用不同的方式进行支付,当类型为1时,使用微信支付;当类型为2时,使用支付宝支付
方案一
定义支付类型
/** * 支付类型 * @author:liyajie * @createTime:2022/2/7 10:21 * @version:1.0 */public class PayType { int type;}
定义微信支付类
/** * 微信支付类 * @author:liyajie * @createTime:2022/2/7 10:46 * @version:1.0 */public class WxPay extends PayType{ WxPay(){ super.type = 1; }}
定义支付宝支付类
/** * 支付宝支付类 * @author:liyajie * @createTime:2022/2/7 10:46 * @version:1.0 */public class ZfbPay extends PayType{ ZfbPay(){ super.type = 2; }}
定义支付操作类
/** * @desc: * @author:liyajie * @createTime:2022/2/7 10:41 * @version:1.0 */public class Pay { public void pay(PayType s) { if (s.type == 1) { wxPay(s); } else if (s.type == 2) { zfbPay(s); } } // 微信支付 public void wxPay(PayType r) { System.out.println(" 正在微信支付中---支付成功 "); } // 支付宝支付 public void zfbPay(PayType r) { System.out.println(" 正在支付宝支付中---支付成功"); }}
定义测试类
/** * 测试类1 * @author:liyajie * @createTime:2022/2/7 10:48 * @version:1.0 */public class Test1 { public static void main(String[] args) { new Pay().pay(new WxPay()); new Pay().pay(new ZfbPay()); }}执行结果
方案二
定义支付类型基类
/** * 支付类型基类 * @author:liyajie * @createTime:2022/2/7 10:21 * @version:1.0 */public abstract class PayTypeNew { int type; public abstract void pay();}
定义微信支付类
/** * 微信支付类 * @author:liyajie * @createTime:2022/2/7 10:46 * @version:1.0 */public class WxPayNew extends PayTypeNew{ WxPayNew(){ super.type = 1; } @Override public void pay() { System.out.println(" 正在微信支付中---支付成功 "); }}
定义支付宝支付类
/** * 支付宝支付类 * @author:liyajie * @createTime:2022/2/7 10:46 * @version:1.0 */public class ZfbPayNew extends PayTypeNew{ ZfbPayNew(){ super.type = 2; } @Override public void pay() { System.out.println(" 正在支付宝支付中---支付成功"); }}
定义支付操作类
/** * 支付操作类 * @author:liyajie * @createTime:2022/2/7 10:41 * @version:1.0 */public class PayNew { public void pay(PayTypeNew payTypeNew) { payTypeNew.pay(); }}
定义测试类
/** * 测试类2 * @author:liyajie * @createTime:2022/2/7 10:48 * @version:1.0 */public class Test2 { public static void main(String[] args) { new PayNew().pay(new WxPayNew()); new PayNew().pay(new ZfbPayNew()); }}执行结果
对比分析
方案一,未遵守开闭原则,耦合较高,如果需求需要扩展,比如再增加一个信用卡支付,那么需要改的地方较大,风险较高
方案二,遵守开闭原则,耦合低,扩展方便,并且也提高了代码的复用性,维护起来也很方便
关于"Java开闭原则实例分析"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.