肖正文命令模式.docx

上传人:b****5 文档编号:7662290 上传时间:2023-01-25 格式:DOCX 页数:24 大小:115.21KB
下载 相关 举报
肖正文命令模式.docx_第1页
第1页 / 共24页
肖正文命令模式.docx_第2页
第2页 / 共24页
肖正文命令模式.docx_第3页
第3页 / 共24页
肖正文命令模式.docx_第4页
第4页 / 共24页
肖正文命令模式.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

肖正文命令模式.docx

《肖正文命令模式.docx》由会员分享,可在线阅读,更多相关《肖正文命令模式.docx(24页珍藏版)》请在冰豆网上搜索。

肖正文命令模式.docx

肖正文命令模式

云南大学软件学院

实验报告

序号:

姓名:

肖正文学号:

20121120141专业:

软件工程日期:

2014/12/22成绩:

实验四命令模式的运用

一、实验目的:

命令模式将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象,命令模式也支持可撤销的操作。

在熟悉命令模式相关理论知识的基础上,使用命令模式实现图片处理程序。

二、实验要求:

使用命令模式实现图片处理程序,要求如下:

1.图片处理程序要有3张图片。

2.每张图片至少有3种操作。

3.实现类似遥控器的菜单,动态的选择对图片进行的处理。

4.要有“撤消操作”,撤消操作要可以撤销至最后一步。

1、设计并绘制该程序的类图;

2、依照设计的类图使用Java语言编写代码,并实现该程序;

3、除了核心的模式相关类实现外,提供测试环境,按照难度高低,分别是:

a)控制台程序,Client硬编码初始化模式和测试环境,运行结果文本输出;

b)控制台程序,Client初始化测试环境,并根据用户输入运算,运行结果文本输出;

c)设计并实现用户UI,Client初始化测试环境,并根据用户在UI控件上的输入运算,运行结果文本输出;

三、实验内容:

1体系结构

2类图

1.命令

2.packagexiao.it.Commend;

3./**

4.*命令接口

5.*@authorAdministrator

6.*

7.*/

8.publicinterfaceCommand{

9.publicStringexecute();

10.}

每张图片1的控制命令

1切割

packagexiao.it.ConcreteCommend1;

importxiao.it.Commend.Command;

importxiao.it.Picture.Picture1;

publicclassCutCommandimplementsCommand{

Picture1p1=null;

publicCutCommand(Picture1p){

this.p1=p;

}

@Override

publicStringexecute(){

//TODOAuto-generatedmethodstub

returnp1.cut();

}

}

2旋转

packagexiao.it.ConcreteCommend1;

importxiao.it.Commend.Command;

importxiao.it.Picture.Picture1;

/**

*对图像1进行旋转

*@authorAdministrator

*

*/

publicclassRotaCommandimplementsCommand{

Picture1p1=null;

publicRotaCommand(Picture1p){

this.p1=p;

}

@Override

publicStringexecute(){

//TODOAuto-generatedmethodstub

returnp1.rota();

}

}

3平移

packagexiao.it.ConcreteCommend1;

importxiao.it.Commend.Command;

importxiao.it.Picture.Picture1;

publicclassTransCommandimplementsCommand{

Picture1p1=null;

publicTransCommand(Picture1p){

this.p1=p;

}

@Override

publicStringexecute(){

//TODOAuto-generatedmethodstub

returnp1.trans();

}

}

图片2的具体命令

1放大

packagexiao.it.ConcreteCommend2;

importxiao.it.Commend.Command;

importxiao.it.Picture.Picture1;

importxiao.it.Picture.Picture2;

/**

*对图片2进行放大

*@authorAdministrator

*

*/

publicclassFangdaCommandimplementsCommand{

Picture2p2=null;

publicFangdaCommand(Picture2p){

this.p2=p;

}

@Override

publicStringexecute(){

//TODOAuto-generatedmethodstub

returnp2.Fangda();

}

}

2缩小

packagexiao.it.ConcreteCommend2;

importxiao.it.Commend.Command;

importxiao.it.Picture.Picture2;

/**

*对图片2进行缩小

*@authorAdministrator

*

*/

publicclassSuoxiCommandimplementsCommand{

Picture2p2=null;

publicSuoxiCommand(Picture2p){

this.p2=p;

}

@Override

publicStringexecute(){

//TODOAuto-generatedmethodstub

returnp2.Suoxi();

}

}

3伪彩色

packagexiao.it.ConcreteCommend2;

importxiao.it.Commend.Command;

