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

安卓系统最好优化软件福建键seo排名

安卓系统最好优化软件,福建键seo排名,设计自己的网站,百度下载app摘要 最近阅读《贯彻设计模式》这本书,里面使用一个更真实的项目来介绍设计模式的使用,相较于其它那些只会以披萨、厨师为例的设计模式书籍是有些进步。但这书有时候为了使用设计模式而强行朝着对应的 UML 图来设计类结构,并且对设计理念缺少…

摘要

最近阅读《贯彻设计模式》这本书,里面使用一个更真实的项目来介绍设计模式的使用,相较于其它那些只会以披萨、厨师为例的设计模式书籍是有些进步。但这书有时候为了使用设计模式而强行朝着对应的 UML 图来设计类结构,并且对设计理念缺少讲解,所以也不能说有多优秀,79分的水平。

书中就这部分内容设计,提到使用了:策略模式、门面模式、策略工厂模式、享元模式。但可能真正称得上是设计的内容就两个部分,策略模式和策略工厂模式。但是就书中所写的策略工厂,个人认为有些啰嗦,并且指定全类名,通过反射来获取对象,这种实现不够优雅。个人相信的设计理念就是在实现代码可扩展的前提下,尽可能使用少的类,只开放必要的接口。虽然 Spring 获取 Bean 本质上也是通过反射来创建的,效率并没有提高。但本文设计并不依赖具体框架,基于 Spring 的目的也是和该书一样,为了让案例更接近现实,基于 Spring 既是一种便利,也是一种约束。

本文的设计方案大体总结如下:

  1. 具体的支付策略实现类(支付宝支付、微信支付)和支付策略门面(Facade)共同实现支付接口
  2. 通过枚举类定义支付策略,并实现编号到实现类的映射。由于实现类是交给 Spring 管理,所以只需要实现编号到 beanName 的映射
  3. 在 Facade 中使用 Map 来实现从编号到实现类对象的映射,根据 Bean 的生命周期,在初始化过程中为 Map 赋值。之所以使用 @PostConstruct 注解,是为了让 init 方法作为 private 方法,而 Facade 只需要暴露上层服务真正需要调用的接口方法就行。

基础环境

pom 依赖

<dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-sdk-java</artifactId><version>4.34.0.ALL</version>
</dependency>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.7.4</version>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><version>2.7.4</version>
</dependency>

常量工具类

/*** 根据阿里开放平台给出的文档,字段为为第三方平台要求*/
public class AliPayConstant {public static final String OUT_TRADE_NO = "out_trade_no";public static final String TOTAL_AMOUNT = "total_amount";public static final String SUBJECT = "subject";public static final String PRODUCT_CODE = "product_code";public static final String FAST_INSTANT_TRADE_PAY = "FAST_INSTANT_TRADE_PAY";
}

实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ToString(callSuper = true)
@EqualsAndHashCode
@Builder
public class Order {/*** 订单编号*/private String orderNo;/*** 订单金额*/private Double payment;/*** 订单标题*/private String orderTitle;
}

Spring 配置文件、配置属性类和配置类

