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;
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, 100100);
            
if (showShade) shade.draw (g, 1050);
            
if (showBorder) border.draw (g, 50150);
        }

    }

}

 

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, 
55100100);
            
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

    }

}

 

ShowClassGraph

原创粉丝点击