.Net Module Encapsulation

Keywords: calculator Programming

Module Encapsulation

Programming

Calculator implemented with object-oriented logic

 class Calcuator
    {
        public  int Add(int i, int j)
        {
            return i + j;
        }
       public int Mul(int i, int j)
        {
            return i - j;
        }
        public int Max(int i, int j)
        {
            return i * j;
        }
        public int Div(int i, int j)
        {
            return i / j;
        }
    }
  private void button1_Click(object sender, EventArgs e)
        {
            int num1 = int.Parse(textBox1.Text);
            int num2 = int.Parse(textBox2.Text);
            string type = comboBox1.SelectedItem.ToString();

            Calcuator calc = new Calcuator();
            if (type=="+")
            {
                label2.Text =calc.Add(num1, num2).ToString();
            }
            else if (type=="-")
            {
                label2.Text = calc.Mul(num1, num2).ToString();
            }
        }

Introduction of new issues

First, it was the first version of the software that was able to perform basic operations on integers.Many customers have been using the software since it came online.

But many users ask questions:

  1. Divide operation cannot find decimal part
  2. Can I add more functionality if I have fewer operations
  3. The last calculator class was written by programmer A, and now problem solving can only be maintained by programmer B

Key Issues: The last calculator class was written by programmer A. Now programmer B maintains all programs except Calculator class. The client's request needs to be changed, but programmer B does not know the source code that A writes. If the calculator class needs to be changed, it can only be done by A. It has nothing to do with B. If you are programmer A, how to solve this hidden danger.

Problem point: Code written by two people in the same project does not meet customer requirements

Causes the final problem: If the program changes are completed, the first version of the program will be offline first, then the second version of the new program will be online, users will have a lot of troubles: 1.0, 1.1, 1.2, 1.3,...Wait for a series of versions,

How to solve the above series of problems

Use module encapsulation

  1. Right-click Solution >> Select New Project >(.NET Framework) Class Library

    2 Add Calc class to class library and write method
public class Calc
    {
        /// <summary>
        ///addition operation
        /// </summary>
        /// <param name="i">parameter 1</param>
        /// <param name="j">parameter 2</param>
        /// <returns></returns>
        public int Add(int i, int j)
        {
            return i + j;
        }
        /// <summary>
        ///subtraction operation
        /// </summary>
        /// <param name="i">parameter 1</param>
        /// <param name="j">parameter 2</param>
        /// <returns></returns>
        public int Mul(int i, int j)
        {
            return i - j;
        }
        /// <summary>
        ///Multiplication
        /// </summary>
        /// <param name="i">parameter 1</param>
        /// <param name="j">parameter 2</param>
        /// <returns></returns>
        public int Max(int i, int j)
        {
            return i * j;
        }
        /// <summary>
        ///Division
        /// </summary>
        /// <param name="i"></param>
        /// <param name="j"></param>
        /// <returns></returns>
        public double Div(int i, int j)
        {
            return  Convert.ToDouble(i) /Convert.ToDouble(j);
        }
    }

3. Right-click the Generate Class Library Project and open the Class Library Program Folder Debug to find the corresponding class library.dll file, which is a dynamic LinkLibrary) dynamically picks up library files that contain all the classes and methods we add to the library to save the results of compiling their internal C#code (dll is a set of machine languages that can be executed directly on the.NET platform)

4. Add a reference to the Calulator.dll file in the main project

  1. Use Calc objects from the Calculator class library in your project
  Calc calc = new Calc();
        private void button1_Click(object sender, EventArgs e)
        {
            int num1 = int.Parse(textBox1.Text);
            int num2 = int.Parse(textBox2.Text);
            string type = comboBox1.SelectedItem.ToString();
            if (type=="+")
            {
                label2.Text = calc.Add(num1,num2).ToString();
            }
            else if (type=="-")
            {
                label2.Text =calc.Mul(num1,num2).ToString();
            }
            else if (type=="×")
            {
                label2.Text =calc.Max(num1,num2).ToString();
            }
            else if (type=="÷")
            {
                label2.Text = calc.Div(num1, num2).ToString() ;
            }
        }

Application of Module Encapsulation

Check for Updates - "Download Files -"Upgrade Replacement - "Apply New Modules

110 original articles published, 165 praised, 10,000 visits+
Private letter follow

Posted by latinofever on Mon, 02 Mar 2020 17:26:01 -0800