设计模式实验.docx
《设计模式实验.docx》由会员分享,可在线阅读,更多相关《设计模式实验.docx(63页珍藏版)》请在冰豆网上搜索。
设计模式实验
《代码重构与设计模式》课堂实验
徐海蛟博士2016.03
实验一工厂模式的应用
【实验目的】
1)掌握工厂模式(Factory)的特点
2)分析具体问题,使用工厂模式进行设计。
【实验内容和要求】
有一个OEM制造商代理做HP笔记本电脑(Laptop),后来该制造商得到了更
多的品牌笔记本电脑的订单Acer、Lenovo、Dell,该OEM商发现,如果一次同
时做很多个牌子的本本,有些不利于管理。
利用工厂模式改善设计,用控制台应
用程序实现该OEM制造商的工厂模式。
该模式的UML图如下。
【模式UML图】
【模式代码(JAVA语言实现)】
publicclassFactoryMethod{//主类
publicstaticvoidmain(String[]args){
Computerc;
Factoryf=newDellFactory();
c=f.getComputerType();
c.ComputerType();
f=newLenovoFactory();
1
《代码重构与设计模式》课堂实验
徐海蛟博士2016.03
c=f.getComputerType();
c.ComputerType();
f=newAcerFactory();
c=f.getComputerType();
c.ComputerType();
}
}
interfaceFactory{
ComputergetComputerType();
}
classDellFactoryimplementsFactory{
@Override
publicComputergetComputerType(){
returnnewDell();
}
}
classAcerFactoryimplementsFactory{
@Override
publicComputergetComputerType(){
returnnewAcer();
}
}
classLenovoFactoryimplementsFactory{
@Override
publicComputergetComputerType(){
returnnewLenovo();
}
}
/**
*电脑品牌
*/
interfaceComputer{
publicvoidComputerType();
}
classDellimplementsComputer{
@Override
publicvoidComputerType(){
//TODOAuto‐generatedmethodstub
2
《代码重构与设计模式》课堂实验
徐海蛟博士2016.03
System.out.println("DellComputer");
}
}
classAcerimplementsComputer{
@Override
publicvoidComputerType(){
System.out.println("AcerComputer");
}
}
classLenovoimplementsComputer{
@Override
publicvoidComputerType(){
//TODOAuto‐generatedmethodstub
System.out.println("LenovoComputer");
}
}
【运行截图】
【实验小结】
通过本次实验,学会了使用工厂方法模式。
工厂方法模式的适用性如下:
当一个类不知道它所必须创建的对象的类时。
当一个类希望由它的子类来指定它所创建的对象时。
当类将创建对象的职责委托给多个帮助子类中的某一个,并且希望将哪
一个帮助子类是代理这一信息局部化时。
3
《代码重构与设计模式》课堂实验
徐海蛟博士2016.03
实验二抽象工厂模式的应用
【实验目的】
1)掌握抽象工厂模式(AbstractFactory)的特点
2)分析具体问题,使用抽象工厂模式进行设计。
【实验内容和要求】
麦当劳(McDonalds)和肯德基(KFC)快餐店都经营汉堡(Hamburg)和
可乐(Cola),用JAVA控制台应用程序实现这两个快餐店经营产品的抽象工厂
模式。
该模式的UML图如下。
【模式UML图】
【模式代码】
publicclassAbstractFactoryTest{
publicstaticvoidmain(String[]args){
Hamburgh;
Colac;
AbstractFactoryaf=newMDNFactory();
4
《代码重构与设计模式》课堂实验
徐海蛟博士2016.03
h=af.createHamburg();
c=af.createCola();
h.getHumburg();
c.getCola();
af=newKDJFactory();
h=af.createHamburg();
c=af.createCola();
h.getHumburg();
c.getCola();
}
}
interfaceAbstractFactory{
HamburgcreateHamburg();
ColacreateCola();
}
classMDNFactoryimplementsAbstractFactory{
@Override
publicHamburgcreateHamburg(){
returnnewMDNHamburg();
}
@Override
publicColacreateCola(){
returnnewMDNCola();
}
}
classKDJFactoryimplementsAbstractFactory{
@Override
publicHamburgcreateHamburg(){
returnnewKDJHamburg();
}
@Override
publicColacreateCola(){
returnnewKDJCola();
}
}
/**
*kDJ&MDN
*/
interfaceHamburg{
5
《代码重构与设计模式》课堂实验
徐海蛟博士2016.03
voidgetHumburg();
}
classMDNHamburgimplementsHamburg{
@Override
publicvoidgetHumburg(){
System.out.println("MDNHamburg");
}
}
classKDJHamburgimplementsHamburg{
@Override
publicvoidgetHumburg(){
//TODOAuto‐generatedmethodstub
System.out.println("KDJHamburg");
}
}
interfaceCola{
voidgetCola();
}
classMDNColaimplementsCola{
@Override
publicvoidgetCola(){
System.out.println("MDNCola");
}
}
classKDJColaimplementsCola{
@Override
publicvoidgetCola(){
System.out.println("KDJCola");
}
}
【运行截图】
6
《代码重构与设计模式》课堂实验
徐海蛟博士2016.03
【实验小结】
抽象工厂模式主要适用于以下情况:
一系列要独立于它的产品的创建、组合和表示时。
、
一个系统要由多个产品系列中的一个来配置时。
当要强调一系列相关的产品对象的设计以便进行联合使用时。
当要提供一个产品类库,而只要显示它们的接口而不是实现时。
7
《代码重构与设计模式》课堂实验
徐海蛟博士2016.03
实验三适配器模式的应用
【实验目的】
1)掌握适配器模式(Adapter)的特点
2)分析具体问题,使用适配器模式进行设计。
【实验内容和要求】
一个软件团队开发绘图系统,设计了圆对象(Circle)、矩形对象(Rectangle),
线对象(Line)都支持Draw()函数,即可以通过Draw()函数绘制图形。
为了加快项
目进度,将角度对象(Angle)绘制功能交给了合作团队实现。
但合作团队将角度
对象绘制函数定为了DrawAngle()。
绘图系统提供给用户后,用户不满意,希望
能统一的调用,不用记太多命令。
应用适配器模式,用JAVA控制台应用程序完
善该设计。
该模式的UML图如下。
【模式UML图】
【模式代码】
publicclassAdapterTest{
publicstaticvoidmain(String[]args){
Painta=newAngleAdapter();
a.draw();
}
}
8
《代码重构与设计模式》课堂实验
interfacePaint{
徐海蛟博士2016.03
voiddraw();
}
classCircleimplementsPaint{
@Override