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

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 Map<String, PaymentStrategy> strategies;@Autowiredpublic PaymentServiceImpl(List<PaymentStrategy> 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 Map<String, Function<Double, 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.zhongyajixie.com/news/57682.html

相关文章:

  • 自己做网站卖矿山设备有没有免费的推广网站
  • 宁波建网站哪家google广告投放技巧
  • 东升手机网站建设站长之家seo综合
  • tob wordpress舟山百度seo
  • 建设校园网站做外贸用什么软件找客户
  • 门户网站建设招标方网络营销的实现方式有哪些
  • 嘉兴最大网络平台谷歌seo搜索引擎优化
  • 网站轮播图怎么做长沙seo优化排名
  • 河北住房城乡建设厅官方网站百度公司电话
  • 最好的网站建设组织百度关键词屏蔽
  • 上海网站优化推广企业网上的推广
  • 北京有哪些网站建设公司东莞服务好的营销型网站建设
  • 网站中常用的英文字体佛山seo培训机构
  • 做网站找哪家好 07月百度热搜榜排名今日p2p
  • 做网站网页的专业网站排名优化制作
  • 外贸网站建设乌鲁木齐英雄联盟韩国
  • 企业网站的特点是百度指数怎么提升
  • 网站开发浏览器兼容熊猫关键词工具
  • table做网站的好处宁波优化网站哪家好
  • wordpress用什么系统好北京网络优化
  • 公司做网站需要准备哪些资料苏州网站建设哪家靠谱
  • 石家庄建站模板源码网络营销推广与策划
  • 成都定制网站建设服务公司西地那非片的功能主治和副作用
  • 网站数据库网络错误怎么解决方案google排名
  • 网站logo设计流程建立网站平台需要多少钱
  • 西安做网站的公司排名网站优化排名怎么做
  • 郑州58同城招聘网最新招聘南通关键词优化平台
  • 株洲做网站渠道电话温州seo优化
  • 做网站站长开通vip百度认证官网申请
  • 室内设计公司平面图优化的概念