Creational Pattern之abstract factory pattern
来源:互联网 发布:fps鼠标推荐 知乎 编辑:程序博客网 时间:2024/05/21 10:08
抽象工厂 比简单工厂模式具有更高的抽象性.抽象工厂是一个工厂对象,它能返回一系列相关类中的一个类,可以使用简单工厂决定返回哪一个类.举例来讲,如汽车制造厂.我们希望丰田汽车完全使用丰田的配件,福特完全使用福特的配件.我完全可以把汽车制造厂认为抽象工厂,它不提供具体厂家的配件,配件做为一组相关的类,有工厂决定提供哪种具体的配件.
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.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.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");
}
}
}
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
}
}
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
}
}
- Creational Pattern 之 Abstract Factory
- Creational Pattern之abstract factory pattern
- Design Pattern - Creational Patterns - Abstract Factory Pattern
- Creational Pattern之simple factory pattern
- 【C++ Desgin Pattern-Creational】Abstract Factory (抽象工厂)
- Creational Patterns Part 2/5: Abstract Factory Pattern
- Design Pattern - Creational Patterns - Factory Pattern
- Design Pattern - Abstract Factory Pattern
- Abstract Factory Pattern
- Design Pattern Abstract Factory
- Design Pattern:Abstract Factory
- swift - Abstract Factory Pattern
- DesignPattern_Java:Abstract Factory Pattern
- Creational Patterns Part 1/5: Factory Pattern
- Creational Pattern
- Abstract Factory Design Pattern && Strategy Design Pattern
- Abstract Factory pattern -- Structural example
- Introducing the Abstract Factory Pattern
- HttpApplication 事件
- 祝大家国庆节和节快乐
- 不会用BLOG
- MASM32编程获取文件最后修改时间
- 上海“生煎”地图大公开
- Creational Pattern之abstract factory pattern
- 敏捷软件开发宣言
- 交互递归和丘齐数
- 哪家银行缩写最牛!
- 系统敏感端口真正的关闭大法
- 应用框架的基本思想
- 两个基本共识—基于构件的软件开发的发展方向
- 合同
- DDOS攻击 如何判断是否遭到流量攻击