importxiao.it.Picture.Picture2;

publicclassWecaiseCommandimplementsCommand{

Picture2p2=null;

publicWecaiseCommand(Picture2p){

this.p2=p;

}

@Override

publicStringexecute(){

//TODOAuto-generatedmethodstub

returnp2.Wecaise();

}

}

图片3处理方式

滤波

packagexiao.it.ConcreteCommend3;

importxiao.it.Commend.Command;

importxiao.it.Picture.Picture2;

importxiao.it.Picture.Picture3;

/*8

*对图片3进行滤波

*/

publicclassLuBoCommandimplementsCommand{

Picture3p3=null;

publicLuBoCommand(Picture3p){

this.p3=p;

}

@Override

publicStringexecute(){

//TODOAuto-generatedmethodstub

returnp3.LuBo();

}

}

2锐化

packagexiao.it.ConcreteCommend3;

importxiao.it.Commend.Command;

importxiao.it.Picture.Picture3;

/**

*对图片3进行锐化

*@authorAdministrator

*

*/

publicclassRuihaCommandimplementsCommand{

Picture3p3=null;

publicRuihaCommand(Picture3p){

this.p3=p;

}

@Override

publicStringexecute(){

//TODOAuto-generatedmethodstub

returnp3.Ruiha();

}

}

曾强

packagexiao.it.ConcreteCommend3;

importxiao.it.Commend.Command;

importxiao.it.Picture.Picture3;

/**

*对图片3进行增强

*@authorAdministrator

*

*/

publicclassZenQiangCommandimplementsCommand{

Picture3p3=null;

publicZenQiangCommand(Picture3p){

this.p3=p;

}

@Override

publicStringexecute(){

//TODOAuto-generatedmethodstub

returnp3.ZenQiang();

}

}

远程控制器

packagexiao.it.control;

importjava.util.ArrayList;

importxiao.it.Commend.Command;

importxiao.it.NullConcreteCommend.Nocommand;

/**

*带有撤销的三种操作

*@authorAdministrator

*

*/

publicclassRenoteControler{

Command[]com1;

Command[]com2;

Command[]com3;

publicstaticintlength;

//用来存储一系列的撤销命令,以达到栈的目的

ArrayListundocommnd=newArrayList();

Commandnocommand=newNocommand();

CommandcurrentCommand;

//对命令初始化

publicRenoteControler(){

com1=newCommand[3];

com2=newCommand[3];

com3=newCommand[3];

for(inti=0;i<3;i++){

com1[i]=nocommand;

com2[i]=nocommand;

com3[i]=nocommand;

}

}

/**

*为每一张图片分配三种操作

*@parampicture

*@paramcom1

*@paramcom2

*@paramcom3

*/

publicvoidsetCommond(intpicture,Commandcom1,Commandcom2,Commandcom3){

1[picture]=com1;

2[picture]=com2;

3[picture]=com3;

}

/**

*对指定图片进行第一种操作

*@parampicture

*@return

*/

publicStringoperation1(intpicture){

this.undocommnd.add(com1[picture]);

returncom1[picture].execute();

}

/**

*对指定图片进行第二种操作

*@parampicture

*@return

*/

publicStringoperation2(intpicture){

this.undocommnd.add(com2[picture]);

returncom2[picture].execute();

}

/**

*对指定图片进行第三种操作

*@parampicture

*@return

*/

publicStringoperation3(intpicture){

this.undocommnd.add(com3[picture]);

returncom3[picture].execute();

}

/**

*对图片进行撤销操作

*@return

*/

publicvoidUndo(){

length=undocommnd.size();

System.out.println(length);

}

publicStringupstep(){

length--;

if(length>=0){

returnundocommnd.get(length).execute();

}else{

returnnull;

}

}

publicStringtoString(){

return"图片1\n{1旋转2切割3平移}\n\n\n图片2\n{1放大2缩小3伪彩色}\n\n\n图片3\n{1锐化2滤波3增强}";

}

}

无命令状态

packagexiao.it.NullConcreteCommend;

importxiao.it.Commend.Command;

publicclassNocommandimplementsCommand{

@Override

publicStringexecute(){

//TODOAuto-generatedmethodstub

returnnull;

}

}

图片类

packagexiao.it.Picture;

/**

*图片1

*@authorAdministrator

*

*/

publicclassPicture1{

/**

**对图片1进行旋转

*/

publicStringrota(){

return"图片1旋转";

}

/**

*对图片1切割

*/

publicStringcut(){

return"图片1切割";

}

/**

*对图片1平移

*/

publicStringtrans(){

return"图片1平移";

}

}

