工厂方法模式和简单工厂模式.doc_第1页
工厂方法模式和简单工厂模式.doc_第2页
工厂方法模式和简单工厂模式.doc_第3页
工厂方法模式和简单工厂模式.doc_第4页
工厂方法模式和简单工厂模式.doc_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

课程设计报告设计题目:简单设计模式及应用 学 院:计算机科学与信息学院 专 业: 软 件 工 程 班 级: 学 号: 学生姓名: 指导教师: 年 月 日一、实训目的及要求 什么是设计模式 了解抽象工厂设计模式 应用简单工厂模式和工厂设计模式二、实训环境windows7操作系统,配置Vc#2010运行环境,安装SQL Server2010数据库管理系统三、实训内容不同的图形具有不同的计算面积和周长的方法,通过选择图行类别获得不同图形面积和周长的计算方法,从而得到图形的面积。用简单工厂模式和工厂方法模式分别编写程序计算不同图形的面积和周长四、算法描述及实训步骤A简单工厂模式1算法描述定义一个图形基类shaped,从Shaped派生出具体图形类Circle、Rectangular、Triangle、Square。定义一个产生图形工厂ShapeFactoryUML图如下所示Shaped/图形工厂#Color :string+GetArea():double+GetPerimeter():doubleShapeFactory+CreateShape():Shaped Rectangular-Lenth:double-Width:double+Rectangular(string Color, double Lenth,double Width)+GetArea():double+GetPerimeter():doubleTriangle-Ed1:double-Ed2:double-Ed3:double +Triangle(stringColor,double Ed1, double Ed2, double Ed3)+GetArea():double+GetPerimeter():double Square-Ed0:double+Square(string Color, double Ed0)+GetArea():double+GetPerimeter():doubleCircle-Radius:double+Circle(string Color,double Radius)+GetArea():double+GetPerimeter():double 2.实训步骤 (1)创建一个控制台应用程序,名为GetShaped。(2)添加一个基类shaped,代码如下public abstract class Shaped protected string Color; public Shaped() ; public Shaped(string Color) this.Color = Color; public string GetColor() return Color; public abstract double GetArea(); public abstract double Getperimeter();(3)添加一个派生类Circle如下:public class Circle : Shaped private double Radius; public Circle(string Color, double Radius) this.Color = Color; this.Radius = Radius; public override double GetArea() return System.Math.PI * Radius * Radius; public override double Getperimeter() return 2.0 * System.Math.PI * Radius; (4)添加一个派生类Rectangular如下:public class Rectangular : Shaped private double Length, Width; public Rectangular(string Color, double Length, double Width) this.Color = Color; this.Length = Length; this.Width = Width; public override double GetArea() return Length * Width; public override double Getperimeter() return 2.0 * Length + 2.0 * Width; (5)添加一个派生类Triangle如下:public class Triangle : Shaped private double Ed1, Ed2, Ed3; public Triangle(string Color, double Ed1, double Ed2, double Ed3) this.Color = Color; this.Ed1 = Ed1; this.Ed2 = Ed2; this.Ed3 = Ed3; public override double GetArea() double p = (Ed1 + Ed2 + Ed3) / 2; return System.Math.Sqrt(p * (p - Ed1) * (p - Ed2) * (p - Ed3); public override double Getperimeter() return Ed1 + Ed2 + Ed3; (6)添加一个派生类Square如下:public class Square : Shaped private double Ed0; public Square(string Color, double Ed0) this.Color = Color; this.Ed0 = Ed0; public override double GetArea() return Ed0 * Ed0; public override double Getperimeter() return Ed0 * 4; (7)添加一个图形工厂类ShapeFactory,代码如下:public class ShapeFactory public static Shaped CreateShape(string shaped) Shaped shap=null; switch(shaped) casecircle: Console.WriteLine(请输入圆的颜色); string Col = Console.ReadLine(); Console.WriteLine(请输入圆的半径); Double Rad= Convert.ToDouble ( Console.ReadLine(); shap=new Circle(Col,Rad ); break; caserectangular: Console.WriteLine(请输入矩形的颜色); string col = Console.ReadLine(); Console.WriteLine(请输入矩形的长和宽); double Len = Convert.ToDouble(Console.ReadLine(); double Wid = Convert.ToDouble(Console.ReadLine(); shap= new Rectangular(col,Len,Wid); break; casetriangle: Console.WriteLine(请输入三角形的颜色); string Col1 = Console.ReadLine(); Console.WriteLine(请输入三角形的三边长); double Ed1 = Convert.ToDouble(Console.ReadLine(); double Ed2 = Convert.ToDouble(Console.ReadLine(); double Ed3 = Convert.ToDouble(Console.ReadLine(); shap=new Triangle(Col1,Ed1,Ed2,Ed3); break; casesquare: Console.WriteLine(请输入正方形的颜色); string Col2 = Console.ReadLine(); Console.WriteLine(请输入正方形的边长); double Ed0 = Convert.ToDouble(Console.ReadLine(); shap= new Square(Col2,Ed0); break; return shap; (8)创建客户端实类对象。(9)通过从菜单中选择“生成”“生成解决方案”,来生成该解决方案。通过从菜单中选择“调试” “开始执行(不调试)”选项来执行此应用程序。B工厂方法模式1.算法描述定义一个抽象图形类Shaped,它拥有计算面积和周长的抽象方法GetArea和GetPerimeter,从Shaped派生出具体类Circle、Rectangular、Triangle、Square。定义一个抽象图形工厂接口ShapeFactory,用圆形工厂CircleFactory、矩形工厂RectangularFactory、三角形工厂TriangleFactory和正方形工厂SquareFactory具体化抽象图形工厂接口,用来生产具体图形。UML图如下所示。 Shaped/图形工厂#Color :string+GetArea():double+GetPerimeter():doubleShapeFactory+CreateShape():Shaped CircleFactory+CreateShape():CircleRectangular-Lenth:double-Width:double+Rectangular(string Color, double Lenth,double Width)+GetArea():double+GetPerimeter():doubleRectangularFactory+CreateShape():RectangularSquareFactory+CreateShape():Square Square-Ed0:double+Square(string Color, double Ed0)+GetArea():double+GetPerimeter():doubleCircle-Radius:double+Circle(string Color,double Radius)+GetArea():double+GetPerimeter():doubleTriangle-Ed1:double-Ed2:double-Ed3:double +Triangle(stringColor,double Ed1, double Ed2, double Ed3)+GetArea():double+GetPerimeter():double TriangleFactory+CreateShape():Triangle 2.实训步骤 (1)创建一个控制台应用程序,名为GetShaped。(2)添加一个基类shaped和由它派生出的具体类Circle、Rectangular、Triangle、Square,代码和简单工厂模式相同。(3)创建一个抽象图形工厂ShapeFactory;(4)分别添加具体图形工厂CircleFactory类、RectangularFactory类、TriangleFactory类、SquareFactory类。(5)创建客户端实类对象。(6)通过从菜单中选择“生成”“生成解决方案”,来生成该解决方案。通过从菜单中选择“调试” “开始执行(不调试)”选项来执行此应用程序。五、调试过程及实训结果1调试过程中的问题及解决办法 (1)无法设置各种图形的边长及颜色。 解决方法:在具体化图形工厂CreateShape中添加输入语句,将输入的值作为参数,传递给构造函数。 (2)在实例化不同图形类的对象时,语句重复繁琐。 解决方法:使用foraech遍历语句进行访问。2调试运行结果:两种模式结果相同如下六、总结七、附录程序完整源代码如下/A简单工厂模式using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace uu class Program /定义基类Shape public abstract class Shaped protected string Color; public Shaped() ; public Shaped(string Color) this.Color = Color; public string GetColor() return Color; public abstract double GetArea(); public abstract double Getperimeter(); /定义Circle类,从Shape类中派生 public class Circle : Shaped private double Radius; public Circle(string Color, double Radius) this.Color = Color; this.Radius = Radius; public override double GetArea() return System.Math.PI * Radius * Radius; public override double Getperimeter() return 2.0 * System.Math.PI * Radius; /定义Rectangular类,从Shape类中派生 public class Rectangular : Shaped private double Length, Width; public Rectangular(string Color, double Length, double Width) this.Color = Color; this.Length = Length; this.Width = Width; public override double GetArea() return Length * Width; public override double Getperimeter() return 2.0 * Length + 2 * Width; /定义Triangle类,从Shape类中派生 public class Triangle : Shaped private double Ed1, Ed2, Ed3; public Triangle(string Color, double Ed1, double Ed2, double Ed3) this.Color = Color; this.Ed1 = Ed1; this.Ed2 = Ed2; this.Ed3 = Ed3; public override double GetArea() double p = (Ed1 + Ed2 + Ed3) / 2; return System.Math.Sqrt(p * (p - Ed1) * (p - Ed2) * (p - Ed3); public override double Getperimeter() return Ed1 + Ed2 + Ed3; /定义Square类,从Shape类中派生 public class Square : Shaped private double Ed0; public Square(string Color, double Ed0) this.Color = Color; this.Ed0 = Ed0; public override double GetArea() return Ed0 * Ed0; public override double Getperimeter() return Ed0 * 4; public class ShapeFactory public static Shaped CreateShape(string shaped) Shaped shap=null; switch(shaped) casecircle: Console.WriteLine(请输入圆的颜色); string Col = Console.ReadLine(); Console.WriteLine(请输入圆的半径); double Rad= Convert.ToDouble ( Console.ReadLine(); shap=new Circle(Col,Rad ); break; caserectangular: Console.WriteLine(请输入矩形的颜色); string col = Console.ReadLine(); Console.WriteLine(请输入矩形的长和宽); double Len = Convert.ToDouble(Console.ReadLine(); double Wid = Convert.ToDouble(Console.ReadLine(); shap= new Rectangular(col,Len,Wid); break; casetriangle: Console.WriteLine(请输入三角形的颜色); string Col1 = Console.ReadLine(); Console.WriteLine(请输入三角形的三边长); double Ed1 = Convert.ToDouble(Console.ReadLine(); double Ed2 = Convert.ToDouble(Console.ReadLine(); double Ed3 = Convert.ToDouble(Console.ReadLine(); shap=new Triangle(Col1,Ed1,Ed2,Ed3); break; casesquare: Console.WriteLine(请输入正方形的颜色); string Col2 = Console.ReadLine(); Console.WriteLine(请输入正方形的边长); double Ed0 = Convert.ToDouble(Console.ReadLine(); shap= new Square(Col2,Ed0); break; return shap; static void Main(string args) string shapefactory = new string4; shapefactory0 = circle; shapefactory1 = rectangular; shapefactory2 = triangle; shapefactory3 = square; foreach (string SF in shapefactory) Shaped shaped = ShapeFactory.CreateShape(SF); Console.WriteLine(shaped Color is 0,shaped Area is 1,shaped Perimeter is2,shaped.GetColor(),shaped.GetArea(), shaped.Getperimeter(); /B工厂方法模式using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1 /定义基类Shape public abstract class Shaped protected string Color; public Shaped() ; public Shaped(string Color) this.Color = Color; public string GetColor() return Color; public abstract double GetArea(); public abstract double Getperimeter(); /定义Circle类,从Shape类中派生 public class Circle : Shaped private double Radius; public Circle(string Color, double Radius) this.Color = Color; this.Radius = Radius; public override double GetArea() return System.Math.PI * Radius * Radius; public override double Getperimeter() return 2.0 * System.Math.PI * Radius; /定义Rectangular类,从Shape类中派生 public class Rectangular : Shaped private double Length, Width; public Rectangular(string Color, double Length, double Width) this.Color = Color; this.Length = Length; this.Width = Width; public override double GetArea() return Length * Width; public override double Getperimeter() return 2.0 * Length + 2 .0* Width; /定义Triangle类,从Shape类中派生 public class Triangle : Shaped private double Ed1, Ed2, Ed3; public Triangle(string Color, double Ed1, double Ed2, double Ed3) this.Color = Color; this.Ed1 = Ed1; this.Ed2 = Ed2; this.Ed3 = Ed3; public override double GetArea() double p = (Ed1 + Ed2 + Ed3) / 2; return System.Math.Sqrt(p * (p - Ed1) * (p - Ed2) * (p - Ed3); public override double Getperimeter() return Ed1 +Ed2 + Ed3; /定义Square类,从Shape类中派生 public class Square : Shaped private double Ed0; public Square(string Color, double Ed0) this.Color = Color; this.Ed0 = Ed0; public override double GetArea() return Ed0 * Ed0; public override double Getperimeter() return Ed0 * 4; /创建抽象图形工厂ShapeFactory interface ShapeFactory Shaped CreateShape();/定义圆形工厂CircleFactory类, class CircleFactory : ShapeFactory public Shaped CreateShape() Console.WriteLine(请输入圆的颜色); string Col = Console.ReadLine(); Console.WriteLine(请输入圆的半径); double Rad= Convert.ToDouble ( Console.ReadLine(); return new Circle(Col,Rad ); /定义矩形工厂RectangularFactory类 class RectangularFactory : ShapeFactory public Shaped CreateShape() Console.WriteLine(请输入矩形的颜色); string Col = Console.ReadLine(); Console.WriteLine(请输入矩形的长和宽); double Len = Convert.ToDouble(Console.ReadLine(); double Wid = Convert.ToDoub

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论