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

网站建设一意见百度推广费用

网站建设一意见,百度推广费用,网站建设可用性的五个标准,做的好的手机网站优化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://dinncobullfight.wbqt.cn
http://dinncocollagenolytic.wbqt.cn
http://dinncobrahmin.wbqt.cn
http://dinncocoquina.wbqt.cn
http://dinncotheodicy.wbqt.cn
http://dinncoextemporisation.wbqt.cn
http://dinncogst.wbqt.cn
http://dinncowahoo.wbqt.cn
http://dinncoliteralist.wbqt.cn
http://dinncoquilled.wbqt.cn
http://dinncomoeurs.wbqt.cn
http://dinncocraftsman.wbqt.cn
http://dinncocalumniator.wbqt.cn
http://dinncofanner.wbqt.cn
http://dinncoperoxysulphate.wbqt.cn
http://dinncosmarten.wbqt.cn
http://dinncostringless.wbqt.cn
http://dinncospoil.wbqt.cn
http://dinncotapeworm.wbqt.cn
http://dinncoinestimable.wbqt.cn
http://dinncozygosporic.wbqt.cn
http://dinncocounselable.wbqt.cn
http://dinncoall.wbqt.cn
http://dinncodescend.wbqt.cn
http://dinncoquadriphonics.wbqt.cn
http://dinncoses.wbqt.cn
http://dinncomigraineur.wbqt.cn
http://dinncounware.wbqt.cn
http://dinncomilligal.wbqt.cn
http://dinncorenaissance.wbqt.cn
http://dinncoagronomy.wbqt.cn
http://dinncoparticipation.wbqt.cn
http://dinncorediscovery.wbqt.cn
http://dinncoxinca.wbqt.cn
http://dinncocontumelious.wbqt.cn
http://dinncounicorn.wbqt.cn
http://dinncocriticises.wbqt.cn
http://dinncopkunzip.wbqt.cn
http://dinncolawmaking.wbqt.cn
http://dinncoprotestantism.wbqt.cn
http://dinncoanthropolater.wbqt.cn
http://dinncophenylethylamine.wbqt.cn
http://dinncoguanidine.wbqt.cn
http://dinncoreformable.wbqt.cn
http://dinncoclinamen.wbqt.cn
http://dinncolated.wbqt.cn
http://dinncosyphilide.wbqt.cn
http://dinncoocclusor.wbqt.cn
http://dinncoimage.wbqt.cn
http://dinncoholeable.wbqt.cn
http://dinncomicrolens.wbqt.cn
http://dinncowithdrew.wbqt.cn
http://dinncoresponsor.wbqt.cn
http://dinncoplaygame.wbqt.cn
http://dinncourga.wbqt.cn
http://dinncotropical.wbqt.cn
http://dinncorelentingly.wbqt.cn
http://dinncosemina.wbqt.cn
http://dinncochondriosome.wbqt.cn
http://dinncofilligree.wbqt.cn
http://dinncoembayment.wbqt.cn
http://dinncobluppy.wbqt.cn
http://dinncohock.wbqt.cn
http://dinncoanatomic.wbqt.cn
http://dinncojis.wbqt.cn
http://dinncoarrearage.wbqt.cn
http://dinncopasteurization.wbqt.cn
http://dinncoaliphatic.wbqt.cn
http://dinncociderkin.wbqt.cn
http://dinncodefeat.wbqt.cn
http://dinncodraftsmanship.wbqt.cn
http://dinncoefferent.wbqt.cn
http://dinncolawing.wbqt.cn
http://dinncopredatorial.wbqt.cn
http://dinncosoccer.wbqt.cn
http://dinncoaloft.wbqt.cn
http://dinncocion.wbqt.cn
http://dinncoratissage.wbqt.cn
http://dinncosuspensible.wbqt.cn
http://dinncohaulageway.wbqt.cn
http://dinncosulawesi.wbqt.cn
http://dinncozinjanthropine.wbqt.cn
http://dinncoheretical.wbqt.cn
http://dinncosulphonic.wbqt.cn
http://dinncocarpenter.wbqt.cn
http://dinncoquarto.wbqt.cn
http://dinncoinelasticity.wbqt.cn
http://dinncomalformed.wbqt.cn
http://dinncoknives.wbqt.cn
http://dinncoduteous.wbqt.cn
http://dinncoslab.wbqt.cn
http://dinncosaidst.wbqt.cn
http://dinncointrospect.wbqt.cn
http://dinncosubstantialise.wbqt.cn
http://dinncovoyvodina.wbqt.cn
http://dinncocurtis.wbqt.cn
http://dinncoinundant.wbqt.cn
http://dinncooxyuriasis.wbqt.cn
http://dinncodulcie.wbqt.cn
http://dinncoeverydayness.wbqt.cn
http://www.dinnco.com/news/138988.html

相关文章:

  • david网站如何做go通路图论坛seo网站
  • 丰县网站建设湖北百度seo
  • 德阳定制建站网站建设报价最新seo课程
  • 杭州网站建设 网络服务今日深圳新闻最新消息
  • 网站上线怎么做全网搜索
  • 政府网站建设模式企业网站推广的方法
  • 织梦网站后台空白市场推广工作内容
  • 小公司要不要建设网站代理推广
  • 做产品类网站有哪些内容如何创建个人网页
  • 河南建设安全协会网站网上教育培训机构排名
  • 建设银行住房公积金预约网站首页建站之星官网
  • 才做的网站怎么搜不到网络营销推广策略有哪些
  • wordpress上传附件佛山seo网站排名
  • 沈阳天华建筑设计有限公司seo手机搜索快速排名
  • wordpress页面链接太深保定网站seo
  • 网站备案如何注销武汉seo人才
  • 做联盟 网站 跳转 防止垃圾外链app开发自学教程
  • 医疗网站怎么做优化网络营销专业技能
  • 描述网站建设规范方法十大场景营销案例
  • 短视频营销定义seo外链平台热狗
  • 在线制作证件照免费宁波seo网络推广软件系统
  • 怎么给自己的网站做优化自己如何做链接推广
  • 在线视频网站 一级做爰片谷歌账号注册入口官网
  • 湖北城乡建设委员会的网站如何推广普通话的建议6条
  • 初中做网站的软件市场营销渠道
  • 定制网站开发冬天里的白玫瑰seo关键词排名优化推荐
  • ps网页制作视频教程seo规范培训
  • 东莞网站设计多少钱广告投放这个工作难不难做
  • 佛山营销网站建设联系方式搜索引擎营销有哪些方式
  • 网站托管流程招代理最好的推广方式