当前位置: 首页 > news >正文

VPS如何做网站服务器网站开发和网站制作的区别

VPS如何做网站服务器,网站开发和网站制作的区别,有什么搜图片的网站好,ks刷粉网站推广马上刷案例#xff1a;使用策略模式和工厂模式优化大量的if-else 原先代码如下#xff0c;有衣服、零食、蔬菜三种类型的商品#xff0c;并且每一类满减折扣不一样#xff0c;如果使用if-else#xff0c;代码会显得非常冗长#xff0c;并且大量的if-else很容易眼花导致维护代码…案例使用策略模式和工厂模式优化大量的if-else 原先代码如下有衣服、零食、蔬菜三种类型的商品并且每一类满减折扣不一样如果使用if-else代码会显得非常冗长并且大量的if-else很容易眼花导致维护代码时改错地方不利于后期维护和扩展。 public static double calculatePrice(String type, double price) {if (cloth.equals(type)) {if (price 1000) {return price * 0.7;} else if (price 800) {return price * 0.75;} else if (price 600) {return price * 0.8;} else if (price 500) {return price * 0.85;} else if (price 300) {return price * 0.88;} else if (price 200) {return price * 0.9;} else {return price;}} else if (snacks.equals(type)) {if (price 500) {return price * 0.8;} else if (price 300) {return price * 0.85;} else if (price 200) {return price * 0.88;} else {return price;}} else if (vegetable.equals(type)) {if (price 200) {return price * 0.8;} else if (price 100) {return price * 0.88;} else if (price 50) {return price * 0.95;} else {return price;}}return price;} 使用策略模式优化 废话不多说直接上代码 1、首先定义一个价格计算策略接口 public interface PriceCalculationStrategy {double calculatePrice(double price); }2、衣物价格计算策略实现类 public class ClothPriceCalculationStrategy implements PriceCalculationStrategy {Overridepublic double calculatePrice(double price) {if (price 1000) {return price * 0.7;} else if (price 800) {return price * 0.75;} else if (price 600) {return price * 0.8;} else if (price 500) {return price * 0.85;} else if (price 300) {return price * 0.88;} else if (price 200) {return price * 0.9;} else {return price;}} } 当然这里如果你不想使用大量的if-else也可以使用Map因为Map也是一种类似于策略模式的思想这里的Map是在静态代码块进行属性设置的优点如下 采用空间换取时间的思想后Map在程序启动之后即可预先赋值。当然不必担心内存溢出的问题自JDK 8以后方法区的实现类是元空间其内存存在于实际内存中而非JVM。因此new几个对象占用的空间犹如沧海一粟可以把key, value像枚举一样一行行写出来易于修改。 具体实现如下 public class ClothPriceCalculationStrategy implements PriceCalculationStrategy {private static final MapInteger, Double DISCOUNT_MAP new HashMap();static {DISCOUNT_MAP.put(1000, 0.7);DISCOUNT_MAP.put(800, 0.75);DISCOUNT_MAP.put(600, 0.8);DISCOUNT_MAP.put(500, 0.85);DISCOUNT_MAP.put(300, 0.88);DISCOUNT_MAP.put(200, 0.9);}Overridepublic double calculatePrice(double price) {for (int threshold : DISCOUNT_MAP.keySet()) {if (price threshold) {return price * DISCOUNT_MAP.get(threshold);}}return price;} } 3、零食价格计算策略实现类 public class SnacksPriceCalculationStrategy implements PriceCalculationStrategy {Overridepublic double calculatePrice(double price) {if (price 500) {return price * 0.8;} else if (price 300) {return price * 0.85;} else if (price 200) {return price * 0.88;} else {return price;}} } 4、 蔬菜价格计算策略实现类 public class VegetablePriceCalculationStrategy implements PriceCalculationStrategy {Overridepublic double calculatePrice(double price) {if (price 200) {return price * 0.8;} else if (price 100) {return price * 0.88;} else if (price 50) {return price * 0.95;} else {return price;}} } 5、测试通过new不同的策略传入价格即可计算。  public class test {public static void main(String[] args) {// ---通过new不同的策略实现类进行计算价格---// 衣物类满减策略PriceCalculationStrategy clothPriceCalculationStrategy new ClothPriceCalculationStrategy();System.out.println(clothPriceCalculationStrategy.calculatePrice(1738));// 食品类满减策略PriceCalculationStrategy snacksPriceCalculationStrategy new SnacksPriceCalculationStrategy();System.out.println(snacksPriceCalculationStrategy.calculatePrice(375));// 蔬菜类满减策略PriceCalculationStrategy vegetablePriceCalculationStrategy new VegetablePriceCalculationStrategy();System.out.println(vegetablePriceCalculationStrategy.calculatePrice(77));} } 当然我们可以使用工厂模式进一步优化 也就是通过工厂统一管理直接new 一个工厂传参即可直接上代码 1、创建价格计算工厂策略类 public class PriceCalculationStrategyFactory {private static final MapString, PriceCalculationStrategy strategyMap new HashMap();static {strategyMap.put(cloth, new ClothPriceCalculationStrategy());strategyMap.put(snacks, new SnacksPriceCalculationStrategy());strategyMap.put(vegetable, new VegetablePriceCalculationStrategy());}public double calculatePrice(String type, double price) {PriceCalculationStrategy strategy strategyMap.get(type);if (strategy ! null) {return strategy.calculatePrice(price);}return price;} } 2、测试通过new一个工厂传入类型和价格即可。  public class test {public static void main(String[] args) {PriceCalculationStrategyFactory factory new PriceCalculationStrategyFactory();double cloth factory.calculatePrice(cloth, 1738);double snacks factory.calculatePrice(snacks, 375);double vegetable factory.calculatePrice(vegetable, 77);System.out.println(满减计算后衣服价格 cloth);System.out.println(满减计算后零食价格 snacks);System.out.println(满减计算后蔬菜价格 vegetable);} } 总结 对于大量的if-else特别是大量的嵌套的if-else可以用策略模式工厂模式优化少量的if-else就没必要了使用设计模式将会增加代码量但是解耦性特别强代码更加清晰易懂而且具有更强的扩展性和维护性。策略模式将不同的责任分配到不同的类中实现单一职责原则提高代码的内聚性。 ps以下是我整理的java面试资料感兴趣的可以看看。最后创作不易觉得写得不错的可以点点关注 链接https://www.yuque.com/u39298356/uu4hxh?# 《Java面试宝典》
http://www.pierceye.com/news/400623/

