栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

软件设计实验二 创建型设计模式实验

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

软件设计实验二 创建型设计模式实验

一、实验目的

9.结合实例,熟练绘制常见的创建型设计模式结构图。
10.结合实例,熟练使用任意一种面向对象编程语言实现常见的创建型设计模式。
11.通过本实验,理解每一种创建型设计模式的模式动机,掌握模式结构,学习如何使用代码实现这些设计模式。

二、实验内容

1.使用简单工厂模式设计一个可以创建不同几何形状(Shape),如圆形(Circle)、矩形
(Rectangle)和三角形(Triangle)等的绘图工具类,每个几何图形均具有绘制draw()和擦除erase()两个方法,要求在绘制不支持的几何图形时,抛出一个 UnsupportedShapeException 异常,绘制类图并编程模拟实现。
2.用Java代码实现教材中“日志记录器”实例,如果在系统中增加一个日志记录方式——控制台日志记录(ConsoleLog),绘制类图并修改代码。
3. 计算机包含内存(RAM)、CPU等硬件设备,根据下图所示的“产品等级结构-产品族”示意图,使用抽象工厂模式实现计算机设备创建过程并绘制相应的类图。

4.使用单例模式设计一个多文档窗口(注:在 Java AWT/Swing 开发中可使用
JDesktopPane 和 JInternalFrame 来实现),要求在主窗体中某个内部子窗体只能实例化一次,即只能弹出一个相同的子窗体,如下图所示,编程实现该功能。

5.某软件公司欲开发一套界面皮肤库,可以对桌面软件进行界面美化。不同的皮肤将提供视觉效果不同的按钮、文本框、组合框等界面元素,其结构如下图所示:

该皮肤库需要具备良好的灵活性和可扩展性,用户可以自由选择不同的皮肤,开发人员可以在不修改既有代码的基础上增加新的皮肤。试使用抽象工厂模式设计该皮肤库,绘制类图并编程模拟实现。
6.在某网络管理软件中,需要不同的网络协议提供不同的连接类,例如针对POP3协议的连接类POP3Connection、针对IMAP协议的连接类IMAPConnection、针对HTTP协议的连接类HTTPConnection等。由于网络连接对象的创建过程较为复杂,需要将其创建过程封装到专门的类中,该软件还将支持更多类型的网络协议。现采用工厂方法模式进行设计,绘制类图并编程模拟实现。
7. 某系统为了改进数据库操作的性能,用户可以自定义数据库连接对象Connection和语句对象Statement,针对不同类型的数据库提供不同的连接对象和语句对象,如提供Oracle或MySQL专用连接类和语句类,而且用户可以通过配置文件等方式根据实际需要动态更换系统数据库。使用抽象工厂模式设计该系统,绘制对应的类图并编程模拟实现。

三、实验要求
  1. 结合实例,绘制常见创建型设计模式的结构图;
  2. 使用任意一种面向对象编程语言实现常见创建型设计模式实例,代码运行正确。
    四、实验步骤
  3. 结合实例,绘制简单工厂模式实例结构图并用面向对象编程语言实现该模式实例;
  4. 结合实例,绘制工厂方法模式实例结构图并用面向对象编程语言实现该模式实例;
  5. 结合实例,绘制抽象工厂模式实例结构图并用面向对象编程语言实现该模式实例;
  6. 结合实例,绘制单例模式实例结构图并用面向对象编程语言实现该模式实例;
  7. 结合实例,绘制抽象工厂模式实例结构图并用面向对象编程语言实现该模式实例;
  8. 结合实例,绘制工厂方法模式实例结构图并用面向对象编程语言实现该模式实例;
  9. 结合实例,绘制抽象工厂模式实例结构图并用面向对象编程语言实现该模式实例。
    五、实验结果
  10. 需要提供简单工厂模式实例的结构图(类图)和实现代码。
    (1)类图实现:

(2)代码实现
//工厂类:
class ShapeFactory{
public static Shape createShape(String type) throws UnsupportedShapeException{
if(type.equalsIgnoreCase(“Circle”)) return new Circle();
else if(type.equalsIgnoreCase(“Rectangle”)) return new Rectangle();
else if(type.equalsIgnoreCase(“Triangle”)) return new Triangle();
else throw new UnsupportedShapeException(“不支持该形状!”);
} }
//抽象产品类
public interface Shape{ public void draw(); public void erase(); }
//具体产品类:Circle/Triangle/Rectangle
public class Circle/Triangle/Rectangle implements Shape{
public void draw(){ System.out.println(“绘制圆形/三角形/矩形”); }
public void erase(){ System.out.println(“删除圆形/三角形/矩形”); } }
//自定义异常类
class UnsupportedShapeException extends Exception{
public UnsupportedShapeException(String message){
super(message); } }
//客户端测试类
class Client{
public static void main(String args[]){
try{ Shape shape;
String shapeName=XMLUtilTV.getShapeName();
shape=ShapeFactory.createShape(shapeName);
shape.draw(); shape.erase();
}catch(UnsupportedShapeException e){
System.out.println(e.getMessage()); }
} }
//配置文件
import javax.xml.parsers.;
import org.w3c.dom.;
import org.xml.sax.SAXException;
import java.io.*;
public class XMLUtilTV{
public static String getShapeName(){
try{DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc; doc = builder.parse(new File(“shape.xml”));
NodeList nl = doc.getElementsByTagName(“shapeName”);
Node classNode=nl.item(0).getFirstChild();
String shapeName=classNode.getNodeValue().trim();
return shapeName;
} catch(Exception e){
e.printStackTrace(); return null; }
}
}
2. 需要提供工厂方法模式实例的结构图(类图)和实现代码。
(1)类图实现:

(2)代码实现:
//抽象工厂
public interface LogFactory{ public Log createLog(); }
//抽象产品类
public interface Log{ public void writeLog(); }
//具体产品类:FileLog
public class FileLog implements Log{
public void writeLog(){ System.out.println(“文件日志记录。”); } }
//具体工厂类:DatabaseLogFactory/FileLogFactory/ConsoleLogFactory
public class DatabaseLogFactory/FileLogFactory/ConsoleLogFactory implements LogFactory{
public Log createLog(){
return new DatabaseLog()/FileLog()/ConsoleLog();
} }
//具体产品类:ConsoleLog
public class ConsoleLog implements Log {
public void writeLog(){
System.out.println(“控制台日志记录方式。”); } }
//具体产品类:DatabaseLog
class DatabaseLog implements Log{
public void writeLog(){
System.out.println(“数据库日志记录。”); } }
//客户端测试类
public class Client{
public static void main(String args[]){
try{Log log; LogFactory factory;
factory=(LogFactory)XMLUtil.getBean();
log=factory.createLog(); log.writeLog();
}catch(Exception e){
System.out.println(e.getMessage());
} } }
//配置文件
import javax.xml.parsers.; import org.w3c.dom.;
import org.xml.sax.SAXException; import java.io.*;
public class XMLUtil{
public static Object getBean(){
try{ DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File(“FactoryMethodconfig1.xml”));
NodeList nl = doc.getElementsByTagName(“className”);
Node classNode=nl.item(0).getFirstChild();
String cName=classNode.getNodeValue();
Class c=Class.forName(cName); Object obj=c.newInstance();
return obj;
}catch(Exception e){
e.printStackTrace(); return null;
} } }

3.需要提供抽象工厂模式实例的结构图(类图)和实现代码。
(1)类图实现:

(2)代码实现:
public class Client{
public static void main(String args[]) {
try{ HWFactory factory; CPU tv; RAM ac;
factory=(HWFactory)XMLUtil.getBean();
tv=factory.produceCPU(); tv.play();
ac=factory.produceRAM(); ac.changeRAM();
}catch(Exception e){
System.out.println(e.getMessage());
} } }
public interface CPU{ public void play(); }
public interface HWFactory{
public CPU produceCPU(); public RAM produceRAM(); }
public class MACCPU implements CPU{
public void play(){ System.out.println(“MACCPU运行中…”); } }
public class MacFactory implements HWFactory{
public CPU produceCPU(){ return new MACCPU(); }
public RAM produceRAM(){ return new MacRAM(); } }
public class MacRAM implements RAM{
public void changeRAM(){ System.out.println(“MACRAM改变中…”); } }
public class PCCPU implements CPU{
public void play(){ System.out.println(“PCCPU运行中…”); } }
public class PCFactory implements HWFactory{
public CPU produceCPU(){ return new PCCPU(); }
public RAM produceRAM(){ } }
public class PCRAM implements RAM{
public void changeRAM(){ System.out.println(“PCRAM改变中…”); } }
public interface RAM { public void changeRAM(); }
//配置文件
import javax.xml.parsers.; import org.w3c.dom.;
import org.xml.sax.SAXException; import java.io.*;
public class XMLUtil{
public static Object getBean() {
try{DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File(“AbstractFactoryconfig.xml”));
NodeList nl = doc.getElementsByTagName(“className”);
Node classNode=nl.item(0).getFirstChild();
String cName=classNode.getNodeValue();
Class c=Class.forName(cName); Object obj=c.newInstance();
return obj;
}catch(Exception e) {
e.printStackTrace(); return null; }
} }
4.需要提供单例模式实例的结构图(类图)和实现代码。
(1)类图实现:

(2)代码实现:
public class Client {
public static void main(String args[]){
new JInternalFrameDemo(); }}
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; } }

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() {
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”);
} } }
5.需要提供抽象工厂模式实例的结构图(类图)和实现代码。
(1)类图实现:

(2)代码实现:
//按钮接口:抽象产品
interface Button { public void display(); }
//Spring按钮类:具体产品 //Summer按钮类同
class SpringButton implements Button {
public void display() { System.out.println(“显示浅绿色按钮。”); } }
//文本框接口:抽象产品
interface TextField { public void display(); }
//Spring文本框类:具体产品 //Summer文本框类同
class SpringTextField implements TextField {
public void display() { System.out.println(“显示绿色边框文本框。”); } }
//组合框接口:抽象产品
interface ComboBox { public void display(); }
//Spring组合框类:具体产品 //Summer 组合框类同
class SpringComboBox implements ComboBox {
public void display() { System.out.println(“显示绿色边框组合框。”); } }
//界面皮肤工厂接口:抽象工厂
interface SkinFactory {
public Button createButton(); public TextField createTextField();
public ComboBox createComboBox(); }
//Spring皮肤工厂:具体工厂 //Summer皮肤工厂类同
class SpringSkinFactory implements SkinFactory {
public Button createButton() { return new SpringButton(); }
public TextField createTextField() { return new SpringTextField(); }
public ComboBox createComboBox() { return new SpringComboBox(); } }
//客户端测试类
class Client {public static void main(String args[]) {
SkinFactory factory; Button bt; TextField tf; ComboBox cb;
factory = (SkinFactory)XMLUtil.getBean();
bt = factory.createButton(); tf = factory.createTextField(); cb = factory.createComboBox();
bt.display(); tf.display(); cb.display(); } }
6.需要提供工厂方法模式实例的结构图(类图)和实现代码。
(1)类图实现:

(2)代码实现:
public class Client{
public static void main(String args[]){ try { XY tv; XYFactory factory;
factory=(XYFactory)XMLUtil.getBean(); tv=factory.produceTV(); tv.play();
}catch(Exception e) { System.out.println(e.getMessage()); } } }
public class HTTPConnection implements XY{
public void play(){ System.out.println(“HTTP连接中…”); } }
public class HTTPFactory implements XYFactory{
public XY produceTV(){ System.out.println(“HTTP提供HTTPConnection连接类”);
return new HTTPConnection(); } }
public class IMAPConnection implements XY{
public void play(){ System.out.println(“IMAP连接中…”); } }
public class IMAPFactory implements XYFactory{
public XY produceTV(){
System.out.println(“IMAP协议提供IMAPConnection连接类”);
return new IMAPConnection(); } }
public class POP3Connection implements XY{
public void play() { System.out.println(“POP3连接中…”); } }
public class POP3Factory implements XYFactory{
public XY produceTV() {
System.out.println(“POP3协议提供POP3Connection连接类”);
return new POP3Connection(); } }
import javax.xml.parsers.; import org.w3c.dom.;
import org.xml.sax.SAXException; import java.io.*;
public class XMLUtil{ public static Object getBean(){
Try{ DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder(); Document doc;
doc = builder.parse(new File(“AbstractFactoryconfig.xml”));
NodeList nl = doc.getElementsByTagName(“className”);
Node classNode=nl.item(0).getFirstChild();
String cName=classNode.getNodeValue();
Class c=Class.forName(cName); Object obj=c.newInstance(); return obj;}
catch(Exception e){ e.printStackTrace(); return null; } } }
public interface XY{ public void play(); }
public interface XYFactory{ public XY produceTV(); }
7.需要提供抽象工厂模式实例的结构图(类图)和实现代码。
(1)类图实现:

(2)代码实现:
public class Client{ public static void main(String args[]){ Try{ DBactory factory; Connection tv;
Statement ac; factory=(DBactory)XMLUtil.getBean();
tv=factory.createConnection(); tv.play();
ac=factory.createStatement(); ac.changeTemperature();
}catch(Exception e){ System.out.println(e.getMessage()); } } }
public interface Connection{ public void play(); }
public interface DBactory{
public Connection createConnection(); public Statement createStatement(); }
public class MYSQLConnection implements Connection{ // OracleConnection同
public void play(){ System.out.println(“MYSQL数据库连接中…”); } }
public class MYSQLFactory implements DBactory{ //OracleFactory同
public Connection createConnection(){ return new MYSQLConnection(); }
public Statement createStatement(){ return new MYSQLStatement(); } }
public class MYSQLStatement implements Statement{ // OracleStatement同
public void changeTemperature(){ System.out.println(“MYSQL数据库使用中…”); }}
public interface Statement { public void changeTemperature(); }
import javax.xml.parsers.; import org.w3c.dom.;
import org.xml.sax.SAXException; import java.io.*;
public class XMLUtil{ public static Object getBean(){
try{ DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc; doc = builder.parse(new File(“AbstractFactoryconfig.xml”));
NodeList nl = doc.getElementsByTagName(“className”);
Node classNode=nl.item(0).getFirstChild();
String cName=classNode.getNodeValue();
Class c=Class.forName(cName); Object obj=c.newInstance(); return obj;
}catch(Exception e){
e.printStackTrace(); return null; } } }

六、实验小结

1.熟练绘制常见的创建型设计模式结构图,理解每一种创建型设计模式的模式动机。
2.通过实例进一步掌握简单工厂模式、工厂方法模式、抽象工厂模式、单例模式的结构和特点,用过比较学习各模式的异同,学会如何在实际软件项目开发中合理使用相应的模式。
3.理解简单工厂模式中工厂类的作用和实现以及为何需要定义抽象产品类。
4.理解引入抽象工厂类的原因,工厂方法模式中多态性的体现以及客户代码的而编写,同时理解如何通过DOOM和Java反射机制来操作XML配置文件。
5.理解了抽象工厂模式中“开闭原则”的倾斜性,抽象工厂模式中“开闭原则”的倾斜性。
6.掌握单例模式的适用场景,在合适的情况下合理使用单例模式,避免因为不恰当的使用给系统带来负面影响。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000244.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号