抽象工厂是一种创建型的模式,它为我们创建对象提供了有效地方法,我们不用直接new对象而是可以为创建对象配置一个接口,该接口定义了如何创建对象的方法。我们还知道抽象工厂创建的对象是一个系列的或者是一族的。该模式的最大特点就是将它的具体创建的任务交给了他的子类也就是具体的类,因此我们将创建对象的时间延迟到了它的子类。
下面使用C#代码说明:
三个子类:PerennialGarden、AnnualGarden、VeggieGarden
PerennialGarden:
using System;
namespace Gardener
{
///
/// Summary description for PerennialGarden.
///
public class PerennialGarden : Garden
{
public PerennialGarden()
{
shade = new Plant("Astilbe");
border = new Plant ("Dicentrum");
center = new Plant ("Sedum");
}
}
}
AnnualGarden:
using System;
using System.Drawing ;
namespace Gardener
{
///
/// Summary description for AnnualGarden.
///
public class AnnualGarden : Garden
{
public AnnualGarden ()
{
shade = new Plant("Coleus");
border = new Plant ("Alyssum");
center = new Plant ("Marigold");
}
}
}
VeggieGarden:
using System;
namespace Gardener
{
///
/// Summary description for VeggieGarden.
///
public class VeggieGarden : Garden
{
public VeggieGarden()
{
shade = new Plant("Broccoli");
border = new Plant ("Peas");
center = new Plant ("Corn");
}
}
}
代表抽象工厂类为Plant类,其实现代码为:
using System;
using System.Drawing;
namespace Gardener
{
///
/// Summary description for Plant.
///
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);
}
}
}
代表操作类为Garden类,其实现代码为:
using System;
using System.Drawing ;
namespace Gardener
{
///
/// Summary description for Garden.
///
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);
}
}
}
测试类型为我inForm,类型为Form1,其实现代码为:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Gardener
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private Gardener.GdPic gdPic1;
private System.Windows.Forms.RadioButton opAnnual;
private System.Windows.Forms.RadioButton opVegetable;
private System.Windows.Forms.RadioButton opPerennial;
private System.Windows.Forms.CheckBox ckCenter;
private System.Windows.Forms.CheckBox ckBorder;
private System.Windows.Forms.CheckBox ckShade;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
private Garden garden;
private void init()
{
}
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
init();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.ckCenter = new System.Windows.Forms.CheckBox();
this.gdPic1 = new Gardener.GdPic();
this.opVegetable = new System.Windows.Forms.RadioButton();
this.ckBorder = new System.Windows.Forms.CheckBox();
this.ckShade = new System.Windows.Forms.CheckBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.opPerennial = new System.Windows.Forms.RadioButton();
this.opAnnual = new System.Windows.Forms.RadioButton();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// ckCenter
//
this.ckCenter.Location = new System.Drawing.Point(160, 208);
this.ckCenter.Name = "ckCenter";
this.ckCenter.Size = new System.Drawing.Size(64, 16);
this.ckCenter.TabIndex = 2;
this.ckCenter.Text = "Center";
this.ckCenter.CheckedChanged += new System.EventHandler(this.ckCenter_CheckedChanged);
//
// gdPic1
//
this.gdPic1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.gdPic1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.gdPic1.Location = new System.Drawing.Point(160, 8);
this.gdPic1.Name = "gdPic1";
this.gdPic1.Size = new System.Drawing.Size(184, 192);
this.gdPic1.TabIndex = 1;
this.gdPic1.TabStop = false;
//
// opVegetable
//
this.opVegetable.Location = new System.Drawing.Point(16, 48);
this.opVegetable.Name = "opVegetable";
this.opVegetable.Size = new System.Drawing.Size(80, 16);
this.opVegetable.TabIndex = 1;
this.opVegetable.Text = "Vegetable";
this.opVegetable.CheckedChanged += new System.EventHandler(this.opVegetable_CheckedChanged);
//
// ckBorder
//
this.ckBorder.Location = new System.Drawing.Point(224, 208);
this.ckBorder.Name = "ckBorder";
this.ckBorder.Size = new System.Drawing.Size(64, 16);
this.ckBorder.TabIndex = 3;
this.ckBorder.Text = "Border";
this.ckBorder.CheckedChanged += new System.EventHandler(this.ckBorder_CheckedChanged);
//
// ckShade
//
this.ckShade.Location = new System.Drawing.Point(288, 208);
this.ckShade.Name = "ckShade";
this.ckShade.Size = new System.Drawing.Size(64, 16);
this.ckShade.TabIndex = 3;
this.ckShade.Text = "Shade";
this.ckShade.CheckedChanged += new System.EventHandler(this.ckShade_CheckedChanged);
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.opPerennial,
this.opVegetable,
this.opAnnual});
this.groupBox1.Location = new System.Drawing.Point(8, 40);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(120, 120);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Garden type";
//
// opPerennial
//
this.opPerennial.Location = new System.Drawing.Point(16, 72);
this.opPerennial.Name = "opPerennial";
this.opPerennial.Size = new System.Drawing.Size(80, 16);
this.opPerennial.TabIndex = 2;
this.opPerennial.Text = "Perennial";
this.opPerennial.CheckedChanged += new System.EventHandler(this.opPerennial_CheckedChanged);
//
// opAnnual
//
this.opAnnual.Location = new System.Drawing.Point(16, 24);
this.opAnnual.Name = "opAnnual";
this.opAnnual.Size = new System.Drawing.Size(88, 16);
this.opAnnual.TabIndex = 0;
this.opAnnual.Text = "Annual";
this.opAnnual.CheckedChanged += new System.EventHandler(this.opAnnual_CheckedChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(368, 237);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.ckShade,
this.ckBorder,
this.ckCenter,
this.gdPic1,
this.groupBox1});
this.Name = "Form1";
this.Text = "Garden planner";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void opAnnual_CheckedChanged(object sender, EventArgs e)
{
setGarden( new AnnualGarden ());
}
//-----
private void opVegetable_CheckedChanged(object sender, EventArgs e)
{
setGarden( new VeggieGarden ());
}
//-----
private void opPerennial_CheckedChanged(object sender, EventArgs e)
{
setGarden( new PerennialGarden ());
}
//-----
private void setGarden(Garden gd)
{
garden = gd; //save current garden
gdPic1.setGarden ( gd); //tell picture bos
gdPic1.Refresh (); //repaint it
ckCenter.Checked =false; //clear all
ckBorder.Checked = false; //check
ckShade.Checked = false; //boxes
}
private void ckCenter_CheckedChanged(object sender, System.EventArgs e)
{
garden.setCenter ();
gdPic1.Refresh ();
}
//-----
private void ckBorder_CheckedChanged(object sender, System.EventArgs e)
{
garden.setBorder();
gdPic1.Refresh ();
}
//-----
private void ckShade_CheckedChanged(object sender, System.EventArgs e)
{
garden.setShade ();
gdPic1.Refresh ();
}
}
}