面向对象的设计准则--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 None.gif    public   interface  IProgrammer 
     2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
     3InBlock.gif        void Coding();
     4InBlock.gif        void Pay();
     5ExpandedBlockEnd.gif    }

     6 None.gif
     7 None.gif     public   class  Programmer : IProgrammer
     8 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
     9ExpandedSubBlockStart.gifContractedSubBlock.gif        public void Coding() dot.gif{ }
    10ExpandedSubBlockStart.gifContractedSubBlock.gif        public void Pay() dot.gif{ }
    11ExpandedBlockEnd.gif    }

    12 None.gif
    13 None.gif     public   abstract   class  ManagerBase
    14 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
    15InBlock.gif        public abstract void GetName(IProgrammer programmer);
    16ExpandedBlockEnd.gif    }

    17 None.gif
    18 None.gif     public   class  Manager : ManagerBase
    19 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
    20InBlock.gif        public override void GetName(IProgrammer programmer)
    21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
    22InBlock.gif            throw new Exception("The method or operation is not implemented.");
    23ExpandedSubBlockEnd.gif        }

    24ExpandedBlockEnd.gif    }

    25 None.gif
    26 None.gif     public   class  Client
    27 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
    28InBlock.gif        public Manager se = new Manager();
    29InBlock.gif
    30InBlock.gif        public void CallLib()
    31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
    32InBlock.gif            se.GetName(new Programmer());
    33ExpandedSubBlockEnd.gif        }

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

     7 None.gif
     8 None.gif     public   class  Printer : IPrinter
     9 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
    10InBlock.gif        public string Read()
    11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
    12InBlock.gif            return null;
    13ExpandedSubBlockEnd.gif        }

    14InBlock.gif
    15InBlock.gif        public void Write(string content)
    16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
    17ExpandedSubBlockEnd.gif        }

    18InBlock.gif
    19InBlock.gif        public void Monitor(string format)
    20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
    21ExpandedSubBlockEnd.gif        }

    22ExpandedBlockEnd.gif    }

    23 None.gif
    24 None.gif  
    25 None.gif     public   interface  IPrinter2 
    26 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
    27InBlock.gif        string Read();
    28InBlock.gif        void Write(string content);
    29InBlock.gif        void Monitor(IMeasure format);
    30ExpandedBlockEnd.gif    }

    31 None.gif
    32 None.gif     public   interface  IMeasure 
    33 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
    34InBlock.gif        string Monitor();
    35ExpandedBlockEnd.gif    }

    36 None.gif
    37 None.gif     public   class  Printer2 : IPrinter2
    38 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
    39InBlock.gif        public string Read()
    40ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
    41InBlock.gif            return null;
    42ExpandedSubBlockEnd.gif        }

    43InBlock.gif
    44InBlock.gif        public void Write(string content)
    45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
    46ExpandedSubBlockEnd.gif        }

    47InBlock.gif
    48InBlock.gif        public void Monitor(IMeasure format)
    49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
    50ExpandedSubBlockEnd.gif        }

    51ExpandedBlockEnd.gif    }
  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

转载于:https://www.cnblogs.com/xuxiaoguang/archive/2007/08/07/845939.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值