工廠模式是我們最常用的實例化對象模式了,是用工廠方法代替new操作的一種模式。著名的Jive論壇 ,就大量使用了工廠模式,工廠模式在Java程式系統可以說是隨處可見。因為工廠模式就相當於創建實例對象的new,我們經常要根據類Class生成實例對象,如A a=new A() 工廠模式也是用來創建實例對象的,所以以後new時就要多個心眼,是否可以考慮使用工廠模式,雖然這樣做,可能多做一些工作,但會給你系統帶來更大的可擴展性和儘量少的修改量。
基本介紹
- 中文名:工廠模式
- 外文名:factory pattern
- 本質:工廠方法代替new操作
- 類別:管理模式
- 俗稱:實例化對象模式
- 實例:Jive論壇
編程開發
簡單工廠
public class Factory{ public static ISample creator(int which){ if (which==1) return new SampleA(); else if (which==2) return new SampleB(); }}
抽象工廠
public abstract class Factory{ public abstract Sample creator(); public abstract Sample2 creator(String name);}public class SimpleFactory extends Factory{ public Sample creator(){ ......... return new SampleA } public Sample2 creator(String name){ ......... return new Sample2A }}public class BombFactory extends Factory{ public Sample creator(){ ...... return new SampleB } public Sample2 creator(String name){ ...... return new Sample2B }}
public abstract class ForumFactory { private static Object initLock = new Object(); private static String className = "com.jivesoftware.forum.database.DbForumFactory"; private static ForumFactory factory = null; public static ForumFactory getInstance(Authorization authorization) { //If no valid authorization passed in, return null. if (authorization == null) { return null; } //以下使用了Singleton單態模式 if (factory == null) { synchronized(initLock) { if (factory == null) { ...... try { //動態轉載類 Class c = Class.forName(className); factory = (ForumFactory)c.newInstance(); }catch (Exception e) { return null; } } } } //Now, 返回 proxy.用來限制授權對forum的訪問 return new ForumFactoryProxy(authorization, factory, factory.getPermissions(authorization)); } //真正創建forum的方法由繼承forumfactory的子類去完成. public abstract Forum createForum(String name, String description) throws UnauthorizedException, ForumAlreadyExistsException; ....}
public class CatalogDAOFactory { /** * 本方法制定一個特別的子類來實現DAO模式。 * 具體子類定義是在J2EE的部署描述器中。 */ public static CatalogDAO getDAO() throws CatalogDAOSysException { CatalogDAO catDao = null; try { InitialContext ic = new InitialContext(); //動態裝入CATALOG_DAO_CLASS //可以定義自己的CATALOG_DAO_CLASS,從而在無需變更太多代碼 //的前提下,完成系統的巨大變更。 String className =(String) ic.lookup(JNDINames.CATALOG_DAO_CLASS); catDao = (CatalogDAO) Class.forName(className).newInstance(); } catch (NamingException ne) { throw new CatalogDAOSysException(" CatalogDAOFactory.getDAO: NamingException while getting DAO type : \n" + ne.getMessage()); } catch (Exception se) { throw new CatalogDAOSysException(" CatalogDAOFactory.getDAO: Exception while getting DAO type : \n" + se.getMessage()); } return catDao; }}
PHP
<?php interface abstracted{ public function realCreate(); } //女人類 class Woman{ public function action(){ echo '這是女人'; } } //男人類 class Man{ public function action(){ echo '這是男人'; } } //創建女人 class WomanCreator implements abstracted { public $chromosome;//染色體 public function realCreate(){ if ($this->chromosome == "xx") { return new Woman(); } } } //創建男人 class ManCreator implements abstracted { public $chromosome; public function realCreate(){ if ($this->chromosome == "xy" || $this->chromosome == "xyy") { return new Man(); } } } //人類工廠 class PersonFactory{ public function create($what){ $create = $what."Creator"; return $create = new $create(); } } $create = new PersonFactory(); $instance = $create->create('Woman'); $instance->chromosome = "xx"; $instance->realCreate()->action();?>