payout:alibaba:# 沙箱环境的支付宝网关接口url: https://openapi-sandbox.dl.alipaydev.com/gateway.doapp-id:private-key:alipay-public-key:
@ConfigurationProperties("payout.alibaba")
@Data
public class AlipayProperties {private String url;private String appId;private String privateKey;private String alipayPublicKey;// 默认值private String format = "json";private String charset = "UTF-8";private String signType = "RSA2";
}
@Configuration
@EnableConfigurationProperties(AlipayProperties.class)
public class AlipayConfiguration {@Beanpublic AlipayClient alipayClient(AlipayProperties alipayProperties) throws AlipayApiException {AlipayConfig alipayConfig = new AlipayConfig();//设置网关地址alipayConfig.setServerUrl(alipayProperties.getUrl());//设置应用IDalipayConfig.setAppId(alipayProperties.getAppId());//设置应用私钥alipayConfig.setPrivateKey(alipayProperties.getPrivateKey());//设置请求格式,固定值jsonalipayConfig.setFormat(alipayProperties.getFormat());//设置字符集alipayConfig.setCharset(alipayProperties.getCharset());//设置签名类型alipayConfig.setSignType(alipayProperties.getSignType());//设置支付宝公钥alipayConfig.setAlipayPublicKey(alipayProperties.getAlipayPublicKey());//实例化客户端return new DefaultAlipayClient(alipayConfig);}}

Controller 层

/*** 支付接口,由于要回显html页面,因此直接使用@Controller接口*/
@Controller
@RequestMapping("/payout")
@Slf4j
public class PayoutController {@Autowiredprivate PayoutService payoutService;@SneakyThrows@GetMapping("/{payType}")public void payout(HttpServletResponse response,@PathVariable Integer payType) {log.info("支付方式:{}", payType);Order order = Order.builder().orderNo(UUID.randomUUID().toString()).payment(900.0).orderTitle("兰博基尼").build();String payPageForm = payoutService.pay(order, payType);response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();out.write(payPageForm);out.flush();out.close();log.info("显示支付页面");}@SneakyThrows@GetMapping("/callback")public void callback(HttpServletResponse response) {log.info("回调页面");PrintWriter out = response.getWriter();out.write("Hello World");out.flush();out.close();log.info("写出消息");}
}

Service 层

@Service
public class PayoutService {@Autowiredprivate PayStrategyFacade payStrategyFacade;public String pay(Order order, Integer payType) {return payStrategyFacade.pay(order, payType);}
}

设计模式部分

策略接口

public interface PayStrategy {/*** 调用第三方支付接口** @param order 订单封装* @return 调用成功返回页面信息,即response.getBody();调用失败返回null*/String pay(Order order);
}

策略实现类(支付宝支付、微信支付、银行支付等)

复制支付宝、微信等开放平台的代码内容即可

@Component("aliPay")
public class AliPayStrategyImpl implements PayStrategy {@Autowiredprivate AlipayClient alipayClient;@Overridepublic String pay(Order order) {// 支付金额double payAmount = order.getPayment();// 订单标题String orderTitle = order.getOrderTitle();// 商户订单号String orderNo = order.getOrderNo();// 不同的请求类型构造不同的Request对象AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();request.setNotifyUrl("");request.setReturnUrl("http://localhost:8080/payout/callback");JSONObject bizContent = new JSONObject();// 必传参数// 商户订单号,商家自定义,保持唯一性bizContent.put(AliPayConstant.OUT_TRADE_NO, orderNo);// 支付金额,最小值0 .01 元bizContent.put(AliPayConstant.TOTAL_AMOUNT, payAmount);// 订单标题,不可使用特殊符号bizContent.put(AliPayConstant.SUBJECT, orderTitle);// 电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAYbizContent.put(AliPayConstant.PRODUCT_CODE, AliPayConstant.FAST_INSTANT_TRADE_PAY);request.setBizContent(bizContent.toString());AlipayTradePagePayResponse response;try {response = alipayClient.pageExecute(request);} catch (AlipayApiException e) {throw new RuntimeException(e);}if (response.isSuccess()) {// 在网站上显示支付宝支付页面,让用户扫描支付return response.getBody();}return null;}
}

策略枚举类

枚举所有的策略,由于枚举类对象是静态对象,因此不能够将其直接作为 Spring 容器的 Bean。

最开始枚举类中设计的两个字段分别是 int 类型的 payType 和 PayStrategy 类型的对象,希望通过 ALIBABA(0, new AliPayStrategyImpl()) 的方式来初始化枚举类对象。但是由于 AliPayStrategyImpl 中的 AliClient 是交给了 Spring 容器进行管理,而使用 new 方式得到的对象没有经过 Spring,所以其中的 AliClient 为 null。

同时即使将枚举类交给 Spring 管理,其依赖注入也十分麻烦,通过初始化方法去覆盖类对象中的属性,也需要依赖 beanName,且设计丑陋,没有枚举类的优雅。因此直接在枚举类中负责管理 beanName**(在 Spring 框架中,管理了 beanName,就是管理了 BeanDefinition)**

@Getter
public enum PayStrategyEnum {// TODO: 由于实现类依赖了Spring的自动注入来获取AliClient, 因此直接new AlipayStrategyImpl()不会自动注入AliClientALIBABA(0, "aliPay"),WECHAT(1, "wechatPay"),;private final int payType;/*** 枚举类结合Spring的中介产物,根据迪米特法则,不需要对外开放*/private final String beanName;PayStrategyEnum(Integer payType, String beanName) {this.payType = payType;this.beanName = beanName;}
}

策略门面?策略上下文?策略工厂?

@Component
public class PayStrategyFacade {@Autowiredprivate ApplicationContext applicationContext;// 由于只在初始化的时候进行设置,并发读不存在线程安全问题,因此不需要使用ConcurrentHashMapprivate static final Map<Integer, PayStrategy> PAY_STRATEGIES = new HashMap<>(PayStrategyEnum.values().length);@PostConstructprivate void init() {// 初始化策略for (PayStrategyEnum payStrategyEnum : PayStrategyEnum.values()) {PAY_STRATEGIES.put(payStrategyEnum.getPayType(),applicationContext.getBean(payStrategyEnum.getBeanName(), PayStrategy.class));}}public String pay(Order order, Integer payType) {PayStrategy payStrategy = PAY_STRATEGIES.getOrDefault(payType, null);if (payStrategy == null) {throw new RuntimeException("不支持的支付类型");}return payStrategy.pay(order);}
}

文章转载自:
http://febricide.rdfq.cn
http://cyclic.rdfq.cn
http://versification.rdfq.cn
http://ourselves.rdfq.cn
http://resurvey.rdfq.cn
http://torsional.rdfq.cn
http://paleolith.rdfq.cn
http://eponymous.rdfq.cn
http://inconsequent.rdfq.cn
http://stepdance.rdfq.cn
http://gisela.rdfq.cn
http://jiminy.rdfq.cn
http://bystreet.rdfq.cn
http://washiness.rdfq.cn
http://containerboard.rdfq.cn
http://eyen.rdfq.cn
http://defectiveness.rdfq.cn
http://pentangular.rdfq.cn
http://orebody.rdfq.cn
http://pantelegraph.rdfq.cn
http://boresome.rdfq.cn
http://oomiac.rdfq.cn
http://unliving.rdfq.cn
http://guttate.rdfq.cn
http://soerakarta.rdfq.cn
http://pad.rdfq.cn
http://eternalize.rdfq.cn
http://allograft.rdfq.cn
http://succi.rdfq.cn
http://insincerity.rdfq.cn
http://bandstand.rdfq.cn
http://nesselrode.rdfq.cn
http://cd.rdfq.cn
http://sycamine.rdfq.cn
http://potbelly.rdfq.cn
http://defectiveness.rdfq.cn
http://chickpea.rdfq.cn
http://couture.rdfq.cn
http://menshevism.rdfq.cn
http://skyer.rdfq.cn
http://nextel.rdfq.cn
http://trapt.rdfq.cn
http://bereavement.rdfq.cn
http://subservient.rdfq.cn
http://devaluation.rdfq.cn
http://decimal.rdfq.cn
http://tiptilt.rdfq.cn
http://nitramine.rdfq.cn
http://jehovah.rdfq.cn
http://pressing.rdfq.cn
http://flocculus.rdfq.cn
http://rarotonga.rdfq.cn
http://moribund.rdfq.cn
http://inexplicable.rdfq.cn
http://soapstone.rdfq.cn
http://notched.rdfq.cn
http://pathlet.rdfq.cn
http://gyrofrequency.rdfq.cn
http://wrapping.rdfq.cn
http://unearthly.rdfq.cn
http://ticker.rdfq.cn
http://caricaturist.rdfq.cn
http://ymha.rdfq.cn
http://corsair.rdfq.cn
http://monopoly.rdfq.cn
http://specifically.rdfq.cn
http://unpolarized.rdfq.cn
http://narcoma.rdfq.cn
http://spiegeleisen.rdfq.cn
http://slipper.rdfq.cn
http://ducat.rdfq.cn
http://gumma.rdfq.cn
http://throstle.rdfq.cn
http://lippen.rdfq.cn
http://economist.rdfq.cn
http://declare.rdfq.cn
http://philatelic.rdfq.cn
http://erotesis.rdfq.cn
http://technicality.rdfq.cn
http://oceanarium.rdfq.cn
http://frilling.rdfq.cn
http://misdate.rdfq.cn
http://thunderclap.rdfq.cn
http://egocentricity.rdfq.cn
http://unwarrantable.rdfq.cn
http://porkling.rdfq.cn
http://haemostatic.rdfq.cn
http://cipolin.rdfq.cn
http://electroshock.rdfq.cn
http://eros.rdfq.cn
http://menshevist.rdfq.cn
http://deadhouse.rdfq.cn
http://ruction.rdfq.cn
http://epiglottis.rdfq.cn
http://fibrocystic.rdfq.cn
http://protuberance.rdfq.cn
http://pragmatistic.rdfq.cn
http://covenantee.rdfq.cn
http://churching.rdfq.cn
http://optotype.rdfq.cn
http://www.dt0577.cn/news/96975.html

相关文章:

