跳到主要內容

抽象工廠模式(Abstract Factory)

  • 提供一個建立一系列相關或相互依賴物件的介面,而無須指定它們具體的類別。


用簡單工廠來改進抽象工廠


<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <appsettings>
  <add key="DB" value="Access">
 </add></appsettings>
</configuration>

using System;
using System.Reflection;
using System.Configuration;

namespace DPExample
{
 class User
 {
  private int _id;
  public int ID {
   get { return _id; }
   set { _id = value; }
  }

  private string _name;
  public string Name {
   get { return _name; }
   set { _name = value; }
  }
 }

 class Department
 {
  private int _id;
  public int ID {
   get { return _id; }
   set { _id = value; }
  }

  private string _deptName;
  public string DeptName {
   get { return _deptName; }
   set { _deptName = value; }
  }
 }

 interface IUser
 {
  void Insert(User user);
  User GetUser(int id);
 }

 class SqlserverUser : IUser
 {
  public void Insert (User user)
  {
   Console.WriteLine ("在 SQL Server 中給 User 表增加一條紀錄。");
  }

  public User GetUser (int id)
  {
   Console.WriteLine ("在 SQL Server 中根據 ID 得到 User 表一條紀錄。");
   return null;
  }
 }

 class AccessUser : IUser
 {
  public void Insert (User user)
  {
   Console.WriteLine ("在 Access 中給 User 表增加一條紀錄。");
  }

  public User GetUser (int id)
  {
   Console.WriteLine ("在 Access 中根據 ID 得到 Department 表一條紀錄。");
   return null;
  }
 }

 interface IDepartment
 {
  void Insert(Department dept);
  Department GetDepartment(int id);
 }

 class SqlserverDepartment : IDepartment
 {
  public void Insert (Department dept)
  {
   Console.WriteLine ("在 SQL Server 中給 Department 表增加一條紀錄。");
  }

  public Department GetDepartment (int id)
  {
   Console.WriteLine ("在 SQL Server 中根據 ID 得到 Department 表一條紀錄。");
   return null;
  }
 }

 class AccessDepartment : IDepartment
 {
  public void Insert (Department dept)
  {
   Console.WriteLine ("在 Access 中給 Department 表增加一條紀錄。");
  }

  public Department GetDepartment (int id)
  {
   Console.WriteLine ("在 Access 中根據 ID 得到 Department 表一條紀錄。");
   return null;
  }
 }

 class DataAccess
 {
  private static readonly string AssemblyName = "DPExample";
  private static readonly string db = ConfigurationManager.AppSettings["DB"];

  public static IUser CreateUser ()
  {
   string className = AssemblyName + "." + db + "User";
   IUser result = (IUser)Assembly.Load (AssemblyName).CreateInstance (className);
   return result;
  }

  public static IDepartment CreateDepartment ()
  {
   string className = AssemblyName + "." + db + "Department";
   IDepartment result = (IDepartment)Assembly.Load (AssemblyName).CreateInstance (className);
   return result;
  }
 }
}

留言

這個網誌中的熱門文章

用 C# 批次控制 Word 合併列印

前由 我有全區的電話資料,問題在於我要依不同里別來製作出電話簿。結果如下圖: 單純採用合併列印無法達成我的需求。解決方法係用「功能變數」儲存上一個里別,與現在里別進行比較:若不同,則換頁。不過,這樣功能變數還蠻長的。最後,我還是採用 C# 來解決。 解決方案 用 C# 控制 WORD 中合併列印的「資料來源 Data Source」,給予不同里別的「sqlstatement」。迴圈處理不同的里別即可。但可預見其處理過程會很慢,不過還好,我可以不用在意它,有跑出結果即可。 程式碼 IList<string> areas = new List<string>() { "後壁", "侯伯", "嘉苳", "土溝", "嘉田", "嘉民", "菁豊", "崁頂", "後廍", "墨林", "菁寮", "新嘉", "頂長", "平安", "仕安", "竹新", "新東", "長安", "頂安", "福安", "烏樹" }; string root = @"D:\"; // 根目錄 string data = root + @"\data.docm"; // 資料檔(即資料來源) string template = root + @"\template.docx"; // 已設定好格式與合併欄位的 Word 檔 string output = @"d:\Final"; // 輸出之資料夾 object oMissing = System.Reflection.Missing.Va...

VLC c# 順利編譯

原文網址: http://www.cnblogs.com/haibindev/archive/2011/12/21/2296173.html 原文作者: haibindev 原文標題:c#万能视频播放器 本文的重點在於修正 class VlcPlayer,使其能順利在 VC# Express 2010 .Net Framework 4 下順利編譯。 修正重點在於 CallingConvention = CallingConvention. StdCall 改成 CallingConvention = CallingConvention. Cdecl using System; using System.Runtime.InteropServices; using System.Security; using System.Text; namespace VlcDotNet { class VlcPlayer { private IntPtr libvlc_instance_; private IntPtr libvlc_media_player_; private double duration_; public VlcPlayer(string pluginPath) { string plugin_arg = "--plugin-path=" + pluginPath; string[] arguments = { "-I", "dummy", "--ignore-config", "--no-video-title", plugin_arg }; libvlc_instance_ = LibVlcAPI.libvlc_new(arguments); libvlc_media_player_ = LibVlcAPI.libvlc_media_player_new(libvlc_instance_); } public ...