php策略模式(strategy pattern)
2024-10-20 05:33:30
。。。
<?php /* The strategy pattern defines a family of algorithms, each of which is encapsulated and made interchangeable with other members within that family. */ interface PaymentStrategy { public function pay($amount); } class StripePayment implements PaymentStrategy { public function pay($amount) { echo 'StripePayment.<br/>'; } } class PayPalPayment implements PaymentStrategy { public function pay($amount) { echo 'PayPalPayment.<br/>'; } } class Checkout { private $amount; public function __construct($amount) { $this->amount = $amount; } public function capturePayment() { if ($this->amount > 99.99) { $payment = new PayPalPayment(); } else { $payment = new StripePayment(); } $payment->pay($this->amount); } } $checkout = new Checkout(49.99); $checkout->capturePayment(); $checkout = new Checkout(149.99); $checkout->capturePayment(); ?>
最新文章
- apt-get 与 yum 的区别
- 解决因为I_JOB_NEXT问题导致job执行不正常,不停生成trace文件问题
- jq 移除包含某个字符串的类名js
- cycleInterpolator 循环加速器
- 深入理解ServletRequest与ServletResponse
- POJ 1573
- Linux_系统信息
- js字符串常用判断方法
- Android 高级UI设计笔记04:使用setDrawingCacheEnabled(boolean flag)提高绘图速度
- 测试用(编写优质嵌入式C程序)
- SGU 111.Very simple problem
- City Game
- 最受欢迎的Web开发工具
- Node.js之操作文件系统(二)
- 《认知与设计:理解UI设计准则》【PDF】下载
- volume_manager.go
- CSS预处器的了解
- JS_高程5.引用类型(4)Array类型的各类方法
- python+django+uwsgi 搭建环境
- LightOJ 1030 【概率DP求期望】