C#设计模式(2)

软件设计原则与重构
* 软件腐化的原因:

问题所在   设计目标
----------------------------------------------------------------------------
过于僵硬   可扩展性(新性能可以很容易加入系统)
过于脆弱   灵活性(修改不会波及其它)
复用率低  
粘度过高   可插入性(新功能容易加入系统(气囊加入方向盘))

* 提高系统可复用性的几点原则:
传统复用:
1. 代码的粘帖复用
2. 算法的复用
3. 数据结构的复用

* 可维护性与可复用性并不完全一致

* 对可维护性的支持:


一、 "开放-封闭"原则(OCP)

Open-Closed Principle原则讲的是:一个软件实体应当对扩展开放,对修改关闭。

优点:
    通过扩展已有软件系统,可以提供新的行为,以满足对软件的新的需求,使变化中的软件有一定的适应性和灵活性。
    已有软件模块,特别是最重要的抽象层模块不能再修改,这使变化中的软件系统有一定的稳定性和延续性。

例子:玉帝招安美猴王
当年大闹天宫便是美猴王对玉帝的新挑战。美猴王说:"'皇帝轮流做,明年到我家。'只教他搬出去,将天宫让于我!"对于这项挑战,太白金星给玉皇大帝提出的建议是:"降一道招安圣旨,宣上界来…,一则不劳师动众,二则收仙有道也。"

换而言之,不劳师动众、不破坏天规便是"闭",收仙有道便是"开"。招安之道便是玉帝天庭的"开放-封闭"原则。

 Pic28.gif

招安之法的关键便是不允许更改现有的天庭秩序,但允许将妖猴纳入现有秩序中,从而扩展了这一秩序。用面向对象的语言来讲,不允许更改的是系统的抽象层,而允许更改的是系统的实现层。


二、 里氏代换原则(LSP)

Liskov Substitution Principle(里氏代换原则):子类型(subtype)必须能够替换它们的基类型。

白马、黑马
 Pic29.gif

反过来的代换不成立
《墨子·小取》说:"娣,美人也,爱娣,非爱美人也……"娣便是妹妹,哥哥喜爱妹妹,是因为两人是兄妹关系,而不是因为妹妹是个美人。因此,喜爱妹妹不等同于喜爱美人。用面向对象语言描述,美人是基类,妹妹是美人的子类。哥哥作为一个有"喜爱()"方法,接受妹妹作为参数。那么,这个"喜爱()"方法一般不能接受美人的实例。

 Pic30.gif

一个违反LSP的简单例子(长方形和正方形)

None.gifpublic class Rectangle
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
private long width;
InBlock.gif   
private long height;
InBlock.gif    
InBlock.gif   
public void setWidth(long width)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
this.width = width;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
public long getWidth()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return this.width;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
public void setHeight(long height)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
this.height = height;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
public long getHeight()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return this.height;
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif
None.gif
public class Square
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
private long side;
InBlock.gif    
InBlock.gif   
public void setSide(long side)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
this.side = side;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public long getSide()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return side;
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}


正方形不可以做长方形的子类

None.gifusing System;
None.gif
None.gif
public class Rectangle
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
private long width;
InBlock.gif   
private long height;
InBlock.gif    
InBlock.gif   
public void setWidth(long width)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
this.width = width;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
public long getWidth()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return this.width;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
public void setHeight(long height)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
this.height = height;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
public long getHeight()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return this.height;
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif
None.gif
public class Square : Rectangle
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
private long side;
InBlock.gif
InBlock.gif   
public void setWidth(long width)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      setSide(width);
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public long getWidth()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return getSide();
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public void setHeight(long height)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      setSide(height);
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public long getHeight()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return getSide();
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public long getSide()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return side;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public void setSide(long side)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
this.side = side;
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif
None.gif
public class SmartTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
public void resize(Rectangle r)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
while (r.getHeight() >= r.getWidth() )
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         r.setWidth(r.getWidth() 
+ 1);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

Pic31.gif 
在执行SmartTest的resize方法时,如果传入的是长方形对象,当高度大于宽度时,会自动增加宽度直到超出高度。但是如果传入的是正方形对象,则会陷入死循环。

代码重构

None.gifpublic interface Quadrangle
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
public long getWidth();
InBlock.gif   
public long getHeight();
ExpandedBlockEnd.gif}

None.gif
None.gif
public class Rectangle : Quadrangle 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
private long width;
InBlock.gif   
private long height;
InBlock.gif    
InBlock.gif   
public void setWidth(long width)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
this.width = width;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
public long getWidth()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return this.width;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
public void setHeight(long height)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
this.height = height;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
public long getHeight()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return this.height;
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif
None.gif
public class Square : Quadrangle 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
private long side;
InBlock.gif
InBlock.gif   
public void setSide(long side)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
this.side = side;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public long getSide()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return side;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public long getWidth()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return getSide();
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public long getHeight()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return getSide();
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

Pic32.gif

 

转载于:https://www.cnblogs.com/xujiaci/archive/2007/09/28/909267.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值