面向对象的设计准则--Principles Of Object-Oriented Design

  1. 开放封闭原则  Software entities (classes, modules, etc) should be open for extension, but closed for modification. 软件实体像类,模块等应该对扩展是开放的,而对修改是封闭的。
  2. 里氏替换原则 Derived classes must be usable through the base class interface without the need for the user to know the difference.
  3. 依赖倒置原则 Details should depend upon abstractions. Abstractions should not depend upon details.
     1  public interface IProgrammer 
     2    {
     3        void Coding();
     4        void Pay();
     5    }

     6
     7    public class Programmer : IProgrammer
     8    {
     9        public void Coding() { }
    10        public void Pay() { }
    11    }

    12
    13    public abstract class ManagerBase
    14    {
    15        public abstract void GetName(IProgrammer programmer);
    16    }

    17
    18    public class Manager : ManagerBase
    19    {
    20        public override void GetName(IProgrammer programmer)
    21        {
    22            throw new Exception("The method or operation is not implemented.");
    23        }

    24    }

    25
    26    public class Client
    27    {
    28        public Manager se = new Manager();
    29
    30        public void CallLib()
    31        {
    32            se.GetName(new Programmer());
    33        }

    34    }
  4. 接口分离原则 Many client specific interfaces are better than one general purpose interface.
  5. 单一职责原则 A class should have only one reason to change.类仅有一个原因去修改。
     1 public interface IPrinter
     2    {
     3        string Read();
     4        void Write(string content);
     5        void Monitor(string format);
     6    }

     7
     8    public class Printer : IPrinter
     9    {
    10        public string Read()
    11        {
    12            return null;
    13        }

    14
    15        public void Write(string content)
    16        {
    17        }

    18
    19        public void Monitor(string format)
    20        {
    21        }

    22    }

    23
    24  
    25    public interface IPrinter2 
    26    {
    27        string Read();
    28        void Write(string content);
    29        void Monitor(IMeasure format);
    30    }

    31
    32    public interface IMeasure 
    33    {
    34        string Monitor();
    35    }

    36
    37    public class Printer2 : IPrinter2
    38    {
    39        public string Read()
    40        {
    41            return null;
    42        }

    43
    44        public void Write(string content)
    45        {
    46        }

    47
    48        public void Monitor(IMeasure format)
    49        {
    50        }

    51    }
  6. ...

http://www.tek271.com/articles/pood/PrinciplesOfOOD.java.html
http://ootips.org/ood-principles.html
http://www.oodesign.com/oo_principles/oo_principles/single_responsibility_principle.html

posted @ 2007-08-07 11:27  许晓光  阅读(307)  评论(0编辑  收藏  举报