模版方法模式(Template Method Pattern)

        模板方法模式可以所是最为常见的一种设计模式了,出乎很多人意料的是,很多人已经在他们的代码中用到了模板方法模式而没有意识到自己用到了这个模式,模板方法模式几乎可以在所有的抽象基类中找到。 通过模板方法模式我们可以定义一个操作的骨架,而将该操作中的各个步骤的具体实现延迟至派生类中。代表这些具体实现的方法同常称作基本方法(Primitive Method),而调用这些基本方法的方法叫做模板方法(Template Method)。呵呵,你这是已经明白模板方法模式名字的由来了。 模板方法的结构图:
<?php
//模版模式
abstract class Goods {
	final public function buy($goodsName) {
		$goods = $this->selectGoods($goodsName);
		$order = $this->buyGoods($goods);
		$send = $this->sendGoods($order);
	}
	abstract public function selectGoods($goodsName);
	abstract public function buyGoods($select);
	abstract public function sendGoods($order);
}

class PSV extends Goods {
	function selectGoods($goodsName) {
		$goods = $goodsName;
		return $goods;
	}
	function buyGoods($goods) {
		$order = 'order'.$goods;
		return $order;
	}
	function sendGoods($order) {
		$success = $order.'is OK';
		echo $success;
	}
}
$myPSV = new PSV();
$myPSV->buy('PSV');
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值