⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formmain.cs

📁 使用C#程序将23个常用设计模式进行列表显示
💻 CS
字号:
/************************************************************************/
/* Design Pattern Program                                               */
/* Copyright (C) 2006-2008 fujie(fishjam@163.com)                            */
/************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace DesignPattern
{
    public partial class FormMain : Form
    {

        public FormMain()
        {
            InitializeComponent();
        }

        enum SourceCodeMode
        {
            scmRealWorld,
            scmStructural
        };


        private CPatternList m_PatternList = new CPatternList();
        private SourceCodeMode m_codeMode = SourceCodeMode.scmRealWorld;

        public static FormMain s_MainForm = null;
        SourceCodeMode CodeMode
        {
            get
            {
                return m_codeMode;
            }
            set
            {
                m_codeMode = value;
            }
        }

        public static void OutputInfo(string strInfo)
        {
            if (s_MainForm != null)
            {
                s_MainForm.textPatternInfo.Text += strInfo + CStringConst.STR_CRLF;
            }
        }
        public static void OutputInfo(string format, params object[] arg)
        {
            OutputInfo(string.Format(Thread.CurrentThread.CurrentCulture, format, arg));
        }

        protected string SourceCodeModeString()
        {
            if (SourceCodeMode.scmRealWorld == this.m_codeMode)
            {
                return "RealWorld";
            }
            else
            {
                return "Structural";
            }
        }

        private void miRealWorld_Click(object sender, EventArgs e)
        {
            this.CodeMode = SourceCodeMode.scmRealWorld;
            miRealWorld.Checked = true;
            miStructural.Checked = false;

            miRealWorld.Enabled = false;
            miStructural.Enabled = true;

            DisplayDesignPattern();

        }

        private void miStructural_Click(object sender, EventArgs e)
        {
            this.CodeMode = SourceCodeMode.scmStructural;
            miRealWorld.Checked = false;
            miStructural.Checked = true;

            miRealWorld.Enabled = true;
            miStructural.Enabled = false;

            DisplayDesignPattern();

        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            for (Int32 i = 0; i < m_PatternList.Count; i++)
            {
                CPatternInfo patternInfo = (CPatternInfo)m_PatternList[i];
                string strNodeInfo = patternInfo.Index.ToString("00") + "." + patternInfo.ChineseName;
                TreeNode tnNewPattern = new TreeNode(strNodeInfo);
                tnNewPattern.Tag = patternInfo;
                treeDesignPattern.Nodes.Add(tnNewPattern);
            }

        }

        private void treeDesignPattern_AfterSelect(object sender, TreeViewEventArgs e)
        {
            DisplayDesignPattern();
        }

        private void miExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void miRunPattern_Click(object sender, EventArgs e)
        {
            miRunPattern.Checked = !miRunPattern.Checked;
            DisplayDesignPattern();

        }

        private void DisplayDesignPattern()
        {
            if (treeDesignPattern.SelectedNode != null)
            {
                try
                {
                    CPatternInfo patternInfo = (CPatternInfo)treeDesignPattern.SelectedNode.Tag;
                    if (miRunPattern.Checked)
                    {
                        Type patterType = Type.GetType("DesignPattern." + patternInfo.FileName + SourceCodeModeString()
                            + "." + patternInfo.FileName + SourceCodeModeString()); //"Structural"
                        if (patterType != null)
                        {
                            MethodInfo info = patterType.GetMethod("Run");
                            if (info != null)
                            {
                                info.Invoke(null, new object[] { textPatternInfo });
                                //patterType.InvokeMember(info.Name,BindingFlags.Default,null,null,null);
                            }
                        }
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();

                        //意图
                        sb.Append(CStringConst.STR_PURPOSE);
                        for (Int32 i = 0; i < patternInfo.Purpose.Count; i++)
                        {
                            sb.AppendFormat("{0}{1}{2}", CStringConst.STR_SPACE, patternInfo.Purpose[i], CStringConst.STR_CRLF);
                        }

                        //适用性
                        sb.Append(CStringConst.STR_CRLF + CStringConst.STR_APPLICABILITY);
                        for (Int32 i = 0; i < patternInfo.Applicability.Count; i++)
                        {
                            sb.AppendFormat("{0}{1}{2}", CStringConst.STR_SPACE, patternInfo.Applicability[i], CStringConst.STR_CRLF);
                        }

                        //使用的例子
                        sb.Append(CStringConst.STR_CRLF + CStringConst.STR_SAMPLE_USE);
                        for (Int32 i = 0; i < patternInfo.SampleUse.Count; i++)
                        {
                            sb.AppendFormat("{0}{1}{2}", CStringConst.STR_SPACE, patternInfo.SampleUse[i], CStringConst.STR_CRLF);
                        }

                        textPatternInfo.Text = sb.ToString();
                    }
                    
                    // Create an instance of StreamReader to read from a file.
                    // The using statement also closes the StreamReader.
                    StringBuilder sbSourceCode = new StringBuilder(1024 * 4);
                    using (StreamReader sr = new StreamReader("..\\..\\Patterns\\" + patternInfo.FileName + SourceCodeModeString()
                               + ".cs", System.Text.Encoding.Default))
                    {
                        String line;
                        // Read and display lines from the file until the end of 
                        // the file is reached.
                        while ((line = sr.ReadLine()) != null)
                        {
                            sbSourceCode.Append(line + CStringConst.STR_CRLF);
                        }
                        textSourceCode.Text = sbSourceCode.ToString();
                    }

                    picBoxUML.ImageLocation = "..\\..\\UML\\" + patternInfo.FileName + ".gif";
                    
                }
                catch (System.Exception ex)
                {
                    // Let the user know what went wrong.
                    MessageBox.Show(ex.Message + CStringConst.STR_CRLF + ex.Source);

                }
            }
        }


        /// <summary>
        /// 显示所有模式的概览图
        /// </summary>
        private void miAllPatterns_Click(object sender, EventArgs e)
        {
            
        }

        private void miAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Author:fishjam@163.com");
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -