抽象工厂 比简单工厂模式具有更高的抽象性.抽象工厂是一个工厂对象,它能返回一系列相关类中的一个类,可以使用简单工厂决定返回哪一个类.举例来讲,如汽车制造厂.我们希望丰田汽车完全使用丰田的配件,福特完全使用福特的配件.我完全可以把汽车制造厂认为抽象工厂,它不提供具体厂家的配件,配件做为一组相关的类,有工厂决定提供哪种具体的配件.
using System;
using System.Drawing;

namespace Gardener

...{

/**//// <summary>
/// Summary description for Plant.
/// </summary>

public class Plant ...{
private string name;
private Brush br;
private Font font;


public Plant(string pname) ...{
name = pname; //save name
font = new Font ("Arial", 12);
br = new SolidBrush (Color.Black );
}
//-------------

public void draw(Graphics g, int x, int y) ...{
g.DrawString (name, font, br, x, y);
}
}
}

using System;
using System.Drawing ;
namespace Gardener

...{

/**//// <summary>
/// Summary description for Garden.
/// </summary>

public class Garden ...{
protected Plant center, shade, border;
protected bool showCenter, showShade, showBorder;
//select which ones to display

public void setCenter() ...{showCenter = true;}

public void setBorder() ...{showBorder =true;}

public void setShade() ...{showShade =true;}
//draw each plant

public void draw(Graphics g) ...{
if (showCenter) center.draw (g, 100, 100);
if (showShade) shade.draw (g, 10, 50);
if (showBorder) border.draw (g, 50, 150);
}
}
}

using System;

namespace Gardener

...{

/**//// <summary>
/// Summary description for VeggieGarden.
/// </summary>

public class VeggieGarden : Garden ...{

public VeggieGarden() ...{
shade = new Plant("Broccoli");
border = new Plant ("Peas");
center = new Plant ("Corn");
}
}
}

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Gardener

...{

/**//// <summary>
/// Summary description for GdPic.
/// </summary>
public class GdPic : System.Windows.Forms.PictureBox

...{

/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Brush br;
private Garden gden;

private void init () ...{
br = new SolidBrush (Color.LightGray );
}

public GdPic() ...{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
init();
}

public void setGarden(Garden garden) ...{
gden = garden;
}

protected override void OnPaint ( PaintEventArgs pe )...{
Graphics g = pe.Graphics;
g.FillEllipse (br, 5, 5, 100, 100);
if(gden != null)
gden.draw (g);
}


/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )

...{
if( disposing )

...{
if(components != null)

...{
components.Dispose();
}
}
base.Dispose( disposing );
}


Component Designer generated code#region Component Designer generated code

/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()

...{
//
// GdPic
//
}
#endregion

}
}

