Post

Abstract Class VS Interface[二]

StrategyPattern,这个实例想做一个策略者:把已经声明过的方法操作,在运行时进行调换。猛地一听好像有些奇怪,类中的某个方法已经声明过了,怎么能在运行时,替换它的实际的处理过程呢,只留了个方法名而作别的事情。StrategyPattern就是实现了这样的操作。

IStrategyDraw IStrategyDraw Interface

1
2
3
4
5
6
7
8
9
10
11
12
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

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System; 
namespace StrategyPattern  
{  
    /// <summary>  
    /// Summary description for ConsoleStrategy.  
    /// </summary>  
    public class ConsoleStrategy : IStrategyDraw  
    {  
        public ConsoleStrategy()  
        {  
        }  

        public string StragegyDraw()  
        {  
        return "Console Draw";  
        }
    }  
}  

WindowStategy WindowStategy Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System; 
namespace StrategyPattern  
{  
    /// <summary>  
    /// Summary description for WindowStategy.  
    /// </summary>  
    public class WindowStategy:IStrategyDraw  
    {  
        public WindowStategy()  
        {
        }

        public string StragegyDraw()  
        {
            return "Window Draw";  
        }
    }  
}  

StrategyDraw Test Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 提供给了子类共有的属性和方法。在 DecoratorPatternStrategyPattern 中多出用到 Interface,体现了 Interface 是方法一级的抽象。

源代码(Windows 2003 Enterprise + VS 2003 Enterprise 下编译通过)

This post is licensed under CC BY 4.0 by the author.