本文小編為大家詳細(xì)介紹“java設(shè)計(jì)模式的抽象工廠模式怎么寫”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“java設(shè)計(jì)模式的抽象工廠模式怎么寫”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

public class UserEntity
{
public int ID { get; set; }
public string Name { get; set; }
}
public interface IUser
{
void InsertUser(UserEntity user);
UserEntity GetUser(int id);
}
class SqlUser: IUser
{
public void InsertUser(UserEntity user)
{
Console.WriteLine("在SQL中插入一個用戶");
}
public UserEntity GetUser(int id)
{
Console.WriteLine("在SQL中獲取一個用戶");
return null;
}
}
class AccessUser : IUser
{
public void InsertUser(UserEntity user)
{
Console.WriteLine("在Access中插入一個用戶");
}
public UserEntity GetUser(int id)
{
Console.WriteLine("在Access中獲取一個用戶");
return null;
}
}
//抽象工廠改簡單工廠->反射
public class SimpleFactory
{
private static string assemblyName = "抽象工廠";
private static string db = ConfigurationSettings.AppSettings["DB"];
public static IUser CreateUser()
{
//switch (db)
//{
// case "sql":return new SqlUser();
// case "access": return new AccessUser();
//}
//return null;
string className = assemblyName + "." + db + "User";
IUser iuser = (IUser)Assembly.Load(assemblyName).CreateInstance(className);
return iuser;
}
public static IDepartment CreateIDepartment()
{
string className = assemblyName + "." + db + "Department";
IDepartment department = (IDepartment)Assembly.Load(assemblyName).CreateInstance(className);
return department;
}
}
<appSettings>
<add key="DB" value="Product.Sql"/>
</appSettings>
static void Main(string[] args)
{
//UserEntity user = new UserEntity();
//IFactory factory = new AccessFactory();
//IUser su = factory.CreateUser();
//su.InsertUser(user);
//su.GetUser(1);
//DepartmentEntity department = new DepartmentEntity();
//IDepartment de = factory.CreateDepartment();
//de.InsertDepartment(department);
//de.GetDepartment(1);
//Console.ReadLine();
UserEntity user = new UserEntity();
DepartmentEntity department = new DepartmentEntity();
IUser su = SimpleFactory.CreateUser();
su.InsertUser(user);
su.GetUser(1);
IDepartment dep = SimpleFactory.CreateIDepartment();
dep.InsertDepartment(department);
dep.GetDepartment(1);
Console.ReadLine();
}讀到這里,這篇“java設(shè)計(jì)模式的抽象工廠模式怎么寫”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站制作公司行業(yè)資訊頻道。
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。
本文名稱:java設(shè)計(jì)模式的抽象工廠模式怎么寫-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://chinadenli.net/article8/cejiop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、ChatGPT、網(wǎng)站排名、外貿(mào)網(wǎng)站建設(shè)、域名注冊、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)