日期:2011-02-18  浏览次数:20340 次

Design Patterns: Solidify Your C# Application Architecture with Design Patterns中文版(下篇)    optimizer(翻译)
  
关键字     设计模式 singleton strategy decorator composite state
  
出处     http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnmag01/html/PATTERNS.asp
  


Design Patterns: Solidify Your C# Application Architecture with Design Patterns中文版(下篇)

作者:Samir Bajaj

译者:荣耀

【译序:C#进阶文章。译者对Samir提供的C#例子进行了简单整理(作者提供的某些代码在译者的环境中无法通过编译),并编写了对应的C++示例,一并置于译注中,以便读者比对。译文中所有C#、C++程序调试环境均为Microsoft Visual Studio.NET 7.0 Beta2】

C++示例:

#include "stdafx.h";

#include <iostream>

#include <list>

using namespace std;

class Shape

{

public:

     virtual void Draw(){};

};

class Line : public Shape

{

private:

     double x1, y1, x2, y2;

public:

     Line(double x1, double y1, double x2, double y2)

     {

          this->x1 = x1;

          this->y1 = y1;

          this->x2 = x2;

          this->y2 = y2;

     }

     void Draw()

     {

         //从(x1, y1) 到(x2, y2)画一条线

          cout<<"Drawing a line"<<endl;

     }

};

class Circle : public Shape

{

private:

     double x, y, r;

public:

     Circle(double x, double y, double radius)

     {

          this->x = x;

          this->y = y;

          this->r = r;

     }

     void Draw()

     {

        //以(x, y)为圆心,r为半径画一个圆

          cout<<"Drawing a circle"<<endl;

     }

};

class Drawing : public Shape

{    

private:

     list<Shape*> shapes;

     list<Shape*>::iterator it;

public:

     Drawing()

     {

     }

     ~Drawing()

     {

         for (it = shapes.begin(); it != shapes.end(); it++)

         {

              if (*it)

              {

                   delete *it;

                   *it = NULL;