Facade Pattern: 將一些相同屬性的類別重新打包整理成一個新的類別, 方便管理
下面例子Applications和Computers類別包裝成一個新的類別MyFacade,
在預設建構子階段, 初始化兩個成員資料, 分別為Applications和Computers物件實體化,
並提供兩個方法讓使用者呼叫, 可以擇一取出
加入參考Microsoft.VisualBasic
1: using Microsoft.VisualBasic.ApplicationServices;
2: using Microsoft.VisualBasic.Devices;
1: // (1) Applications
2: public class Applications
3: {
4: private WindowsFormsApplicationBase m_MyApplication;
5: // 預設建構子
6: public Applications()
7: {
8: m_MyApplication = new WindowsFormsApplicationBase();
9: }
10: public AssemblyInfo Info
11: {
12: get
13: {
14: AssemblyInfo Info = new AssemblyInfo(System.Reflection.Assembly.GetExecutingAssembly());
15: return Info;
16: }
17: }
18: }
1: // (2) Computers
2: public class Computers
3: {
4: private ComputerInfo m_computerInfo;
5: // 預設建構子
6: public Computers() { this.m_computerInfo = new ComputerInfo(); }
7: public ComputerInfo Info
8: {
9: get
10: {
11: ComputerInfo cInfo = new ComputerInfo();
12: return cInfo;
13: }
14: }
15: }
1: //-----------------------------------------------------------------------------
2: // (1)+(2)
3: public class MyFacade
4: {
5: Applications m_apps;
6: Computers m_pc;
7: public MyFacade()
8: {
9: m_apps = new Applications();
10: m_pc = new Computers();
11: }
12: public Applications Applications { get { return m_apps; } }
13: public Computers Computers{ get{return m_pc;} }
14: }
-----------------------------------------------------------------------------------------------------------------------------------
1: private void button1_Click(object sender, EventArgs e)
2: {
3: // 原始呼叫
4: Applications apps = new Applications();
5: MessageBox.Show(apps.Info.Copyright);
6: // Facade Pattern: 將一些相同屬性的類別重新打包整理成一個新的類別, 方便管理
7: // 間接呼叫
8: MyFacade myApp = new MyFacade();
9: MessageBox.Show(myApp.Computers.Info.OSFullName);
10: MessageBox.Show(myApp.Applications.Info.Copyright);
11: }
參考資料: Visual C#程式設計實例演練與系統開發2013 許清榮
全站熱搜
留言列表