设计模式实验三.docx

上传人:b****1 文档编号:671398 上传时间:2023-04-29 格式:DOCX 页数:17 大小:272.68KB
下载 相关 举报
设计模式实验三.docx_第1页
第1页 / 共17页
设计模式实验三.docx_第2页
第2页 / 共17页
设计模式实验三.docx_第3页
第3页 / 共17页
设计模式实验三.docx_第4页
第4页 / 共17页
设计模式实验三.docx_第5页
第5页 / 共17页
设计模式实验三.docx_第6页
第6页 / 共17页
设计模式实验三.docx_第7页
第7页 / 共17页
设计模式实验三.docx_第8页
第8页 / 共17页
设计模式实验三.docx_第9页
第9页 / 共17页
设计模式实验三.docx_第10页
第10页 / 共17页
设计模式实验三.docx_第11页
第11页 / 共17页
设计模式实验三.docx_第12页
第12页 / 共17页
设计模式实验三.docx_第13页
第13页 / 共17页
设计模式实验三.docx_第14页
第14页 / 共17页
设计模式实验三.docx_第15页
第15页 / 共17页
设计模式实验三.docx_第16页
第16页 / 共17页
设计模式实验三.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

设计模式实验三.docx

《设计模式实验三.docx》由会员分享,可在线阅读,更多相关《设计模式实验三.docx(17页珍藏版)》请在冰点文库上搜索。

设计模式实验三.docx

设计模式实验三

实验3创建型设计模式实验

实验学时:

2

每组人数:

1

实验类型:

3(1:

基础性2:

综合性3:

设计性4:

研究性)

实验要求:

1(1:

必修2:

选修3:

其它)

实验类别:

3(1:

基础2:

专业基础3:

专业4:

其它)

一、实验目的

1.熟练使用面向对象设计原则对系统进行重构;

2.熟练使用PowerDesigner和任意一种面向对象编程语言实现几种常见的创建型设计模式,包括简单工厂模式、工厂方法模式、抽象工厂模式和单例模式,理解每一种设计模式的模式动机,掌握模式结构,学习如何使用代码实现这些模式。

二、实验内容

1.在某图形库API中提供了多种矢量图模板,用户可以基于这些矢量图创建不同的显示图形,图形库设计人员设计的初始类图如下所示:

在该图形库中,每个图形类(如Circle、Triangle等)的init()方法用于初始化所创建的图形,setColor()方法用于给图形设置边框颜色,fill()方法用于给图形设置填充颜色,setSize()方法用于设置图形的大小,display()方法用于显示图形。

客户类(Client)在使用该图形库时发现存在如下问题:

由于在创建窗口时每次只需要使用图形库中的一种图形,因此在更换图形时需要修改客户类源代码;

在图形库中增加并使用新的图形时需要修改客户类源代码;

客户类在每次使用图形对象之前需要先创建图形对象,有些图形的创建过程较为复杂,导致客户类代码冗长且难以维护。

现需要根据面向对象设计原则对该系统进行重构,要求如下:

隔离图形的创建和使用,将图形的创建过程封装在专门的类中,客户类在使用图形时无须直接创建图形对象,甚至不需要关心具体图形类类名;

客户类能够方便地更换图形或使用新增图形,无须针对具体图形类编程,符合开闭原则。

绘制重构之后的类图并说明在重构过程中所运用的面向对象设计原则。

2.使用简单工厂模式设计一个可以创建不同几何形状(Shape),如圆形(Circle)、矩形(Rectangle)和三角形(Triangle)等的绘图工具类,每个几何图形均具有绘制Draw()和擦除Erase()两个方法,要求在绘制不支持的几何图形时,抛出一个UnsupportedShapeException异常,绘制类图并编程模拟实现。

3.现需要设计一个程序来读取多种不同类型的图片格式,针对每一种图片格式都设计一个图片读取器(ImageReader),如GIF图片读取器(GifReader)用于读取GIF格式的图片、JPG图片读取器(JpgReader)用于读取JPG格式的图片。

图片读取器对象通过图片读取器工厂ImageReaderFactory来创建,ImageReaderFactory是一个抽象类,用于定义创建图片读取器的工厂方法,其子类GifReaderFactory和JpgReaderFactory用于创建具体的图片读取器对象。

试使用工厂方法模式设计该程序,绘制类图并编程模拟实现。

需充分考虑系统的灵活性和可扩展性。

4.某软件公司欲开发一套界面皮肤库,可以对桌面软件进行界面美化。

不同的皮肤将提供视觉效果不同的按钮、文本框、组合框等界面元素,其结构如下图所示:

该皮肤库需要具备良好的灵活性和可扩展性,用户可以自由选择不同的皮肤,开发人员可以在不修改既有代码的基础上增加新的皮肤。

试使用抽象工厂模式设计该皮肤库,绘制类图并编程模拟实现。