相关文章:

  • html中文美食网站营销型网站维护费用
  • 电商网站建设课设用什么软件制作网站
  • 杭州手机网站wordpress随机调用页面
  • html5网站编写长网页网站
  • 订餐网站系统建设方案建一个网上商城需要多少钱
  • 手机网站asp付费抽奖网站怎么做
  • 国际网站哪里做vs2010 c 建设网站
  • 企业网站更新什么内容永城做网站
  • wordpress 众筹网站模板html5风格网站特色
  • 服装设计参考网站重庆景点排名
  • 网至普的营销型网站建设扬州网站商城建设价格表
  • 成品网站价格表简答网站内容建设的时候内链重要性
  • 视频链接生成器某网站搜索引擎优化
  • flash网站案例vi设计是设计什么
  • ip查询网站备案查询企业网络营销推广平台
  • 多城市网站建设免费制作小程序的平台
  • 郑州网站建设出名吗?wordpress获取登录密码
  • 网站建设论文的开题报告重庆市工程建设信息
  • 商务网站设计方案门户网站的优点
  • 河津网站制作wampserver做的网站
  • 洛阳专业网站设计开发制作建站公司零陵区住房和城乡建设局网站
  • 在哪里可以学做网站游戏开发大亨下载
  • 找人做ps的网站无锡 做公司网站
  • 云速建站可以建个人网站吗wordpress仿站难吗
  • 如何取外贸网站域名凡科h5制作教程
  • 蜘蛛不抓取网站的原因中山h5网站建设
  • 百度免费推广网站建网站用的免费软件
  • 网站建设西安哪里好广州做企业网站的公司
  • 汉中市网站建设爱墙 网站怎么做
  • 失物招领网站开发项目需求分析搭建外文网站