设计模式上机实验一实验报告.docx
《设计模式上机实验一实验报告.docx》由会员分享,可在线阅读,更多相关《设计模式上机实验一实验报告.docx(34页珍藏版)》请在冰豆网上搜索。
设计模式上机实验一实验报告
设计模式实验一
实验报 告书
专业班级软件0703
学号3901070324
姓名吉亚云
指导老师刘伟
时间2010年3月27日
中南大学软件学院
实验一设计模式上机实验一
一、实验目的
熟练使用PowerDesigner绘制类图,构造系统的静态结构模型;使用PowerDesigner和任意一种面向对象编程语言实现几种常见的设计模式,包括简单工厂模式、工厂方法模式、抽象工厂模式、原型模式、单例模式、适配器模式、桥接模式和组合模式。
二、实验要求
1.使用PowerDesigner绘制标准的UML类图;
2.正确无误绘制简单工厂模式、工厂方法模式、抽象工厂模式、原型模式、单例模式、适配器模式、桥接模式和组合模式的模式结构图;
3.使用任意一种面向对象编程语言实现简单工厂模式、工厂方法模式、抽象工厂模式、原型模式、单例模式、适配器模式、桥接模式和组合模式,代码运行正确无误。
三、实验步骤
1.熟悉PowerDesigner11的常用功能;
2.使用PowerDesigner绘制类图;
3.使用PowerDesigner绘制简单工厂模式结构图并用面向对象编程语言实现该模式;
4.使用PowerDesigner绘制工厂方法模式结构图并用面向对象编程语言实现该模式;
5.使用PowerDesigner绘制抽象工厂模式结构图并用面向对象编程语言实现该模式;
6.使用PowerDesigner绘制原型模式结构图并用面向对象编程语言实现该模式;
7.使用PowerDesigner绘制单例模式结构图并用面向对象编程语言实现该模式;
8.使用PowerDesigner绘制适配器模式结构图并用面向对象编程语言实现该模式;
9.使用PowerDesigner绘制桥接模式结构图并用面向对象编程语言实现该模式;
10.使用PowerDesigner绘制组合模式结构图并用面向对象编程语言实现该模式。
四、实验报告要求
1.绘制实例场景的类图;
2.提供简单工厂模式结构图及实现代码;
3.提供工厂方法模式结构图及实现代码;
4.提供抽象工厂模式结构图及实现代码;
5.提供原型模式结构图及实现代码;
6.提供单例模式结构图及实现代码;
7.提供适配器模式结构图及实现代码;
8.提供桥接模式结构图及实现代码;
9.提供组合模式结构图及实现代码。
五、实验结果
使用PowerDesigner绘制类图;用PowerDesigner和任意一种面向对象编程语言实现简单工厂模式、工厂方法模式、抽象工厂模式、原型模式、单例模式、适配器模式、桥接模式和组合模式,包括根据实例绘制模式结构图、编写模式实现代码,运行并测试模式实例代码。
(1)类图
某音像图书出租公司欲开发一个音像图书租赁信息系统,处理日常音像图书租借业务。
该系统将记录所有的图书信息、唱碟信息、用户信息和用户租借信息。
现决定采用面向对象分析和设计方法来开发此系统,经过初步分析,设计人员定义了如下初始实体类:
●Book(图书)类
属性:
no(编号)、title(标题)、author(作者)、isbn(书号)、press(出版社)、price(价格)。
方法:
每个属性的get()方法和set()方法。
●Disc(唱碟)类
属性:
no(编号)、title(标题)、singer(歌手)、producer(制作公司)、price(价格)。
方法:
每个属性的get()方法和set()方法。
●Customer(客户)类
属性:
cno(客户编号)、name(姓名)、address(地址)、tel(电话)。
方法:
每个属性的get()方法和set()方法。
●LoanItem(可租项目)类
记录Book和Disc的共有属性和方法。
●RentHistory(租借历史记录)类
属性:
rno(历史记录编号)、hireTime(出租时间)、returnTime(归还时间)、cno(客户编号)、no(租借图书或唱碟编号)、deposit(押金金额)。
方法:
每个属性的get()方法和set()方法。
在进一步分析之后,需要在LoanItem类中定义一个集合来存储每一种图书或唱碟的所有租借历史记录。
根据以上信息,绘制对应的实体类类图。
(2)简单工厂模式
使用简单工厂模式设计一个可以创建不同几何形状(Shape)的绘图工具类,如可创建圆形(Circle)、方形(Rectangle)和三角形(Triangle)对象,每个几何图形都要有绘制draw()和擦除erase()两个方法,要求在绘制不支持的几何图形时,提示一个UnsupportedShapeException,绘制类图并编程实现。
//抽象图形
interfaceShape
{
voiddraw();
voiderase();
}
classCircleimplementsShape
{
publicvoiddraw()
{
System.out.println("Circledrawing!
");
}
publicvoiderase()
{
System.out.println("Circleeraseing!
");
}
}
classRectangleimplementsShape
{
publicvoiddraw()
{
System.out.println("Rectangledrawing!
");
}
publicvoiderase()
{
System.out.println("Rectangleeraseing!
");
}
}
classTriangleimplementsShape
{
publicvoiddraw()
{
System.out.println("Triangledrawing!
");
}
publicvoiderase()
{
System.out.println("Triangleeraseing!
");
}
}
//工厂
classShapeFactory
{
//工厂方法factorymethod
publicstaticShapeproduceShape(StringshapeName)throwsUnsupportedShapeException
{
if(shapeName.equalsIgnoreCase("circle"))
{
returnnewCircle();
}
elseif(shapeName.equalsIgnoreCase("rectangle"))
{
returnnewRectangle();
}
elseif(shapeName.equalsIgnoreCase("triangle"))
{
returnnewTriangle();
}
else
{
thrownewUnsupportedShapeException();
}
}
}
classUnsupportedShapeExceptionextendsException{
publicStringtoString(){
return"绘制不支持该几何图形!
";
}
}
classShapeClient
{
publicstaticvoidmain(Stringa[])
{
Shapecircle=null;
try{
circle=ShapeFactory.produceShape("circle");
}catch(UnsupportedShapeExceptione){
e.printStackTrace();
}
circle.draw();
circle.erase();
Shapetriangle=null;
try{
triangle=ShapeFactory.produceShape("triangle");
}catch(UnsupportedShapeExceptione){
e.printStackTrace();
}
triangle.draw();
triangle.erase();
}
}
(3)工厂方法模式
海尔工厂(Haier)生产海尔空调(HaierAirCondition),美的工厂(Midea)生产美的空调(MideaAirCondition)。
使用工厂方法模式描述该场景,绘制类图并编程实现。
interfaceAirCondition
{
voidwork();
}
interfaceAirConditionFactory
{
AirConditionproduceAirCondition();
}
classHaierAirConditionimplementsAirCondition
{
publicvoidwork()
{
System.out.println("HaierAirConditionworking!
");
}
}
classMideaAirConditionimplementsAirCondition
{
publicvoidwork()
{
System.out.println("MideaAirConditionworking!
");
}
}
classHaierAirConditionFactoryimplementsAirConditionFactory
{
publicHaierAirConditionproduceAirCondition()
{
System.out.println("HaierAirConditionproduced!
");
returnnewHaierAirCondition();
}
}
classMideaAirConditionFactoryimplementsAirConditionFactory
{
publicMideaAirConditionproduceAirCondition()
{
System.out.println("MideaAirConditionproduced!
");
returnnewMideaAirCondition();
}
}
classAirConditionClient
{
publicstaticvoidmain(Stringa[])
{
AirConditionFactorycf;
AirConditionairCondition;
cf=(AirConditionFactory)XMLUtil.getBean();//这句代码中调用XML配置文件中的getBean函数,实现从XML配置文件中读取类的名字,创建该类的对象,最后返回给调用getBean()方法的Java程序中。
airCondition=cf.produceAirCondition();
airCondition.work();
}
}
(4)抽象工厂模式
电脑配件生产工厂生产内存、CPU等硬件设备,这些内存、CPU的品牌、型号并不一定相同,根据下面的“产品等级结构-产品族”示意图,使用抽象工厂模式实现电脑配件生产过程并绘制相应的类图,绘制类图并编程实现。
interfaceCPU
{
voiddiscribe();
}
interfaceRAM
{
voiddiscribe();
}
classPcCPUimplementsCPU
{
publicvoiddiscribe()
{
System.out.println("PcCPU");
}
}
classMacCPUimplementsCPU
{
publicvoiddiscribe()
{
System.out.println("MacCPU");
}
}
classPcRAMimplementsRAM
{
publicvoiddiscribe()
{
System.out.println("PcRAM");
}
}
classMacRAMimplementsRAM
{
publicvoiddiscribe()
{
System.out.println("MacRAM");
}
}
interfaceComputerPartsFactory
{
CPUproduceCPU();
RAMproduceRAM();
}
classPcFactoryimplementsComputerPartsFactory
{
publicPcCPUproduceCPU(){
System.out.println("PcCPUproduced!
");
returnnewPcCPU();
}
publicPcRAMproduceRAM(){
System.out.println("PcRAMproduced!
");
returnnewPcRAM();
}
}
classMacFactoryimplementsComputerPartsFactory
{
publicMacCPUproduceCPU(){
System.out.println("MacCPUproduced!
");
returnnewMacCPU();
}
publicMacRAMproduceRAM(){
System.out.println("MacRAMproduced!
");
returnnewMacRAM();
}
}
classComputerPartsClient
{
publicstaticvoidmain(Stringa[])
{
ComputerPartsFactoryfactory;
CPUcpu;
RAMram;
factory=(ComputerPartsFactory)XMLUtil.getBean();
cpu=factory.produceCPU();
cpu.discribe();
ram=factory.produceRAM();
ram.discribe();
}
}
(5)原型模式
设计一个客户类Customer,其中客户地址存储在地址类Address中,用浅克隆和深克隆分别实现Customer对象的复制并比较这两种克隆方式的异同。
绘制类图并编程实现。
classAddressimplementsSerializable
{
publicvoiddisplayAddress(){
System.out.println("address...");
}
}
//浅克隆的Customer类
classCustomerimplementsCloneable
{
privateAddressaddress=null;
publicAddressgetAddress(){
returnaddress;
}
publicvoidsetAddress(Addressaddress){
this.address=address;
}
publicObjectclone()
{
Customerclone=null;
try
{
clone=(Customer)super.clone();
}
catch(CloneNotSupportedExceptione)
{
System.out.println("Clonefailure!
");
}
returnclone;
}
}
//深克隆的Customer类
classCustomerDeepCloneimplementsCloneable,Serializable
{
privateAddressaddress=null;
publicAddressgetAddress(){
returnaddress;
}
publicvoidsetAddress(Addressaddress){
this.address=address;
}
publicObjectclone()
{
Customerclone=null;
try
{
//使用流来进行处理
ByteArrayOutputStreambao=newByteArrayOutputStream();
ObjectOutputStreamoos=newObjectOutputStream(bao);
oos.writeObject(this);
ByteArrayInputStreambis=newByteArrayInputStream(bao.toByteArray());
ObjectInputStreamois=newObjectInputStream(bis);
return(ois.readObject());
}catch(IOExceptione){
e.printStackTrace();
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
returnclone;
}
}
classCustomerClient
{
publicstaticvoidmain(Stringa[])
{
Customercustomer=newCustomer();
customer.setAddress(newAddress());
CustomercopyCustomer=(Customer)customer.clone();
System.out.println("customer==copyCustomer?
");
System.out.println(customer==copyCustomer);
System.out.println("customer.getAddress==copyCustomer.getAddress?
");System.out.println(customer.getAddress()==copyCustomer.getAddress());
CustomerDeepClonecustomerDeepClone=newCustomerDeepClone();
customerDeepClone.setAddress(newAddress());
CustomerDeepClonecopyCustomerDeepClone=(CustomerDeepClone)customerDeepClone.clone();
System.out.println("customerDeepClone==copyCustomerDeepClone?
");
System.out.println(customerDeepClone==copyCustomerDeepClone);
System.out.println("customerDeepClone.getAddress==copyCustomerDeepClone.getAddress?
");
System.out.println(customerDeepClone.getAddress()==copyCustomerDeepClone.getAddress());
}
}
运行结果:
(6)单例模式
用懒汉式单例实现在某系统运行时,其登录界面类LoginForm只能够弹出一个,如果第二次实例化该类则提示“程序已运行”。
绘制类图并编程实现。
classLoginFormextendsJFrame{
privatestaticLoginFormuniqueInstance=null;
privateLoginForm()
{
//设置标题
this.setTitle("LoginForm");
this.setSize(400,300);
//设置关闭按钮的事件
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//设置是否可见
this.setVisible(true);
}
publicstaticLoginFormgetInstance()
{
if(uniqueInstance==null)
{
uniqueInstance=newLoginForm();
}else{
JOptionPane.showMessageDialog(null,"LoginFrame已经在运行!
");
}
returnuniqueInstance;
}
}
classLoginFormClient
{
publicstaticvoidmain(Stringa[])
{
LoginForms1=null,s2=null;
s1=LoginForm.getInstance();
s2=LoginForm.getInstance();
System.out.println("s1==s2?
");
System.out.println(s1==s2);
}
}
运行结果:
(7)适配器模式
现有一个接口DataOperation定义了排序方法sort(int[])和查找方法search(int[],int),已知类QuickSort的quickSort(int[])方法实现了快速排序算法,类BinarySearch的binarySearch(int[],int)方法实现了二分查找算法