There can be no Triumph without Loss,No Victory without Suffering,No Freedom without Sacrifice.
All you have to decide is what to do with the time that is given to you.
Get busy Living, or Get busy Dying?
  首页 | 留言给我 | 订阅 Rss | CLI | 黄白之恋 Posts:158   Hits: 5552347    Comments: 173    
 日历归档
<<  <  2024 - 11  >  >>
SuMoTuWeThFrSa
     12
3456789
10111213141516
17181920212223
24252627282930
 About Me
 Name: ZhangSichu
 Sex: Male
 Age: 32
 Email: ZhangSichu@gmail.com
 MSN: ZhangSichu@hotmail.com
 Home: ZhangSichu.com
 WeiBo: weibo.com/zhangsichu
 个人推荐
 分类归档
  ·C++/C(5)  RSS
  ·软件工程(1)  RSS
  ·杂事/随感(26)  RSS
  ·.Net/Java(30)  RSS
  ·面向对象程序设计(5)  RSS
  ·汇编/破解(0)  RSS
  ·平面设计(3)  RSS
  ·SQL(5)  RSS
  ·COM/COM+(2)  RSS
  ·Web开发(81)  RSS
 My Friends
Back Forward Refresh Home 2024年11月25日 星期一 RSS CLI Mine Sweeper. In Javascript.

  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
  Posted @ 1/2/2006 3:42:45 PM | Hits (39771) | Comments (2

  Comment
 #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
你的连接偶早就做好了
  Post Comment
标题 *
作者 *
密码 记住我
评论 *
    


Stable in Firefox 1.5 2.0Stable in IE6 IE7Stable in MozillaStable in Netscape
ZhangSichu.com V0.1.7507
Powered By ZhangSichu
Copyright © ZhangSichu
Download ZhangSichu.com source code. Download source code