大話設計模式 10抽象工廠模式

2021-09-12 01:57:33 字數 2454 閱讀 7100

一、最基本的資料庫訪問方式

public class oracleuserdaoimpl 

public user queryuser(int id)

}

public class sqlserveruserdaoimpl 

public user queryuser(int id)

}

public class user 

public void setid(string id)

public string getname()

public void setname(string name)

}

二、使用工廠方法模式

public inte***ce ifactory
public inte***ce iuserdao
public class oraclefactory implements ifactory 

}

public class sqlserve***ctory implements ifactory 

}

public class oracleuserdaoimpl implements iuserdao 

public user queryuser(int id)

}

public class sqlserveruserdaoimpl implements iuserdao 

public user queryuser(int id)

}

public class runmain 

}

三、使用抽象工廠模式

// 抽象工廠介面,包含所有產品建立的抽象方法

public inte***ce ifactory

// 抽象的產品,有兩種不同的實現

public inte***ce ideptdao

public class dept
// 具體工廠

public class oraclefactory implements ifactory

@override

public ideptdao createdeptdao()

}

public class sqlserve***ctory implements ifactory 

public ideptdao createdeptdao()

}

public class oracledeptdaoimpl implements ideptdao 

@override

public dept querydept(int deptid)

}

public class sqlserverdeptdaoimpl implements ideptdao 

@override

public dept querydept(int deptid)

}

// 工廠方法模式:定義乙個用於建立物件的介面,讓子類決定例項化哪乙個類

public class runmain

}

四、加入反射

public class dataaccess  catch (ioexception e) 

} public static iuserdao createuserdao() throws instantiationexception, illegalacces***ception, classnotfoundexception

public static ideptdao createdeptdao() throws instantiationexception, illegalacces***ception, classnotfoundexception

}

#db.properties

assemblyname = com.talkdesignpattern.demo11.branch02

db = oracle

#db = sqlserver

public class runmain 

}

大話設計模式 抽象工廠模式

1 思想 提供乙個建立一系列相關或互相依賴物件的介面 抽象工廠類 而無需指定它們具體的類。抽象工廠模式和工廠模式一樣都是屬於建立型模式,也就是說它的目的也是用來建立物件的,為什麼有了工廠模式之後還要有抽象工廠模式?因為更方便 更合理 更加低耦合,我舉個例子來說明一下。假設目前你的程式裡面有兩個物件,...

《大話設計模式》抽象工廠模式

抽象工廠模式 abstract factory 提供乙個建立一系列相關或相互依賴物件的介面,而無需指定它們具體的類。優點 缺點 產品1 父類 type cat struct 產品1 子類 需實現介面 type catinte ce inte ce 產品1的具體 子類1 type smallcat s...

大話設計模式 抽象工廠模式 簡單工廠模式結合

user表 class user public function get key else department表 class department public function get key else user表操作介面 inte ce iuser sqlserver class sqlser...