《实验4设计模式课件》由会员分享,可在线阅读,更多相关《实验4设计模式课件(18页珍藏版)》请在金锄头文库上搜索。
1、实验4设计模式抽象工厂模式实验4设计模式要求n本周日最后一次实验时(12月23号晚18:0022:00),前三次未交作业的同学将作业补交。n本次实验为选做实验,设计模式的内容请大家自学。实验4设计模式抽象工厂模式结构图实验目的:1. 理解抽象工厂设计模式的含义2. 练习抽象工厂模式的实现3.掌握学习设计模式的一般方法实验4设计模式问题n编写一个程序来完成花园的规划,花园可能是季生植物型花园、蔬菜型花园或多年生植物型花园。要解决的问题:边缘种植什么植物中央种植什么植物阴凉部分种植什么(用一个图片框来模拟,在一个图片框中的相应位置处标出植物类型,灰色的圆表示阴凉部分。)n以下是以C语言给出的各参考
2、类和运行结果。请实现这一程序,并分析抽象工厂模式的应用情况,使用的程序设计语言不限。实验4设计模式基类Gardenusing 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 displaypublic void setCe
3、nter() showCenter = true;public void setBorder() showBorder =true;public void setShade() showShade =true;/draw each plantpublic 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);实验4设计模式Plant对象设置植物名字,并绘制自己usi
4、ng 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 namefont = new Font (Arial, 12);br = new SolidBrush (Color.Black ); /-public void draw(Graphic
5、s g, int x, int y) g.DrawString (name, font, br, x, y);实验4设计模式季生植物型花园using System;using System.Drawing ;namespace Gardener/ / Summary description for AnnualGarden./ public class AnnualGarden : Gardenpublic AnnualGarden () shade = new Plant(Coleus);border = new Plant (Alyssum);center = new Plant (Mar
6、igold);实验4设计模式蔬菜型花园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);实验4设计模式多年生植物型花园using System;namespace Gardener/ / Summary description
7、for PerennialGarden./ public class PerennialGarden : Gardenpublic PerennialGarden() shade = new Plant(Astilbe);border = new Plant (Dicentrum);center = new Plant (Sedum);实验4设计模式用户界面Form实验4设计模式用户界面单选按钮private void opAnnual_CheckedChanged(object sender, EventArgs e) setGarden( new AnnualGarden ();/-pri
8、vate 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 gardengdPic1.setGarden ( gd);/tell pic
9、ture bosgdPic1.Refresh ();/repaint itckCenter.Checked =false;/clear allckBorder.Checked = false;/checkckShade.Checked = false;/boxes实验4设计模式用户界面复选框private void ckCenter_CheckedChanged(object sender, System.EventArgs e) garden.setCenter ();gdPic1.Refresh ();/-private void ckBorder_CheckedChanged(objec
10、t sender, System.EventArgs e) garden.setBorder();gdPic1.Refresh ();/-private void ckShade_CheckedChanged(object sender, System.EventArgs e) garden.setShade ();gdPic1.Refresh ();实验4设计模式图片框(PictureBox)using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.D
11、ata;using System.Windows.Forms;namespace Gardener/ / Summary description for GdPic./ public class GdPic : System.Windows.Forms.PictureBox / / Required designer variable./ private System.ComponentModel.Container components = null;private Brush br;private Garden gden;private void init () br = new Soli
12、dBrush (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
13、 != null)gden.draw (g);实验4设计模式图片框(PictureBox)(续)/ / Clean up any resources being used./ protected override void Dispose( bool disposing )if( disposing )if(components != null)components.Dispose();base.Dispose( disposing );#region Component Designer generated code/ / Required method for Designer suppo
14、rt - do not modify / the contents of this method with the code editor./ private void InitializeComponent()/ / GdPic/ #endregion实验4设计模式类图GdPicGardensetCenter()setBorder()setShade()draw(g)PlantnamebrfontAnnualGardenAnnualGarden ()PerennialGardenPerennialGarden ()VeggieGardenVeggieGarden ()gdencenterbordershade实验4设计模式运行结果实验4设计模式运行结果实验4设计模式运行结果实验4设计模式