C# object-oriented design principles (1) opening and closing principles

1. Definition of opening and closing principle:

1. The software entity should be open for extension and closed for modification.
2. When the application requirements change, the function of the module can be expanded to meet the new requirements without modifying the source code and binary code of the software entity.
3. Software entities include: modules, classes and interfaces, methods divided in the project

2. The role of the principle of opening and closing:

1. It can improve the reusability of the code
2. It can improve the maintainability of the software

3. Realization of the principle of opening and closing:

1. The principle of opening and closing can be realized through "abstract constraints and encapsulation changes", that is, a relatively stable abstraction layer is defined for software through interfaces or abstract classes, and the variable factors are encapsulated in the same concrete realization class.
2. Because of abstraction Good flexibility and wide adaptability. As long as the abstraction is reasonable, the stability of the software architecture can be basically maintained, and the changeable details in the software can be extended from the implementation classes derived from the abstraction. When the software mode changes, it only needs to be rebuilt according to the needs. Just derive an implementation class


Insert picture description here
Insert picture description here
Insert picture description here

  Type typeInterface;

        private ArrayList plugins = new ArrayList();//存放插件
        int counts = 0;

        private void Form1_Load(object sender, EventArgs e)
        {
    
    
            InterfaceByNote editor;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;//固定窗体
            #region 03
            //先获取程序集目录路径
            string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Addons");
            string[] interpath = Directory.GetFiles(path, "*.dll");
            for (int i = 0; i < interpath.Length; i++)//循环每个dll文件
            {
    
    
                Assembly asm = Assembly.LoadFile(interpath[i]);
                //获取程序集里的所有公共类
                Type[] types=asm.GetExportedTypes();
                typeInterface = typeof(InterfaceByNote);//获取接口的type
                for (int r = 0; r < types.Length; r++)
                {
    
    
                    //循环遍历每个类,判断当前类是否实现了接口
                    if (typeInterface.IsAssignableFrom(types[r])&&!types[r].IsAbstract)
                    {
    
    
                        editor = (InterfaceByNote)Activator.CreateInstance(types[r]);//创建对象并保存到接口类型集合
                        ToolStripItem T=this.编辑EToolStripMenuItem.DropDownItems.Add(editor.Name.ToString());
                        plugins.Add(editor);
                        T.Tag = counts;
                        counts++;
                        T.Click += T_Click1;
                    }
                }
            }
            #endregion
        }
        private void T_Click1(object sender, EventArgs e)
        {
    
    
            ToolStripItem s=sender as ToolStripItem;
            InterfaceByNote st=(InterfaceByNote)plugins[Convert.ToInt32(s.Tag)];
            st.Run(this.textBox1);
            
        }

Notebook plug-in development-open and close principle

Guess you like

Origin blog.csdn.net/MrLsss/article/details/109230502