packagexiao.it.Picture;

/**

*图片2

*@authorAdministrator

*

*/

publicclassPicture2{

/**

*图片2放大

*/

publicStringFangda(){

return"图片2放大";

}

/**

*图片2缩小

*/

publicStringSuoxi(){

return"图片2缩小";

}

/**

*图片2伪彩色

*/

publicStringWecaise(){

return"伪彩色";

}

}

packagexiao.it.Picture;

/**

*图片3

*@authorAdministrator

*

*/

publicclassPicture3{

/**

*图片3锐化

*/

publicStringRuiha(){

return"图片3锐化";

}

/**

*图片3滤波

*/

publicStringLuBo(){

return"图片3滤波";

}

/**

*图片3增强

*/

publicStringZenQiang(){

return"图片3增强";

}

}

UI类

packagexiao.it.UI;

importjava.awt.Dimension;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.Box;

importjavax.swing.JButton;

importjavax.swing.JComboBox;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JPanel;

importjavax.swing.JScrollPane;

importjavax.swing.JSplitPane;

importjavax.swing.JTextArea;

importjavax.swing.JTextField;

importxiao.it.ConcreteCommend1.CutCommand;

importxiao.it.ConcreteCommend1.RotaCommand;

importxiao.it.ConcreteCommend1.TransCommand;

importxiao.it.ConcreteCommend2.FangdaCommand;

importxiao.it.ConcreteCommend2.SuoxiCommand;

importxiao.it.ConcreteCommend2.WecaiseCommand;

importxiao.it.ConcreteCommend3.LuBoCommand;

importxiao.it.ConcreteCommend3.RuihaCommand;

importxiao.it.ConcreteCommend3.ZenQiangCommand;

importxiao.it.Picture.Picture1;

importxiao.it.Picture.Picture2;

importxiao.it.Picture.Picture3;

importxiao.it.control.RenoteControler;

publicclassUI{

RenoteControlerre=null;

//三张图片

Picture1p11=null;

Picture2p21=null;

Picture3p31=null;

/**

*对图片1的三种操作

*/

CutCommandcut=null;

RotaCommandrot=null;

TransCommandtran=null;

/**

*对图片2的三种操作

*

*/

FangdaCommandfa=null;

SuoxiCommandsu=null;

WecaiseCommandwc=null;

/**

*对图片3的三种操作

*

*/

LuBoCommandlb=null;

RuihaCommandrh=null;

ZenQiangCommandzq=null;

publicvoidinit(){

JFramef=newJFrame();

f.setBounds(0,0,400,600);

Stringstr[]={"图片1","图片2","图片3"};

JPanelp1=newJPanel();

JButtonl1=newJButton("图片变换1:

");

p1.add(l1);

JPanelp2=newJPanel();

JButtonl2=newJButton("图片变换2:

");

p2.add(l2);

JPanelp3=newJPanel();

JButtonl3=newJButton("图片变换3:

");

p3.add(l3);

JPanelp4=newJPanel();

JButtonl4=newJButton("我要撤销:

");

p4.add(l4);

JPanelp5=newJPanel();

JButtonl5=newJButton("上一步:

");

p5.add(l5);

finalJTextAreapp=newJTextArea();

finalJComboBoxj1=newJComboBox(str);

j1.setPreferredSize(newDimension(200,20));

//j1.setBounds(0,0,200,20);

Boxbox1=Box.createVerticalBox();

Boxbox2=Box.createVerticalBox();

box1.add(j1);

box1.add(p1);

box1.add(p2);

box1.add(p3);

box1.add(p4);

box1.add(p5);

//初始化就显示变换规则

re=newRenoteControler();

//三张图片

p11=newPicture1();

p21=newPicture2();

p31=newPicture3();

/**

*对图片1的三种操作

*/

cut=newCutCommand(p11);

rot=newRotaCommand(p11);

tran=newTransCommand(p11);

/**

*对图片2的三种操作

*

*/

fa=newFangdaCommand(p21);

su=newSuoxiCommand(p21);

wc=newWecaiseCommand(p21);

/**

*对图片3的三种操作

*

*/

lb=newLuBoCommand(p31);

rh=newRuihaCommand(p31);

zq=newZenQiangCommand(p31);

re.setCommond(0,cut,rot,tran);

re.setCommond(1,fa,su,wc);

re.setCommon

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

当前位置:首页 > 高中教育 > 高中教育

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

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