设计模式实验三.docx

上传人:b****6 文档编号:3484428 上传时间:2022-11-23 格式:DOCX 页数:14 大小:272.17KB
下载 相关 举报
设计模式实验三.docx_第1页
第1页 / 共14页
设计模式实验三.docx_第2页
第2页 / 共14页
设计模式实验三.docx_第3页
第3页 / 共14页
设计模式实验三.docx_第4页
第4页 / 共14页
设计模式实验三.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

设计模式实验三.docx

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

设计模式实验三.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.类图:

实现代码:

ava

publicinterfaceImageReader{

publicvoidreadImage();

}

 

publicclassJpgReaderimplementsImageReader{

publicvoidreadImage(){

"readjgpimage");

}

}

 

publicclassGifReaderimplementsImageReader{

publicvoidreadImage(){

"readgifimage");

}

}

 

publicinterfaceImageReaderFactory{

publicImageReaderproduceImageReader();

}

 

publicclassJpgReaderFactoryimplementsImageReaderFactory{

publicImageReaderproduceImageReader(){

"jpgreader");

returnnewJpgReader();

}

}

 

publicclassGifReaderFactoryimplementsImageReaderFactory{

publicImageReaderproduceImageReader(){

"gifreader");

returnnewGifReader();

}

}

 

publicclassClient{

publicstaticvoidmain(Stringargs[]){

}

}

3.类图:

实现代码:

publicinterfaceAbstractButton{

}

 

publicclassGreenButtonimplementsAbstractButton{

publicvoidaction(){

"greenbutton");

}

}

 

publicclassBlueButtonimplementsAbstractButton{

publicvoidaction(){

"bluebutton");

}

}

 

publicinterfaceAbstractTextbox{

publicvoidaction();

}

 

publicclassGreenTextboximplementsAbstractTextbox{

publicvoidaction(){

"greentextbox");

}

}

 

publicclassBlueTextboximplementsAbstractTextbox{

publicvoidaction(){

"bluetextbox");

}

}

 

publicinterfaceAbstractComboBox{

publicvoidaction();

}

 

publicclassGreenComboBoximplementsAbstractComboBox{

publicvoidaction(){

"greencombobox");

}

}

 

publicclassBlueComboBoximplementsAbstractComboBox{

publicvoidaction(){

"bluecombobox");

}

}

 

publicinterfaceSkinFactory{

publicAbstractButtoncreateButton();

publicAbstractTextboxcreateTextbox();

publicAbstractComboBoxcreateComboBox();

}

 

publicclassSpringSkinFactoryimplementsSkinFactory{

publicAbstractButtoncreateButton(){

"生产greenbutton");

returnnewGreenButton();

}

publicAbstractTextboxcreateTextbox(){

"生产greentextbox");

returnnewGreenTextbox();

}

publicAbstractComboBoxcreateComboBox(){

"生产greencombobox");

returnnewGreenComboBox();

}

}

 

publicclassSummerSkinFactoryimplementsSkinFactory{

publicAbstractButtoncreateButton(){

"生产bluebutton");

returnnewBlueButton();

}

publicAbstractTextboxcreateTextbox(){

"生产bluetextbox");

returnnewBlueTextbox();

}

publicAbstractComboBoxcreateComboBox(){

"生产bluecombobox");

returnnewBlueComboBox();

}

}

 

publicclassClient{

publicstaticvoidmain(Stringargs[]){

}

}

4.类图:

实现代码:

publicclassCompute{

privatestaticCompute[]instance;

privateCompute(){

}

publicstaticComputegetInstance(intnumber){

if(number<=0)

"请输入大于0的整数");

if==0){

for(inti=0,i

Instance[i]=newSingleton();

}

}

intx=(int)()*number);

Inty=x+1;

"调用第y台电脑");

returninstance[x];

}

}

5.类图:

实现代码:

packagetest;

importclassJInternalFrame1extendsJInternalFrame{

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;

}

}

 

packagetest;

importclassJInternalFrameDemoextendsJFrameimplementsActionListener{

privatestaticJInternalFrame1internalFrame;

ContainercontentPane=();

publicJInternalFrameDemo(){

super("主窗体");

(newBorderLayout());

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

(this);

(button,;

(newDimension(300,300));

(true);

(newWindowAdapter(){

@Override

publicvoidwindowClosing(WindowEvente){

(0);

}

});

}

publicvoidactionPerformed(ActionEvente){

internalFrame=("子窗体"

true,true,true,true);

(newDimension(200,200));

(true);

JDesktopPanedesktopPane=newJDesktopPane();

(desktopPane);

(internalFrame);

try{

(true);

}catchex){

"Exceptionwhileselecting");

}

}

 

publicstaticvoidmain(Stringargs[]){

newJInternalFrameDemo();

}

}

 

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

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1