C#
/**//*
(C) OOMusou 2007 http://oomusou.cnblogs.com
Filename : DP_AdpaterPattern_Strategy_Class.cs
Compiler : Visual Studio 2005 / C# 2.0
Description : Demo how to use Strategy Pattern with Adapter Pattern (Class)
Release : 07/11/2007 1.0
*/
using System;
interface IDrawStrategy {
void draw();
}
class Grapher {
protected IDrawStrategy _drawStrategy = null;
public Grapher() {}
public Grapher(IDrawStrategy drawStrategy) {
_drawStrategy = drawStrategy;
}
public void drawShape() {
if (_drawStrategy != null)
_drawStrategy.draw();
}
public void setShape(IDrawStrategy drawStrategy) {
_drawStrategy = drawStrategy;
}
}
interface IPaint {
void paint();
}
class Triangle : IPaint {
public void paint() {
Console.WriteLine("Draw Triangle");
}
}
class Circle : IPaint {
public void paint() {
Console.WriteLine("Draw Circle");
}
}
class Square : IPaint {
public void paint() {
Console.WriteLine("Draw Square");
}
}
class TriangleDrawAdapter : Triangle, IDrawStrategy {
public void draw() {
paint();
}
}
class CircleDrawAdapter : Circle, IDrawStrategy {
public void draw() {
paint();
}
}
class SquareDrawAdapter : Square, IDrawStrategy {
public void draw() {
paint();
}
}
class ClIEnt {
static void Main() {
Grapher grapher = new Grapher(new TriangleDrawAdapter());
grapher.drawShape();
grapher.setShape(new CircleDrawAdapter());
grapher.drawShape();
grapher.setShape(new SquareDrawAdapter());
grapher.drawShape();
}
}