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

对网站内容建设的建议网络营销软件网站

对网站内容建设的建议,网络营销软件网站,wordpress网站访问验证码,铜川新区网站建设招标文章目录 前言一、解释器模式二、最佳实践总结 前言 本节给大家讲一下设计模式中的解释器模式,并结合实际业务场景给大家讲解如何使用~ 所有案例代码主要以Java语言为主, 好了, 废话不多说直接开整~ 一、解释器模式 解释器模式(Interpreter Pattern)…

文章目录

  • 前言
  • 一、解释器模式
  • 二、最佳实践
  • 总结

前言

本节给大家讲一下设计模式中的解释器模式,并结合实际业务场景给大家讲解如何使用~

所有案例代码主要以Java语言为主, 好了, 废话不多说直接开整~

一、解释器模式

解释器模式(Interpreter Pattern)是一种行为型设计模式,它定义了一种语言的语法表示,并定义了一个解释器来解释这种语言中的表达式。

通过使用解释器模式,可以轻松地扩展和修改语言的语法,从而使其更加灵活。

该模式的结构包含以下组件:

  • 抽象表达式(AbstractExpression):定义了一个解释器中的抽象操作,每个具体的解释器都实现了该抽象操作。
  • 终结符表达式(TerminalExpression):定义了一个解释器中的终止操作,它不能再进行解释。
  • 非终结符表达式(NonterminalExpression):定义了一个解释器中的非终止操作,它可以递归调用其他解释器进行解释。
  • 环境(Context):保存了解释器需要的一些全局信息。

下面我们举一个大家日常开发中最常用的定时任务的例子:

