面向对象的洗礼:设计模式(二十六)之享元模式
享元模式解决了大量几乎相似的对象的这种情况。设计模式中的享元模式使程序运行时更加节省服务器资源。享元模式是一种非常好的设计模式。如果一个应用程序使用了大量的对象,而大量的这些对象对服务器资源造成了很大的开销和压力时,就应该考虑使用享元模式。
享元模式解决了大量几乎相似的对象的这种情况。设计模式中的享元模式使程序运行时更加节省服务器资源。享元模式是一种非常好的设计模式。如果一个应用程序使用了大量的对象,而大量的这些对象对服务器资源造成了很大的开销和压力时,就应该考虑使用享元模式。
享元模式:运用共享技术有效的支持大量的细粒度对象。
比如:围棋只有黑白两种棋子,用一个对象生成黑棋子,一个对象生成白棋子,是要一份代码共享给所有的黑棋子共同使用呢,还是每个黑棋子独立一个对象。这就是享元模式,共享对象以达到节省开销的目的。
场景:阿里云旗下的万网提供快速建站的服务,它是给每个用户独立生成一个网站所有的源代码,还是说同类型的网站共享一份代码?答案是后者(示例仅为说明享元模式,并不代表万网的真实实现方式)。以PHP为代码环境,模拟设计模式之享元模式的代码实现。
[code]
<?php
class User{
private $name;
public function __construct($name){
$this->setName($name);
}
public function setName($name){
$this->name = $name;
}
public function getName(){
return $this->name;
}
}
abstract class Website{
private $name;
public function __construct($name){
$this->setName($name);
}
public function setName($name){
$this->name = $name;
}
public function getName(){
return $this->name;
}
}
class ConcreteWebsite extends Website{
public function __construct($name){
parent::__construct($name);
}
public function useWebsite($userObj){
echo '网站名称:' . $this->getName() . '。 所属用户' . $userObj->getName() . '<br>';
}
}
class WebsiteFactory{
private $userWebsiteList = array();
public function getWebsite($key, $name){
if(!isset($this->userWebsiteList[$key])){
$this->userWebsiteList[$key] = new ConcreteWebsite($name);
}
return $this->userWebsiteList[$key];
}
}
//客户端/接口
//网站工厂
$websiteFactory = new WebsiteFactory();
//采用万网提供的第一套模板并起名
$website = $websiteFactory->getWebsite('1', 'LaneBlog');
$website->useWebsite(new User('小轩'));
//采用万网提供的第一套模板并起名
$website = $websiteFactory->getWebsite('1', 'Lane博客');
$website->useWebsite(new User('小明'));
//采用万网提供的第一套模板并起名
$website = $websiteFactory->getWebsite('1', 'LixuanBlog');
$website->useWebsite(new User('小红'));
//采用万网提供的第二套模板并起名
$website = $websiteFactory->getWebsite('2', '论坛');
$website->useWebsite(new User('小白'));
[/code]
根据结果可以看到,多个用户,前三个用户使用的是同一套系统。节省开销。至于名称,从库里读出来即可。这里完全不需要。
点击:2571
点赞:16
点踩:17