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

wordpress建数据库步骤深圳设计优化公司

wordpress建数据库步骤,深圳设计优化公司,wordpress恢复数据库文件,网站备案能快速备案嘛优化if-else的几种方式 策略模式1、创建支付策略接口2、书写不同的支付方式逻辑代码微信支付QQ支付 3、service层的实现类使用4、controller层的调用说明 枚举与策略模式结合1、创建枚举2、service层书写处理方法3、controller层调用4、说明 Lambda表达式与函数接口说明 策略模… 优化if-else的几种方式 策略模式1、创建支付策略接口2、书写不同的支付方式逻辑代码微信支付QQ支付 3、service层的实现类使用4、controller层的调用说明 枚举与策略模式结合1、创建枚举2、service层书写处理方法3、controller层调用4、说明 Lambda表达式与函数接口说明 策略模式 策略模式允许在运行时选择算法。策略模式是将算法定义成独立的类并在运行时动态选择要使用的具体的算法以此来避免多个if-else或switch语句的使用。 下面以支付功能为例子进行说明。 假设我们有一个支付系统支持微信、QQ等多种支付方式。用户在支付时会选择自己需要的支付方式后台功能接口中接收到用户的支付方式选择时会进行不同的处理。在这里会产生if-else或者switch。如何使用策略模式来消除这些if-else的使用下面示例说明。1、创建支付策略接口 /*** 支付策略*/ public interface PaymentStrategy {void pay(double amount);}2、书写不同的支付方式逻辑代码 微信支付 import org.springframework.stereotype.Component;Component public class WeiXinPayment implements PaymentStrategy {Overridepublic void pay(double amount) {System.out.println(微信支付 amount);} }QQ支付 import org.springframework.stereotype.Component;Component public class QQPayment implements PaymentStrategy {Overridepublic void pay(double amount) {System.out.println(QQ支付 amount);} }3、service层的实现类使用 import com.hysoft.study.service.PaymentStrategy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors;/*** 支付实现类*/ Service public class PaymentServiceImpl {private final MapString, PaymentStrategy strategies;Autowiredpublic PaymentServiceImpl(ListPaymentStrategy paymentStrategies){this.strategies paymentStrategies.stream().collect(Collectors.toMap(s - s.getClass().getSimpleName().toLowerCase(), Function.identity()));}public void processPayment(String strategyName,double amount){PaymentStrategy strategy strategies.getOrDefault(strategyName,null);if (strategy ! null){strategy.pay(amount);}else {throw new IllegalArgumentException(Strategy not found strategyName);}}}4、controller层的调用 import com.hysoft.study.service.impl.PaymentServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;RestController RequestMapping(payment) public class PaymentController {Autowiredprivate PaymentServiceImpl paymentService;PostMapping(test)public void test(String paymentname,double amount){this.paymentService.processPayment(paymentname,amount);}}说明 需要注意的是controller层调用service层实现方法的适合传递了参数paymentname(支付方式)这个支付方式需要提前和前端调用人员协商好这里的名称是各种支付方式的bean名称在service处理中已经有所体现因此在这里名称传值可以是qqpayment或weixinpayment。因此传值需要提前和前端进行协商。 枚举与策略模式结合 枚举类型不仅可以用来表示一组常量还可以定义与这些常量相关联的行为。结合策略模式可以进一步简化代码。1、创建枚举 public enum OrderStatus {NEW {Overridepublic void process () {System.out.println(处理新建订单);}},PAID {Overridepublic void process () {System.out.println(订单已支付);}},UNPAD {Overridepublic void process() {System.out.println(订单未支付);}};public abstract void process(); }2、service层书写处理方法 import com.hysoft.study.model.OrderStatus; import org.springframework.stereotype.Service;Service public class OrderServiceImpl {public void handleOrder(OrderStatus status) {status.process();}}3、controller层调用 import com.hysoft.study.model.OrderStatus; import com.hysoft.study.service.impl.OrderServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;RestController RequestMapping(order) public class OrderController {Autowiredprivate OrderServiceImpl orderService;PostMapping(test)public void test(String status){OrderStatus aNew OrderStatus.valueOf(status);this.orderService.handleOrder(aNew);}}4、说明 controller层调用时传输的参数status即使在枚举中的各常量例如NEW或者PAID、UNPAD等Lambda表达式与函数接口 以下示例在service层实现类中直接书写了不同vip等级的结算金额的逻辑import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; import java.util.function.Function;Service public class StreamServiceImpl {private final MapString, FunctionDouble, Double discountFunctions new HashMap();public StreamServiceImpl() {discountFunctions.put(VIP1, e - e * 0.95);discountFunctions.put(VIP2, e - e * 0.95 - 20);}public double applyDiscount(String vipname, double price) {Double apply discountFunctions.getOrDefault(vipname, Function.identity()).apply(price);return apply;} }在controller层调用时需要传入vip等级和总计算金额计算结果时打折后金额 import com.hysoft.study.service.impl.StreamServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;RestController RequestMapping(stream) public class StreamController {Autowiredprivate StreamServiceImpl streamService;PostMapping(test)public double test(String vipname,Double price){return this.streamService.applyDiscount(vipname,price);}}说明 传参vipname的值应参考service层类中的vip参数
http://www.sczhlp.com/news/155956/

相关文章:

  • aso优化报价seo优化推广是什么意思
  • 做行业网站赚钱吗十大免费最亏的免费app
  • 网站空间关闭了怎么办南宁中庭装饰公司电话
  • 网站引导页一般是什么格式找公司做网站需要买服务器
  • 软件开发工程师待遇怎么样网站内优化怎么做
  • 上线了如何制作网站做网站电脑开一天用多少钱
  • 全景校园网站开发wordpress 指南
  • asp网站数据库位置讯美网站建设
  • 网站制作 系统定制wordpress设置角色
  • 重庆电商网站建设费用服务器搭建代理ip
  • 网站建设技术分为哪些方向为什么打开谷歌浏览器是2345网址导航
  • 广药网站建设试题个人网站建设第一步
  • 长沙 网站开发南宁论坛
  • 网站设计都用什么字体新泰建设局网站
  • 上海网站建设 网络推广网站方案建设书模板
  • asp网站开发教案一个人可以做网站
  • 什么网站做3d模型能赚钱昆山的网站建设
  • 电子商务行业网站软件管理软件哪个好
  • 关键词网站推广网站建设招标说明书
  • tp5 基础nginx伪静态
  • 网站站点结构的构建加强红色网站建设
  • 口腔医院网站优化服务商wordpress1003无标题
  • 制作应用的网站软件工程专业就业方向及前景分析
  • 2014网站推广方案html5开发工程师
  • 不会编程做网站办公室设计平面图
  • 学校网站开发报价表显示危险网站怎么解决
  • 微擎做网站费用apache wordpress rewrite
  • 做网站员培训wordpress手机如何登陆
  • 做自媒体要知道的网站北京网站建设公司分形
  • 找黄岩做网站企业wordpress 语录小程序