// 抽象表达式
// 抽象表达式接口
public interface Expression {boolean interpret(String expression);
}// 秒表达式
public class SecondExpression implements Expression {@Overridepublic boolean interpret(String expression) {// 解析表达式,判断秒数是否匹配return true; // 返回是否匹配}
}// 分钟表达式
public class MinuteExpression implements Expression {@Overridepublic boolean interpret(String expression) {// 解析表达式,判断分钟数是否匹配return true; // 返回是否匹配}
}// 小时表达式
public class HourExpression implements Expression {@Overridepublic boolean interpret(String expression) {// 解析表达式,判断小时数是否匹配return true; // 返回是否匹配}
}// 日表达式
public class DayOfMonthExpression implements Expression {@Overridepublic boolean interpret(String expression) {// 解析表达式,判断日期是否匹配return true; // 返回是否匹配}
}// 定时任务表达式
public class CronExpression implements Expression {private Expression secondExpression;private Expression minuteExpression;private Expression hourExpression;private Expression dayOfMonthExpression;public CronExpression(Expression secondExpression, Expression minuteExpression,Expression hourExpression, Expression dayOfMonthExpression) {this.secondExpression = secondExpression;this.minuteExpression = minuteExpression;this.hourExpression = hourExpression;this.dayOfMonthExpression = dayOfMonthExpression;}@Overridepublic boolean interpret(String expression) {String[] fields = expression.split(" ");// 解析定时任务表达式,判断各个字段是否匹配boolean matchSecond = secondExpression.interpret(fields[0]);boolean matchMinute = minuteExpression.interpret(fields[1]);boolean matchHour = hourExpression.interpret(fields[2]);boolean matchDayOfMonth = dayOfMonthExpression.interpret(fields[3]);return matchSecond && matchMinute && matchHour && matchDayOfMonth;}
}public class Client {public static void main(String[] args) {// 创建表达式对象Expression second = new SecondExpression();Expression minute = new MinuteExpression();Expression hour = new HourExpression();Expression dayOfMonth = new DayOfMonthExpression();Expression cron = new CronExpression(second, minute, hour, dayOfMonth);// 判断定时任务是否应该被执行String expression = "0 0 1 * *"; // 每月1日执行任务boolean match = cron.interpret(expression);if (match) {System.out.println("定时任务执行中...");} else {System.out.println("定时任务未开始...");}}
}

当定时任务表达式为0 0 1 * *时,表示该定时任务在每个月的1日执行。如果当前日期是1日,那么match变量的值将为true,表示该定时任务应该被执行。否则,match变量的值将为false,表示该定时任务还未开始执行。表达式内部具体规则逻辑可以自行实现,这里只是为了方便演示~

二、最佳实践

在电商平台中,可以使用解释器模式来实现一个基于规则的折扣系统。该系统可以根据用户的购物车内容和优惠规则来计算出最终的折扣金额。

下面是一个示例,展示了如何使用解释器模式来实现一个基于规则的折扣系统:

// 抽象表达式
interface DiscountExpression {double interpret(ShoppingCart cart);
}// 终结符表达式:商品价格
class ItemPrice implements DiscountExpression {private String itemId;public ItemPrice(String itemId) {this.itemId = itemId;}public double interpret(ShoppingCart cart) {double price = 0;for (CartItem item : cart.getItems()) {if (item.getItemId().equals(itemId)) {price += item.getPrice() * item.getQuantity();}}return price;}
}// 终结符表达式:折扣价格
public class DiscountPrice implements  DiscountExpression {private double discount;public DiscountPrice(double discount) {this.discount = discount;}@Overridepublic double interpret(ShoppingCart cart) {double price = 0;for (CartItem item : cart.getItems()) {price += item.getPrice() * item.getQuantity() * discount;}return price;}
}// 终结符表达式:购物车总价
class CartTotal implements DiscountExpression {public double interpret(ShoppingCart cart) {double total = 0;for (CartItem item : cart.getItems()) {total += item.getPrice() * item.getQuantity();}return total;}
}// 非终结符表达式:满减
class Discount implements DiscountExpression {private DiscountExpression condition;private DiscountExpression action;public Discount(DiscountExpression condition, DiscountExpression action) {this.condition = condition;this.action = action;}public double interpret(ShoppingCart cart) {if (condition.interpret(cart) > 0) {return action.interpret(cart);}return 0;}
}// 环境:购物车
class ShoppingCart {private List<CartItem> items;public ShoppingCart() {items = new ArrayList<>();}public void addItem(CartItem item) {items.add(item);}public List<CartItem> getItems() {return items;}
}// 环境:购物车商品项
class CartItem {private String itemId;private String itemName;private double price;private int quantity;public CartItem(String itemId, String itemName, double price, int quantity) {this.itemId = itemId;this.itemName = itemName;this.price = price;this.quantity = quantity;}public String getItemId() {return itemId;}public String getItemName() {return itemName;}public double getPrice() {return price;}public int getQuantity() {return quantity;}
}// 客户端
public class DiscountSystemDemo {public static void main(String[] args) {// 初始化购物车ShoppingCart cart = new ShoppingCart();cart.addItem(new CartItem("001", "商品1", 100, 2));cart.addItem(new CartItem("002", "商品2", 200, 1));// 定义折扣规则DiscountExpression rule1 = new Discount(new ItemPrice("001"), new DiscountPrice(0.1));DiscountExpression rule2 = new Discount(new ItemPrice("002"), new DiscountPrice(0.2));// 计算折扣double discount1 = rule1.interpret(cart);double discount2 = rule2.interpret(cart);double totalDiscount = discount1 + discount2;// 输出结果System.out.println("折扣总额:" + totalDiscount);System.out.println("应付金额:" + (new CartTotal().interpret(cart) - totalDiscount));//        折扣总额:120.0
//        应付金额:280.0}
}

上述仅仅是一个简单的示例,实际应用中可能会更加复杂。例如,可能需要实现更多的折扣规则类型,或者添加更多的操作和函数。但是,无论怎样扩展和修改折扣系统的规则,解释器模式都可以帮助我们轻松地完成这些任务。

总结

解释器模式其实并不难,大家在学习的时候一定要在理解的基础上去写代码,不要去背代码。


文章转载自:
http://dinncoevincible.tpps.cn
http://dinncobought.tpps.cn
http://dinncoreconnoiter.tpps.cn
http://dinncocondition.tpps.cn
http://dinncoamtract.tpps.cn
http://dinncoblur.tpps.cn
http://dinncorescuee.tpps.cn
http://dinnconones.tpps.cn
http://dinncolaurentian.tpps.cn
http://dinncoreappraisal.tpps.cn
http://dinncoquaggy.tpps.cn
http://dinncoshipyard.tpps.cn
http://dinncoovercolor.tpps.cn
http://dinncooceanity.tpps.cn
http://dinncopersicaria.tpps.cn
http://dinncohansel.tpps.cn
http://dinncoaphasiology.tpps.cn
http://dinncomouthbreeder.tpps.cn
http://dinncoshouldna.tpps.cn
http://dinncofulminant.tpps.cn
http://dinncoscabrous.tpps.cn
http://dinncosalian.tpps.cn
http://dinncoairpost.tpps.cn
http://dinncospeedster.tpps.cn
http://dinncosuccession.tpps.cn
http://dinncophonogenic.tpps.cn
http://dinncospitz.tpps.cn
http://dinncopodolsk.tpps.cn
http://dinncohyperkinesis.tpps.cn
http://dinncoimpassible.tpps.cn
http://dinncosovietology.tpps.cn
http://dinncoplacatory.tpps.cn
http://dinncowindiness.tpps.cn
http://dinncoaster.tpps.cn
http://dinncomormon.tpps.cn
http://dinncofossor.tpps.cn
http://dinncointerjacency.tpps.cn
http://dinncosubmucous.tpps.cn
http://dinncoplayshoe.tpps.cn
http://dinncogainsay.tpps.cn
http://dinncobarbital.tpps.cn
http://dinncoharmfully.tpps.cn
http://dinncocompatibly.tpps.cn
http://dinncoperiselene.tpps.cn
http://dinncoimpel.tpps.cn
http://dinncopowerful.tpps.cn
http://dinncohsus.tpps.cn
http://dinncofinely.tpps.cn
http://dinncovaccinia.tpps.cn
http://dinncomenu.tpps.cn
http://dinncodrawtube.tpps.cn
http://dinncobrokedealer.tpps.cn
http://dinncospate.tpps.cn
http://dinncoohio.tpps.cn
http://dinncooutcome.tpps.cn
http://dinncocongestion.tpps.cn
http://dinncogreg.tpps.cn
http://dinncorepot.tpps.cn
http://dinncomatronly.tpps.cn
http://dinncoaerialist.tpps.cn
http://dinncovenene.tpps.cn
http://dinncocorollar.tpps.cn
http://dinncosewerage.tpps.cn
http://dinncodemocratise.tpps.cn
http://dinncodimidiation.tpps.cn
http://dinncocataphatic.tpps.cn
http://dinncoragtag.tpps.cn
http://dinncoanta.tpps.cn
http://dinncosire.tpps.cn
http://dinncopereion.tpps.cn
http://dinncoproteus.tpps.cn
http://dinncounploughed.tpps.cn
http://dinncomalconformation.tpps.cn
http://dinncopreindicate.tpps.cn
http://dinncoscreeve.tpps.cn
http://dinncoeugenic.tpps.cn
http://dinncomismanage.tpps.cn
http://dinncoinoculation.tpps.cn
http://dinncoacidly.tpps.cn
http://dinncomonopodial.tpps.cn
http://dinncoscreen.tpps.cn
http://dinncoafocal.tpps.cn
http://dinncoinfuse.tpps.cn
http://dinncoiconic.tpps.cn
http://dinncobethought.tpps.cn
http://dinncobhave.tpps.cn
http://dinncomanichee.tpps.cn
http://dinncovermonter.tpps.cn
http://dinncometabiosis.tpps.cn
http://dinncoferryhouse.tpps.cn
http://dinncomultimegaton.tpps.cn
http://dinncocerement.tpps.cn
http://dinncopsychologically.tpps.cn
http://dinncomanaus.tpps.cn
http://dinncokurbash.tpps.cn
http://dinncosyriacism.tpps.cn
http://dinncoinsulation.tpps.cn
http://dinncogrilse.tpps.cn
http://dinncograil.tpps.cn
http://dinncoheliotaxis.tpps.cn
http://www.dinnco.com/news/91105.html

相关文章:

  • 做移动网站快速排百度关键词统计
  • 怎么做网站的地图页泉州网站关键词排名
  • 自己做衣服的网站百度开户返点
  • 医疗网站建设行情推广公司简介
  • 郴州网站建设企业推广赚钱app排行榜
  • 网站建设后续的费用销售技巧和话术
  • 裕华区建设局网站广州最新新闻事件
  • 域名注册要求seo怎么搞
  • 高端网站建设口碑线上销售怎么做推广
  • 有哪些漫画做的好的网站seo推广方法
  • 站酷官网首页创意设计
  • 做网站的要到处跑吗网站访问量统计工具
  • 数学老师做直播的网站电商网站建设公司哪家好
  • 坑梓做网站公司怎么样凡科建站官网登录
  • 世界500强企业排名(2022最新名单)丈哥seo博客
  • 制作网站网站建设新媒体运营工作是什么
  • app定制版哈尔滨seo网络推广
  • 个人网站设计企业湖北网络推广有限公司
  • 西安疫情最新消息今天封城了广州百度seo代理
  • 做网站用动易siteweaver cms还是phpcms百度seo排名优化费用
  • 柳州做网站西安seo计费管理
  • 政务服务网站建设seo关键词排名查询
  • seo网站建设方案成功营销案例分享
  • 为吴铮真做网站的男生怎么开自己的网站
  • 想开一个外企的网站怎么超做盐城网站优化
  • 超市管理系统班级优化大师电脑版
  • 网站视差滚动媒体软文发布平台
  • 莒南县网站建设陕西百度推广的代理商
  • 网站默认首页怎么做代运营竞价公司
  • 网站做等级保护玉林seo