$ for i in `seq 1 32` > { > echo "There are" `egrep '^.{'$i'}$' /usr/dict/words | wc -l` "$i-letter words in the dictionary." > } There are 52 1-letter words in the dictionary. There are 155 2-letter words in the dictionary. There are 1351 3-letter words in the dictionary. There are 5110 4-letter words in the dictionary. There are 9987 5-letter words in the dictionary. There are 17477 6-letter words in the dictionary. There are 23734 7-letter words in the dictionary. There are 29926 8-letter words in the dictionary. There are 32380 9-letter words in the dictionary. There are 30867 10-letter words in the dictionary. There are 26011 11-letter words in the dictionary. There are 20460 12-letter words in the dictionary. There are 14938 13-letter words in the dictionary. There are 9762 14-letter words in the dictionary. There are 5924 15-letter words in the dictionary. There are 3377 16-letter words in the dictionary. There are 1813 17-letter words in the dictionary. There are 842 18-letter words in the dictionary. There are 428 19-letter words in the dictionary. There are 198 20-letter words in the dictionary. There are 82 21-letter words in the dictionary. There are 41 22-letter words in the dictionary. There are 17 23-letter words in the dictionary. There are 5 24-letter words in the dictionary. There are 0 25-letter words in the dictionary. There are 0 26-letter words in the dictionary. There are 0 27-letter words in the dictionary. There are 0 28-letter words in the dictionary. There are 0 29-letter words in the dictionary. There are 0 30-letter words in the dictionary. There are 0 31-letter words in the dictionary. There are 0 32-letter words in the dictionary. $
大多数人认为,接口的意义在于顶替多重继承。众所周知Java没有c 那样多重继承的机制,但是却能够实作多个接口。其实这样做是很牵强的,接口和继承是完全不同的东西,接口没有能力代替多重继承,也没有这个义务。接口的作用,一言以蔽之,就是标志类的类别(type of class)。把不同类型的类归于不同的接口,可以更好的管理他们。OO的精髓,我以为,是对对象的抽象,最能体现这一点的就是接口。为什么我们讨论设计模式都只针对具备了抽象能力的语言(比如c 、java、c#等),就是因为设计模式所研究的,实际上就是如何合理的去抽象。(cowboy的名言是“抽象就是抽去像的部分”,看似调侃,实乃至理)。
继承的概念不用多说,很好理解。为什么要继承呢?因为你想重用代码?这绝对不是理由,继承的意义也在于抽象,而不是代码重用。如果对象 A有一个run()方法,对象B也想有这个方法,所以有人就Class B extends A。这是不经大脑的做法。如果在B中实例化一个A,调用A的Run()方法,是不是可以达到同样的目的?如下:
Class B
{
A a=new A();
a.run();
}
Interface DataOperation
{
public function select($info);
public function selectNum($info);
}
上面这interface定义了读取数据的接口,select方法将返回所需要的文章。selectNum方法返回文章的总数,这是分页显示时用到的。$info是一个数组,用来存放查询条件
Interface DataSource
{
public static function getInstance();
}
这里我们假定我们操作的是数据库,DataSource定义一个接口,所有实现该接口的实例类将得到一个静态对象
Interface Controller
{
public function pop();
public function push();
public function execute();
}
Interface View
{
public function display();
}
好了,我们来实现.
下面定义一个类来实现DataSource接口,这个类运用了单例模式
class DataBaseSource implements DataSource
{
public static $instance = null;
public static function getInstance()
{
if(self::$instance == null)
{
self::$instance == new PDO("mysql:host=localhost;dbname=article","root","123456");
}
return self::$instance;
}
}
定义一个抽象类来实现DataOperation,我们要共享一个数据库连接,所以我在抽象类中将这个数据库对象初始化,这样,所有的子类都能共享这个对象
abstract class DataBaseOperation implements DataOperation
{
protected $db = null;
public function __construct()
{
$this->db = DataBaseSource::getInstance();
}
public function select($info);
public function select($info);
}
下面我来写一个业务子类来实现抽象类DataBaseOperation
class Tech extends DataBaseOperation
{
public function select($info)
{
//在这里实现你的代码
}
public function selectNum($info)
{
//在这里实现你的代码
}
}
业务逻辑层我们实现了,下面是控制层
class ViewController implements Controller
{
private $mod = array();
public function push($key,$value);
{
//实现你的代码,将类注册进$this->mod;
}
public function pop($key)
{
//实现你的代码,将$this->mod[$key]值为null;
}
public function execute($key)
{
//在这里实现你的代码,生成实例.注意利用php5新的特性,异常的处理
}
}
好了,下面是表现层,这里将实现Interface View
abstract ArticleView implements View
{
protected $smarty = null;
public function __construct()
{
$this->smarty = new Smarty();
///下面你可以定义smarty的一些属性值
}
}
具体的页面,例如科技文章的显示页面
class TechArticleView extends ArticleView
{
public function display()
{
//实现你的代码,调用Tech类和更多的DataBaseOperation子类
}
}
好了,下面是总入口 index.php
try
{
$viewController = new ViewController();
$viewController->push("tech",TechArticleView);
//持续的增加
$mod = $_GET["mod"]:$_GET["mod"]:$_POST["mod"];
//最后
$viewController->execute($key);
}
catch(Exception $e)
{
//如何处理异常就是你的事了
}