一次讓代碼更適應變化的經歷

通常狀況下,我寫的程序在界面裝載時,讀取配置文件中的各個項目,中間使用或修改後,在界面退出時,保存一下配置文件。程序員

在還不是面對對象年代,基本上都會把讀配置文件中的一個項目、寫配置文件中的一個項目都寫成一個子程序,這樣調用方便。當須要多增長新的一個項目時,只要在界面裝載、界面退出時,各增長一行。當時感受已是不錯的處理方案了。編程

Program.cs 中「讀寫INI的API」代碼設計模式

        // 聲明INI文件的寫操做函數 WritePrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int WritePrivateProfileString(string section, string key, string val, string filePath);

        // 聲明INI文件的讀操做函數 GetPrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);

        public static bool bWriteINIValue(string sSection, string sKeyName, string sText, string sINIFileName)
        {
            int lRet = WritePrivateProfileString(sSection, sKeyName, sText, sINIFileName);
            if (lRet == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static string sGetINIValue(string sSection, string sKeyName, string sDefault, string sINIFileName)
        {
            int lRet;
            System.Text.StringBuilder sTemp = new StringBuilder(255);
            lRet = GetPrivateProfileString(sSection, sKeyName, sDefault, sTemp, 255, sINIFileName);
            if (lRet == 0)
            {
                return string.Empty;
            }
            else
            {
                return sTemp.ToString();
            }
        }
View Code

Form1.cs 代碼(界面還放一個按鈕)ide

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        string sXM1 = string.Empty;
        string sXM2 = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            sXM1 = sGetINIValue("Normal", "XM1", "", Application.StartupPath + "\\Para.ini");
            sXM2 = sGetINIValue("Normal", "XM2", "", Application.StartupPath + "\\Para.ini");
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            bWriteINIValue("Normal", "XM1", sXM1, Application.StartupPath + "\\Para.ini");
            bWriteINIValue("Normal", "XM2", sXM2, Application.StartupPath + "\\Para.ini");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sTemp = sXM1;
            MessageBox.Show(sTemp);
            sXM2 = DateTime.Now.ToString("hh:mm:ss");
        }
    }
}

它存在的問題是:程序員要知道界面裝載時讀取(通常這個會記得),同時也要知道界面退出時要保存(甚至有時並不全在界面退出時保存,可能有個保存按鈕的點擊事件中處理)。不然程序員常常寫的代碼就是忘記保存。函數

面對對象的編程中,可否實如今程序的一個地方設置後,程序員就能夠沒必要管其它地方的代碼?ui

我想出來的解決方案以下:
一、創建一個項目類 —— ClsXM,它有 名稱,默認值,當前值(爲代碼簡單,使用變量而不是使用屬性)
二、創建項目類集合 —— ClsXMJH,實現AddRange、GetAllValue、SaveAllValue、索引器(我刻意不實現 IList、IDictionary接口,由於此次的需求比較少)this

ClsXM.cs代碼spa

using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    class ClsXM
    {
        public string Name = string.Empty;
        public string Default = string.Empty;
        public string Value = string.Empty;

        public ClsXM(string sName)
        {
            this.Name = sName;
        }

        public void GetValue()
        {
            Value = Program.sGetINIValue("Normal", Name, Default, Application.StartupPath + "\\Para.ini");
        }

        public void SaveValue()
        {
            Program.bWriteINIValue("Normal", Name, Value, Application.StartupPath + "\\Para.ini");
        }
    }
}

ClsXMJH.cs 代碼設計

using System.Collections.Generic;

namespace WindowsFormsApplication12
{
    class ClsXMJH
    {
        List<ClsXM> oXM = new List<ClsXM>();

        public void AddRange(params ClsXM[] oItems)
        {
            foreach (ClsXM oTemp in oItems)
            {
                oXM.Add(oTemp);
            }
        }

        public void GetAllValue()
        {
            foreach (ClsXM oTempXM in oXM)
            {
                oTempXM.GetValue();
            }
        }

        public void SaveAllValue()
        {
            foreach (ClsXM oTempXM in oXM)
            {
                oTempXM.SaveValue();
            }
        }

        public ClsXM this[string sName]
        {
            get
            {
                ClsXM oRet = null;
                foreach (ClsXM oTempXM in oXM)
                {
                    if (oTempXM.Name == sName)
                    {
                        oRet = oTempXM;
                        break;
                    }
                }

                return oRet;
            }
        }
    }
}

Form1.cs 改後的代碼code

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        ClsXMJH oXMJH = new ClsXMJH();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            oXMJH.AddRange(new ClsXM("XM1"), new ClsXM("XM2"));

            oXMJH.GetAllValue();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            oXMJH.SaveAllValue();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sTemp = oXMJH["XM1"].Value;
            MessageBox.Show(sTemp);
            oXMJH["XM2"].Value = DateTime.Now.ToString("hh:mm:ss");
        }
    }
}

如今想問的是這是不是正確的解決方案?是否還存在更好的解決方案?是否有與此相關的設計模式,它叫什麼名稱?

相關文章
相關標籤/搜索