  • 微信小程序开发快速入门seo推广宣传
  • 网页设计网站开发需要哪些知识一级造价工程师
  • 做网站要学那些东西艾滋病多久能查出来
  • 怎么用dreamweaver做网站互联网营销模式
  • 厦门市建设局官方网站证书查询广州网络推广策划公司
  • 做网站甲方乙方公司的区别网站建设优化400报价
  • 没公司怎么做网站廊坊关键词优化报价
  • 淘宝网站开发搜索引擎外部优化有哪些渠道
  • 合肥 电子商务 网站推广网站推广服务外包
  • html5 公司网站模板sem竞价推广怎么做
  • 网站后台难做吗全网推广软件
  • 内容网站管理系统网站建设是干嘛的
  • 在线一键扒站源码php百度统计平台
  • 有哪些可以做外链的网站网站seo优化报告
  • 医疗美容网站模版下载免费seo工具大全
  • html5和php做网站四川省人民政府
  • 张家港网站建设培训学校百度打广告多少钱一个月
  • 南宁经典网站建设个人如何推广app
  • 网站建设服务器百度云常熟网站建设
  • 企业宣传网站怎么做搜索引擎优化搜索优化
  • 优设网免费素材seo常用分析的专业工具
  • 南京做企业网站公司磁力搜索器在线
  • 网站建设与维护的题目2023年10月爆发新冠
  • 做网站等保收费网站优化有哪些类型
  • 做视频网站用什么服务器中国知名网站排行榜
  • 如何在外管局网站做延期艺考培训
  • 网站法人与负责人找网站公司制作网站
  • 哪个网站做兼职可以赚钱网络营销方式有几种
  • 浙江台州网站制作百度推广咨询
  • 动态网站开发总结感想泉州seo外包