日历归档 |
|
<< < 2024 - 11 > >> | Su | Mo | Tu | We | Th | Fr | Sa | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
|
|
|
About Me |
|
|
ZhangSichu |
|
Male |
|
32 |
|
ZhangSichu@gmail.com |
|
ZhangSichu@hotmail.com |
|
ZhangSichu.com |
|
weibo.com/zhangsichu |
|
|
|
个人推荐 |
|
|
|
|
分类归档 |
|
|
|
|
My Friends |
|
|
|
|
Abstract Class VS Interface[二]
|
StrategyPattern,这个实例想做一个策略者:把已经声明过的方法操作,在运行时进行调换。猛地一听好像有些奇怪,类中的某个方法已经声明过了,怎么能在运行时,替换它的实际的处理过程呢,只留了个方法名而作别的事情。StrategyPattern就是实现了这样的操作。
IStrategyDraw IStrategyDraw Interface using System; namespace StrategyPattern { /// <summary> /// Summary description for IStrategyDraw. /// </summary> public interface IStrategyDraw { //here just define a general draw function. string StragegyDraw(); } }
GeneralDraw GeneralDraw Class using System; namespace StrategyPattern { /// <summary> /// Summary description for GeneralDraw. /// </summary> public class GeneralDraw { private IStrategyDraw _strategyDraw;
public GeneralDraw() { }
public IStrategyDraw StrategyDraw { get { return this._strategyDraw; } set { this._strategyDraw = value; } }
public string StragegyDraw() { if (this._strategyDraw != null) { return this._strategyDraw.StragegyDraw(); } else { return ""; } }
public string SelfDraw() { return "Self Draw"; }
} }
ConsoleStrategy ConsoleStrategy Class using System; namespace StrategyPattern { /// <summary> /// Summary description for ConsoleStrategy. /// </summary> public class ConsoleStrategy : IStrategyDraw { public ConsoleStrategy() { }
#region IStrategyDraw Members
public string StragegyDraw() { return "Console Draw"; }
#endregion } }
WindowStategy WindowStategy Class using System; namespace StrategyPattern { /// <summary> /// Summary description for WindowStategy. /// </summary> public class WindowStategy:IStrategyDraw { public WindowStategy() { }
#region IStrategyDraw Members
public string StragegyDraw() { return "Window Draw"; }
#endregion } }
StrategyDraw Test Code private void StrategyDraw() { IStrategyDraw conStrategy = new ConsoleStrategy(); IStrategyDraw winStrategy = new WindowStategy(); GeneralDraw genDraw = new GeneralDraw(); genDraw.StrategyDraw = conStrategy;
Console.WriteLine("{0}",genDraw.StragegyDraw()); Console.WriteLine("{0}",genDraw.SelfDraw());
genDraw.StrategyDraw = winStrategy; Console.WriteLine("{0}",genDraw.StragegyDraw()); Console.WriteLine("{0}",genDraw.SelfDraw()); } GeneralDraw在运行时,根据自己内部的的IStrategyDraw确定Draw方法的实际操作,而外表上看GeneralDraw只给外界公开说自己有一个Draw方法。 在上面的两个设计模式中,DecoratorPattern中的ComponentDecorator这个AbstractClass体现了Class一级的抽象,AbstractClass提供给了子类共有的属性和方法。在DecoratorPattern和StrategyPattern中多出用到Interface,体现了Interface是方法一级的抽象。
所有代码在Windows 2003 Enterprise + VS 2003 Enterprise 下编译通过
File: Click to Download
|
|
#re:Abstract Class VS Interface[二] 6/25/2010 9:31:49 AM Alec.He
|
很巧我也被问过这个问题,这是我的答案
http://www.cnblogs.com/AlecHe/archive/2010/03/24/1693686.html
|
|
#re:Abstract Class VS Interface[二] 1/10/2006 1:05:32 PM 淡如云烟
|
做个友情连接吧 淡如云烟 www.lupeng.org 你的连接偶早就做好了
|
|
|
|
|
|