面向对象设计的基本原则 单一职责 系统中的每一个对象应该只有一个单独的职责,所有对象关注的应该是自身职责的完成。 基本思想:高内聚,低耦合。 开闭原则 一个对象对扩展开放,对修改关闭。 基本思想:对类的改动是通过增加代码进行的,而不是修改现有的
面向对象设计的基本原则
设计分类
简单工厂模式
简单工厂模式是指专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。
从图中我们可以清楚的看到,该模式中主要包含下面3种角色:
它是工厂模式的核心,负责实现创建所有势力的内部逻辑。工厂类可以被外界直接调用,创建所需的产品的对象。
简单工厂模式所创建的所有对象的父类,负责描述所有实例所共有的公共接口。
是该模式的创建目标,所有创建的对象都是充当这个角色的某个具体类的实例。一般来讲是抽象产品类的子类,实现了抽象产品类中定义的所有接口方法。
举个例子:
代码说明一切
<span>//</span><span>Animal.java 抽象产品角色</span> <span>public</span> <span>abstract</span> <span>class</span><span> Animal { </span><span>public</span> <span>abstract</span> <span>void</span><span> eat(); }</span>
<span>//</span><span>Dog.java 具体产品角色</span> <span>public</span> <span>class</span> Dog <span>extends</span><span> Animal { @Override </span><span>public</span> <span>void</span><span> eat() { System.out.println(</span>"dog eat"<span>); } }</span>
<span>//</span><span>Pig.java 具体产品角色</span> <span>public</span> <span>class</span> Pig <span>extends</span><span> Animal { @Override </span><span>public</span> <span>void</span><span> eat() { System.out.println(</span>"pig eat"<span>); } }</span>
<span>//</span><span>AnimalFactory.java 工厂角色</span> <span>public</span> <span>class</span><span> AnimalFactory { </span><span>private</span><span> AnimalFactory(){} </span><span>public</span> <span>static</span><span> Animal animalFactory(String string) { </span><span>if</span>(string.equals("dog"<span>)) </span><span>return</span> <span>new</span><span> Dog(); </span><span>else</span> <span>if</span>(string.equals("pig"<span>)) </span><span>return</span> <span>new</span><span> Pig(); </span><span>else</span> <span>return</span> <span>null</span><span>; } }</span>
<span>//</span><span>AnimalTest.java 客户端调用</span> <span>public</span> <span>class</span><span> AnimalTest { </span><span>public</span> <span>static</span> <span>void</span><span> main(String[] args) { Animal dog </span>= AnimalFactory.animalFactory("dog"<span>); dog.eat(); Animal pig </span>= AnimalFactory.animalFactory("pig"<span>); pig.eat(); Animal ani </span>= AnimalFactory.animalFactory("dog"<span>); ani.eat(); ani </span>= AnimalFactory.animalFactory("pig"<span>); ani.eat(); } }</span>
优缺点
对象的创建比较复杂的时候,就考虑使用工厂来实现。
优点:在简单工厂模式中,客户端不再负责对象的创建,而是把这个责任丢给了工厂类,客户端值负责对象的调用,从而明确了各个类的职责(单一职责)。
缺点:由于这个工厂类负责所以的对象的创建,那么当子类不断增多的时候,我们就需要去修改工厂的代码,这样就违反了开闭原则。
PHP
<span>php </span><span>define</span>('DB_TYPE','pdo'<span>); </span><span>interface</span><span> IDB{ </span><span>function</span><span> select(); } </span><span>class</span> DBMysql <span>implements</span><span> IDB{ </span><span>private</span> <span>$tableName</span><span>; </span><span>private</span> <span>$link</span><span>; </span><span>function</span> __construct(<span>$tableName</span><span>){ </span><span>$this</span>->tableName=<span>$tableName</span><span>; </span><span>$this</span>->link=<span>mysql_connect</span>('localhost','root',''<span>); </span><span>mysql_select_db</span>('base',<span>$this</span>-><span>link); } </span><span>function</span><span> select(){ </span><span>//</span><span>从数据库中获取数据</span> <span>$sql</span>="select * from {<span>$this</span>->tableName}"<span>; </span><span>$result</span>=<span>mysql_query</span>(<span>$sql</span><span>); </span><span>if</span>(<span>$result</span> && <span>mysql_num_rows</span>(<span>$result</span>)>0<span>){ </span><span>while</span>(<span>$row</span>=<span>mysql_fetch_assoc</span>(<span>$result</span><span>)){ </span><span>$data</span>[]=<span>$row</span><span>; } </span><span>return</span> <span>$data</span><span>; }</span><span>else</span><span>{ </span><span>return</span> <span>false</span><span>; } } } </span><span>class</span> DBPdo <span>implements</span><span> IDB{ </span><span>private</span> <span>$tableName</span><span>; </span><span>private</span> <span>$handler</span><span>; </span><span>function</span> __construct(<span>$tableName</span><span>){ </span><span>$this</span>->tableName=<span>$tableName</span><span>; </span><span>$dsn</span>="mysql:host=localhost;dbname=ztz"<span>; </span><span>$this</span>->handler=<span>new</span> Pdo(<span>$dsn</span>,'root',''<span>); } </span><span>function</span><span> select(){ </span><span>//</span><span>从数据库中获取数据</span> <span>$sql</span>="select * from {<span>$this</span>->tableName}"<span>; </span><span>$stmt</span>=<span>$this</span>->handler->query(<span>$sql</span><span>); </span><span>return</span> <span>$stmt</span>->fetchAll(PDO::<span>FETCH_ASSOC); } } </span><span>class</span><span> DBFactory{ </span><span>public</span> <span>static</span> <span>function</span> getInstance(<span>$tableName</span>='factory'<span>){ </span><span>$class</span>='DB'.<span>DB_TYPE; </span><span>$obj</span>=<span>new</span> <span>$class</span>(<span>$tableName</span><span>); </span><span>if</span>(<span>$obj</span><span> instanceof IDB){ </span><span>return</span> <span>$obj</span><span>; }</span><span>else</span><span>{ </span><span>die</span>('该类没有能实现IDB接口'<span>); } } } </span><span>$m</span>=DBFactory::<span>getInstance(); </span><span>$arr</span>=<span>$m</span>-><span>select(); </span><span>echo</span> "<pre class="brush:php;toolbar:false">"<span>; </span><span>print_r</span>(<span>$arr</span><span>); </span><span>echo</span> ""; ?>
PHP的简单工厂和Java的意思差不多,但是感觉PHP灵活的多。这里是考虑的是数据库连接,如果换服务器,数据库也跟着换了,那么数据库的连接怎么办,不可能每次都边吧,所以这里就用简单工厂来实现不同数据库的连接。
我是天王盖地虎的分割线
源代码:http://pan.baidu.com/s/1dD1Qx01
java工厂.zip