面向对象设计原则和设计模式的概念汇总.docx
- 文档编号:5820003
- 上传时间:2023-01-01
- 格式:DOCX
- 页数:11
- 大小:134.33KB
面向对象设计原则和设计模式的概念汇总.docx
《面向对象设计原则和设计模式的概念汇总.docx》由会员分享,可在线阅读,更多相关《面向对象设计原则和设计模式的概念汇总.docx(11页珍藏版)》请在冰豆网上搜索。
面向对象设计原则和设计模式的概念汇总
publicvoidEnter({
mLight.Open(;
}
publicvoidLeave({
mLight.Close(;
}
publicLightlight{
get{returnmLight;}
set{mLight=value;}}publicvoidOpen({
System.Console.WriteLine(“lightturnningon...”;
}
publicvoidClose({
System.Console.WriteLine(“lightturnningoff...”;
}
微软亚洲研究院资助
publicclassTestMain{
publicstaticvoidMain({
Roomroom=newRoom(;Lightlight=newLight(;room.light=light;room.enter(;
System.Console.WriteLine(“living...”;room.leave(;
}
}
微软亚洲研究院资助publicvoidEnter({
mLight.Open(;mTV.Open(;}
publicvoidLeave({
mLight.Close(;mTV.Close(;}
publicLightlight{
get{returnmLight;}set{mLight=value;}}
publicTVtv{...}
随着生活水平不断的提
微软亚洲研究院资助Switch预见了所有的具有打开和关闭功能的生活用品
Switch也同时允许所有具有该接口的类扩充到系统中来
Demo:
演示这种设计给系统扩充带来的便利
微软亚洲研究院资助里氏代换原则(LSP
•如果对每一个类型为T1的对象O1,都有类型为,使得以T1定义的所有程序PO2时,程序P的行为没有变化,那么类型的子类型
–反过来的代换是不成立的–是继承复用的基石
•只有当子类可以替换掉基类,软件单位的功能不受影响的时候,我们才说基类真正被复用了。
namespacexjtuse.lsp{
publicclassRectangle{
privatelongwidth;
privatelongheight;
publicvirtualvoidsetWidth(longwidth{this.width=width;
}
publicvirtualvoidsetHeight(longheight{this.height=height;
}
publicvirtuallonggetWidth({
returnwidth;
}
publicvirtuallonggetHeight({
returnheight;
}
}}
namespacexjtuse.lsp{
publicclassSquare:
Rectangle{
publicoverridevoidsetWidth(longwidth
{this.width=height=width;}
publicoverridevoidsetHeight(longheight
{width=this.height=height;}
publicoverridelonggetWidth(
{returnwidth;}
publicoverridelonggetHeight(
{returnheight;}
publicvoidsetSide(longside
{setWidth(side;}
}
}
namespacexjtuse.lsp{
publicclassSmartTest{
publicstaticvoidMain({
Rectangler=newRectangle(;
r.setWidth(10;r.setHeight(5;
resize(r;
Squares=newSquare(;
s.setSide(10;
resize(s;
}
publicstaticvoidresize(Rectangler{
while(r.getHeight(<=r.getWidth({
r.setHeight(r.getHeight(+1;
System.Console.WriteLine(“{0},{1}”,
r.getHeight(,r.getWidth(;
}
}
}
+getWidth(
+getHeight(
<<接口>>
Quadrangle
+getWidth(:
long
+setWidth(:
void
+getHeight(:
long
+setHeight(:
void-width:
long
-height:
long
Rectangle
+getSide(:
long+setSide(:
void+getWidth(:
long+getHeight(:
long
-side:
longSquare
依赖倒转原则(DIP
传统的过程性系统的设计方法倾向于使高层次的模块依赖于低层次的模块:
抽象层依赖于具体层次
西安交大软件学院帐号、帐号的种类和帐号的状态Account依赖于AccountType和
AccountStatus两个抽象类型
西安交大软件学院微软亚洲研究院资助接口隔离原则(ISP
接口污染
–过于臃肿的接口
–每一个接口都代表一个角色,实现一个接口的对象,在它的整个生命周期中都扮演这个角色
将角色区分清楚就是系统设计的一个重要工作
不应当将几个不同的的角色都交给同一个接口,而应当交给不同的接口
定义
–使用多个专门的接口比使用单一的总接口要好
Client1
Client2
Client3
+m1(+p2(<<接口>>IService1+m2(+p1(
<<接口>>IService2+m1(+p1(+p2(
<<接口>>IService3+m1(+m2(+p1(+p2(
Service
“Is-A”
for(IEnumeratorie=vec.GetEmuerator(;vec.MoveNext(;{...}
IEnumeratorie=vec.GetEmuerator(;
while(vec.MoveNext({
...}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向 对象 设计 原则 模式 概念 汇总