程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 我的Design Pattern之旅[6]:Adapter Pattern(OO)(12)

我的Design Pattern之旅[6]:Adapter Pattern(OO)(12)

編輯:關於C語言

C#

/**//*
(C) OOMusou 2007 http://oomusou.cnblogs.com

Filename  : DP_AdpaterPattern_Strategy_Object.cs
Compiler  : Visual Studio 2005 / Visual C# 2.0
Description : Demo how to use Strategy Pattern with Adapter Pattern (Object)
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 DrawAdapter : IDrawStrategy {
 protected IPaint _adaptee = null;

 public DrawAdapter() {}

 public DrawAdapter(IPaint adaptee) {
  _adaptee = adaptee;
 }

 public void draw() {
  _adaptee.paint();
 }
}

class ClIEnt {
 static void Main() {
  Grapher grapher = new Grapher(new DrawAdapter(new Triangle()));
  grapher.drawShape();

  grapher.setShape(new DrawAdapter(new Circle()));
  grapher.drawShape();

  grapher.setShape(new DrawAdapter(new Square()));
  grapher.drawShape();
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved