资源描述
实验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. 使用单例模式设计一个多文档窗口(注:在Java AWT/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. 类图:
ﻩ实现代码:
//代码颜色使用黑色,字体使用Times New Roman或Arial,字号为五号,如
abstract class Shape
Shape、java
public interface Shape {
void draw();
void erase();
}
Rectangle、java
public class Rectangle implements Shape {
public void draw() {
System、out、println("draw Rectangle”);
}
public void erase(){
System、out、println("erase Rectangle”);
}
}
Circle、java
public class Circle implements Shape {
public void draw() {
System、out、println("draw Circle");
}
public void erase(){
System、out、println(”erase Circle");
}
}
Triangle、java
public class Triangle implements Shape {
public void draw() {
System、out、println("draw Triangle");
}
public void erase(){
System、out、println(”erase Triangle");
}
}
ShapeFactory、java
public class ShapeFactory {
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( UnsupportedShapeException use);
}
}
}
Client、java
public class Client {
public static void main(String args[]){
}
}
UnsupportedShapeException、java
public class UnsupportedShapeException extends Exception{
public UnsupportedShapeException() {
System、out、println("绘制图形异常,请确认输入图形.");
}
}
3. 类图:
实现代码:
ImageReader 、java
public interface ImageReader {
ﻩpublic void readImage();
}
JpgReader、java
public class JpgReader implements ImageReader {ﻩ
ﻩpublic void readImage(){
ﻩSystem、out、println("read jgp image”);
}
}
GifReader、java
public class GifReader implements ImageReader {
public void readImage(){
ﻩﻩSystem、out、println("read gif image”);
ﻩ}
}
ImageReaderFactory、java
public interface ImageReaderFactory {
public ImageReader produceImageReader();
}
JpgReaderFactory、java
public class JpgReaderFactory implements ImageReaderFactory {
public ImageReader produceImageReader(){
System、out、println("jpg reader");
ﻩ return new JpgReader();
ﻩ}
}
GifReaderFactory、java
public class GifReaderFactory implements ImageReaderFactory {
public ImageReader produceImageReader(){
ﻩ System、out、println(”gif reader");
return new GifReader();
ﻩ}
}
Client、java
public class Client {
public static void main(String args[]){
}
}
4. 类图:
实现代码:
AbstractButton、java
public interface AbstractButton {
}
GreenButton、java
public class GreenButton implements AbstractButton {
public void action(){
ﻩSystem、out、println("green button");
ﻩ}ﻩ
}
BlueButton、java
public class BlueButton implements AbstractButton {
public void action() {
ﻩSystem、out、println("blue button");
}
}
AbstractTextbox、java
public interface AbstractTextbox{
ﻩpublic void action();
}
GreenTextbox、java
public class GreenTextbox implements AbstractTextbox {
public void action() {
System、out、println("green textbox");
}
}
BlueTextbox、java
public class BlueTextbox implements AbstractTextbox {
ﻩpublic void action() {
ﻩ System、out、println(”blue textbox");
ﻩ}
}
AbstractboBox、java
public interface AbstractboBox {
public void action();
}
GreenboBox、java
public class GreenboBox implements AbstractboBox {
public void action() {
ﻩ System、out、println("green bo box");
}
}
BlueboBox、java
public class BlueboBox implements AbstractboBox {
public void action() {
ﻩﻩSystem、out、println(”blue bo box");
}
}
SkinFactory、java
public interface SkinFactory {
ﻩpublic AbstractButton createButton();
public AbstractTextbox createTextbox();
public AbstractboBox createboBox();
}
SpringSkinFactory、java
public class SpringSkinFactory implements SkinFactory {
ﻩpublic AbstractButton createButton() {
ﻩ System、out、println("生产green button”);
ﻩ return new GreenButton();
ﻩ}
ﻩpublic AbstractTextbox createTextbox() {
ﻩ System、out、println("生产green textbox”);
ﻩ return new GreenTextbox();
ﻩ}
ﻩpublic AbstractboBox createboBox() {
ﻩSystem、out、println("生产green bo box");
ﻩﻩreturn new GreenboBox();
}
}
SummerSkinFactory、java
public class SummerSkinFactory implements SkinFactory {
ﻩpublic AbstractButton createButton() {
ﻩSystem、out、println("生产 blue button”);
ﻩﻩreturn new BlueButton();
ﻩ}
public AbstractTextbox createTextbox() {
ﻩﻩSystem、out、println("生产 blue textbox");
ﻩreturn new BlueTextbox();
}
ﻩpublic AbstractboBox createboBox() {
ﻩ System、out、println("生产 blue bo box”);
return new BlueboBox();
}
}
Client、java
public class Client {
public static void main(String args[]){
}
}
5. 类图:
实现代码:
public class pute {
private static pute[] instance;
private pute() {
}
public static pute getInstance(int number) {
if(number 〈= 0)
System、out、println("请输入大于0得整数");
if(instance、length == 0){
for(int i = 0, i 〈 number, i++){
ﻩﻩ Instance[i]=new Singleton();
}
}
int x = (int)(Math、random() * number);
Int y = x + 1;
System、out、println("调用第y台电脑");
ﻩ return instance[x];
}
}
6. 类图:
ﻩ实现代码:
JInternalFrame1、java
package test;
import javax、swing、JInternalFrame;
public class JInternalFrame1 extends JInternalFrame{
private static JInternalFrame1 JIF1 = null;
ﻩprivate JInternalFrame1(String name, boolean b1, boolean b2, boolean b3, boolean b4){
ﻩﻩsuper(name, b1, b2, b3, b4);
}
public static JInternalFrame1 getJInternalFrame1(String name, boolean b1, boolean b2, boolean b3, boolean b4){
if(JIF1 == null)
ﻩ ﻩJIF1 = new JInternalFrame1(name, b1, b2, b3, b4);
return JIF1;
}
}
JInternalFrameDemo、java
package test;
import java、awt、BorderLayout;
import java、awt、Container;
import java、awt、Dimension;
import java、awt、event、ActionEvent;
import java、awt、event、ActionListener;
import java、awt、event、WindowAdapter;
import java、awt、event、WindowEvent;
import javax、swing、JButton;
import javax、swing、JDesktopPane;
import javax、swing、JFrame;
import javax、swing、JInternalFrame;
import javax、swing、JTextArea;
public class JInternalFrameDemo extends JFrame implements ActionListener {
ﻩ
private static JInternalFrame1 internalFrame;
ﻩContainer contentPane = this、getContentPane();
ﻩ
ﻩpublic JInternalFrameDemo(){
ﻩsuper("主窗体");
ﻩ contentPane、setLayout(new BorderLayout());
ﻩﻩJButton button = new JButton("创建一个子窗体”);
button、addActionListener(this);
ﻩﻩcontentPane、add(button, BorderLayout、SOUTH);ﻩ
ﻩ this、setSize(new Dimension(300, 300));
ﻩ this、setVisible(true);
ﻩ this、addWindowListener(new WindowAdapter() {
Override
public void windowClosing(WindowEvent e) {
System、exit(0);
}
ﻩ });
}
public void actionPerformed(ActionEvent e) { ﻩ
ﻩﻩinternalFrame = JInternalFrame1、getJInternalFrame1("子窗体”
ﻩﻩ , true, true, true, true);
ﻩinternalFrame、setSize(new Dimension(200, 200));
ﻩ internalFrame、setVisible(true);
ﻩﻩﻩJDesktopPane desktopPane = new JDesktopPane();
ﻩ contentPane、add(desktopPane);
ﻩﻩdesktopPane、add(internalFrame);
ﻩtry {
ﻩinternalFrame、setSelected(true);
ﻩﻩ } catch (java、beans、PropertyVetoException ex) {
ﻩﻩﻩ System、out、println("Exception while selecting");
ﻩ ﻩ}ﻩ
}
public static void main(String args[]){ﻩﻩ
ﻩﻩ new JInternalFrameDemo();ﻩﻩ
}
}
展开阅读全文