c#六大设计原则(以仪器代码为例) (3)

 

c#六大设计原则(以仪器代码为例)

从以上代码可以看出,用父类代替子类时,不能影响其子类的行为,否则,就违背了里氏替换原则。

五、接口隔离原则

简述:要让接口尽量简单,功能一致的可以写在仪器,与接口无关的必须重新写接口,也就是说,不要让接口变得臃肿。

C#代码如下:

    interface IInstrument

    {

        void Write(string command);

        string Read();

        double Voltage { get; }

        double Current { get; }

}

以上代码有两组,一组为Write、Read,另外一组为Voltage、Current,按照功能分类的话,Write、Read应属于端口的读写,而Voltage、Current则为电表的读电压、读电流,按照接口隔离原则,需要进行隔离。

修改后的代码如下:

    interface IPort

    {

        void Write(string command);

        string Read();

}

 

    interface IMeter

    {

        double Voltage { get; }

        double Current { get; }

}

六、迪米特法则

简述:类A、类B之间本身没有直接关联关系,那么就不能相互调用,可以通过第三个类(类C)进行间接调用,降低类A与类B的耦合性,提高模块的相对独立性。

C#代码如下:

    class DCPowerSupply

    {

 

        public Port Port { set; get; }

 

        public double Voltage

        {

            set

            {

                Port.Write($"Voltage:{value}");

            }

            get

            {

                return Convert.ToDouble(Port.Read());

            }

        }

        public double Current

        {

            set

            {

                Port.Write($"Current:{value}");

            }

            get

            {

                return Convert.ToDouble(Port.Read());

            }

        }

 

        public void On() { }

        public void Off() { }

    }

 

    class Port

    {

        public void Write(string command)

        {

 

        }

 

        public string Read()

        {

            return new Random().NextDouble().ToString();

        }

    }

以上代码,Port类与DCPowerSupply类进行了相互调用,耦合性很强,改变其中一个类的方法,对调用的那个类有很大的影响。

修改之后的代码如下:

    class DCPowerSupply

    {

        public double Voltage { set; get; }

        public double Current { set; get; }

 

        public void On() { }

        public void Off() { }

    }

 

    class Port

    {

        public void Write(string command)

        {

 

        }

 

        public string Read()

        {

            return new Random().NextDouble().ToString();

        }

    }

 

 

    class DCPowerSupplyWithPort

    {

        DCPowerSupply powerSupply;

        Port port;

        public DCPowerSupplyWithPort()

        {

            powerSupply = new DCPowerSupply();

            port = new Port();

        }

 

        public double Voltage

        {

            set

            {

                port.Write($"Voltage:{value}");

            }

            get

            {

                return Convert.ToDouble(port.Read());

            }

        }

 

        public double Current

        {

            set

            {

                port.Write($"Current:{value}");

            }

            get

            {

                return Convert.ToDouble(port.Read());

            }

        }

 

        public void On()

        {

            powerSupply.On();

        }

 

        public void Off()

        {

            powerSupply.Off();

        }

    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyfywf.html