5.使用单例模式的思想实现多例模式,确保系统中某个类的对象只能存在有限个,例如两个或三个,设计并编写代码实现一个多例类。

6.使用单例模式设计一个多文档窗口(注:

在JavaAWT/Swing开发中可使用JDesktopPane和JInternalFrame来实现),要求在主窗体中某个内部子窗体只能实例化一次,即只能弹出一个相同的子窗体,如下图所示,编程实现该功能。

 

(注:

用C#或C++实现类似功能也可以)

三、实验要求

1.选择合适的面向对象设计原则对系统进行重构,正确无误地绘制重构之后的类图;

2.结合实例,正确无误地绘制简单工厂模式、工厂方法模式、抽象工厂模式和单例模式的模式结构图;

3.使用任意一种面向对象编程语言实现简单工厂模式、工厂方法模式、抽象工厂模式和单例模式实例,代码运行正确无误。

四、实验步骤

1.选择合适的面向对象设计原则对系统进行重构,使用PowerDesigner绘制重构之后的类图;

2.结合实例,使用PowerDesigner绘制简单工厂模式实例结构图并用面向对象编程语言实现该模式实例;

3.结合实例,使用PowerDesigner绘制工厂方法模式实例结构图并用面向对象编程语言实现该模式实例;

4.结合实例,使用PowerDesigner绘制抽象工厂模式实例结构图并用面向对象编程语言实现该模式实例;

5.结合实例,使用PowerDesigner绘制多例模式实例结构图并用面向对象编程语言实现该模式实例;

6.结合实例,使用PowerDesigner绘制单例模式实例结构图并用面向对象编程语言实现该模式实例。

五、实验结果

1.重构之后的类图:

重构过程中所使用的面向对象设计原则及简要说明:

a.开闭原则:

创建新图形只要新加入图形工厂和对应图形类,不修改源代码。

b.依赖倒转原则:

针对接口编程。

c.单一职责原则:

每个工厂只生产对应图形。

2.类图:

实现代码:

//代码颜色使用黑色,字体使用TimesNewRoman或Arial,字号为五号,如

abstractclassShape

Shape.java

publicinterfaceShape{

voiddraw();

voiderase();

}

Rectangle.java

publicclassRectangleimplementsShape{

publicvoiddraw(){

System.out.println("draw Rectangle");

}

publicvoiderase(){

System.out.println("erase Rectangle");

}

}

Circle.java

publicclassCircleimplementsShape{

publicvoiddraw(){

System.out.println("draw Circle");

}

publicvoiderase(){

System.out.println("erase Circle");

}

}

Triangle.java

publicclassTriangleimplementsShape{

publicvoiddraw(){

System.out.println("draw Triangle");

}

publicvoiderase(){

System.out.println("erase Triangle");

}

}

ShapeFactory.java

publicclassShapeFactory{

public static Shape produceShape(String shape) throws UnsupportedShapeException{

if (shape.equals("Circle")){    

System.out.println("Circle");    

return new Circle;

}

else if(shape.equals("Triangle")){

System.out.println("Triangle");    

return new Triangle;

}

else if(shape.equals("Rectangle")){

System.out.println("Rectangle");    

return new Rectangle;

}

else {   

 throw new UnsupportedShapeException( UnsupportedShapeExceptionuse);

}

}

}

Client.java

publicclassClient{

publicstaticvoidmain(Stringargs[]){

}

}

UnsupportedShapeException.java

publicclassUnsupportedShapeExceptionextendsException{

publicUnsupportedShapeException(){

System.out.println("绘制图形异常,请确认输入图形。

");

}

}

3.类图:

 

实现代码:

ImageReader.java

publicinterfaceImageReader{

publicvoidreadImage();

}

JpgReader.java

publicclassJpgReaderimplementsImageReader{

publicvoidreadImage(){

System.out.println("readjgpimage");

}

}

GifReader.java

publicclassGifReaderimplementsImageReader{

publicvoidreadImage(){

System.out.println("readgifimage");

}

}

ImageReaderFactory.java

publicinterfaceImageReaderFactory{

publicImageReaderproduceImageReader();

}

JpgReaderFactory.java

publicclassJpgReaderFactoryimplementsImageReaderFactory{

publicImageReaderproduceImageReader(){

System.out.println("jpgreader");

returnnewJpgReader();

}

}

GifReaderFactory.java

publicclassGifReaderFactoryimplementsImageReaderFactory{

publicImageReaderproduceImageReader(){

System.out.println("gifreader");

returnnewGifReader();

}

}

Client.java

publicclassClient{

publicstaticvoidmain(Stringargs[]){

}

}

4.类图:

实现代码:

AbstractButton.java

publicinterfaceAbstractButton{

}

GreenButton.java

publicclassGreenButtonimplementsAbstractButton{

publicvoidaction(){

System.out.println("greenbutton");

}

}

BlueButton.java

publicclassBlueButtonimplementsAbstractButton{

publicvoidaction(){

System.out.println("bluebutton");

}

}

AbstractTextbox.java

publicinterfaceAbstractTextbox{

publicvoidaction();

}

GreenTextbox.java

publicclassGreenTextboximplementsAbstractTextbox{

publicvoidaction(){

System.out.println("greentextbox");

}

}

BlueTextbox.java

publicclassBlueTextboximplementsAbstractTextbox{

publicvoidaction(){

System.out.println("bluetextbox");

}

}

AbstractComboBox.java

publicinterfaceAbstractComboBox{

publicvoidaction();

}

GreenComboBox.java

publicclassGreenComboBoximplementsAbstractComboBox{

publicvoidaction(){

System.out.println("greencombobox");

}

}

BlueComboBox.java

publicclassBlueComboBoximplementsAbstractComboBox{

publicvoidaction(){

System.out.println("bluecombobox");

}

}

SkinFactory.java

publicinterfaceSkinFactory{

publicAbstractButtoncreateButton();

publicAbstractTextboxcreateTextbox();

publicAbstractComboBoxcreateComboBox();

}

SpringSkinFactory.java

publicclassSpringSkinFactoryimplementsSkinFactory{

publicAbstractButtoncreateButton(){

System.out.println("生产greenbutton");

returnnewGreenButton();

}

publicAbstractTextboxcreateTextbox(){

System.out.println("生产greentextbox");

returnnewGreenTextbox();

}

publicAbstractComboBoxcreateComboBox(){

System.out.println("生产greencombobox");

returnnewGreenComboBox();

}

}

SummerSkinFactory.java

publicclassSummerSkinFactoryimplementsSkinFactory{

publicAbstractButtoncreateButton(){

System.out.println("生产bluebutton");

returnnewBlueButton();

}

publicAbstractTextboxcreateTextbox(){

System.out.println("生产bluetextbox");

returnnewBlueTextbox();

}

publicAbstractComboBoxcreateComboBox(){

System.out.println("生产bluecombobox");

returnnewBlueComboBox();

}

}

Client.java

publicclassClient{

publicstaticvoidmain(Stringargs[]){

}

}

5.类图:

实现代码:

publicclassCompute{

privatestaticCompute[]instance;

privateCompute(){

}

publicstaticComputegetInstance(intnumber){

if(number<=0)

System.out.println("请输入大于0的整数");

if(instance.length==0){

for(inti=0,i

Instance[i]=newSingleton();

}

}

intx=(int)(Math.random()*number);

Inty=x+1;

System.out.println("调用第y台电脑");

returninstance[x];

}

}

6.类图:

实现代码:

JInternalFrame1.java

packagetest;

importjavax.swing.JInternalFrame;

publicclassJInternalFrame1extendsJInternalFrame{

privatestaticJInternalFrame1JIF1=null;

privateJInternalFrame1(Stringname,booleanb1,booleanb2,booleanb3,booleanb4){

super(name,b1,b2,b3,b4);

}

publicstaticJInternalFrame1getJInternalFrame1(Stringname,booleanb1,booleanb2,booleanb3,booleanb4){

if(JIF1==null)

JIF1=newJInternalFrame1(name,b1,b2,b3,b4);

returnJIF1;

}

}

JInternalFrameDemo.java

packagetest;

importjava.awt.BorderLayout;

importjava.awt.Container;

importjava.awt.Dimension;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.WindowAdapter;

importjava.awt.event.WindowEvent;

importjavax.swing.JButton;

importjavax.swing.JDesktopPane;

importjavax.swing.JFrame;

importjavax.swing.JInternalFrame;

importjavax.swing.JTextArea;

 

publicclassJInternalFrameDemoextendsJFrameimplementsActionListener{

privatestaticJInternalFrame1internalFrame;

ContainercontentPane=this.getContentPane();

publicJInternalFrameDemo(){

super("主窗体");

contentPane.setLayout(newBorderLayout());

JButtonbutton=newJButton("创建一个子窗体");

button.addActionListener(this);

contentPane.add(button,BorderLayout.SOUTH);

this.setSize(newDimension(300,300));

this.setVisible(true);

this.addWindowListener(newWindowAdapter(){

@Override

publicvoidwindowClosing(WindowEvente){

System.exit(0);

}

});

}

publicvoidactionPerformed(ActionEvente){

internalFrame=JInternalFrame1.getJInternalFrame1("子窗体"

true,true,true,true);

internalFrame.setSize(newDimension(200,200));

internalFrame.setVisible(true);

JDesktopPanedesktopPane=newJDesktopPane();

contentPane.add(desktopPane);

desktopPane.add(internalFrame);

try{

internalFrame.setSelected(true);

}catch(java.beans.PropertyVetoExceptionex){

System.out.println("Exceptionwhileselecting");

}

}

 

publicstaticvoidmain(Stringargs[]){

newJInternalFrameDemo();

}

}

 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2