资源预览内容
第1页 / 共18页
第2页 / 共18页
第3页 / 共18页
第4页 / 共18页
第5页 / 共18页
第6页 / 共18页
第7页 / 共18页
第8页 / 共18页
第9页 / 共18页
第10页 / 共18页
亲,该文档总共18页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
实验4设计模式抽象工厂模式实验4设计模式要求n本周日最后一次实验时(12月23号晚18:0022:00),前三次未交作业的同学将作业补交。n本次实验为选做实验,设计模式的内容请大家自学。实验4设计模式抽象工厂模式结构图实验目的:1. 理解抽象工厂设计模式的含义2. 练习抽象工厂模式的实现3.掌握学习设计模式的一般方法实验4设计模式问题n编写一个程序来完成花园的规划,花园可能是季生植物型花园、蔬菜型花园或多年生植物型花园。要解决的问题:边缘种植什么植物中央种植什么植物阴凉部分种植什么(用一个图片框来模拟,在一个图片框中的相应位置处标出植物类型,灰色的圆表示阴凉部分。)n以下是以C语言给出的各参考类和运行结果。请实现这一程序,并分析抽象工厂模式的应用情况,使用的程序设计语言不限。实验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 setCenter() 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对象设置植物名字,并绘制自己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 namefont = 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);实验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 (Marigold);实验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 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 ();/-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 gardengdPic1.setGarden ( gd);/tell picture 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(object 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.Data;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 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);实验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 support - 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设计模式
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号