实验目的与任务 目的:熟悉UML的使用,熟悉简单工厂模式。 任务:按照实验内容要求,完成使用UML和java实现简单画图功能。 | ||||||
预习内容 复习UML课程的内容,熟悉简单工厂模式的使用。 | ||||||
实验内容及要求
1、请用UML简单画出泛化关系、关联关系、聚合关系、合成关系、依赖关系、实现关系,并给出相关代码。
| ||||||
实验结果(可续页)
1.创建界面 import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Stage extends JFrame{ private int x = 100, y = 100; private int width =600, height =500; private static int drawWdith = 800; private static int drawHeight = 560; public static int offsetX = 0; public static int offsetY = 0; public static int shapeW = 100; public static int shapeH = 100;
private Graphics g; private Color color = Color.red; private JPanel controlPanel; private JPanel drawPanel; private JButton cirBtn; private JButton triBtn; private JButton squBtn; private JButton clearBtn;
public Stage() { Container con = getContentPane(); setBounds(x, y, width, height); setVisible(true); con.setBackground(Color.WHITE); setLayout(null); setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("绘制图形"); controlPanel = new JPanel(); controlPanel.setLocation(0, 0); controlPanel.setSize(800,40); //设置按钮 cirBtn = new JButton("圆形"); controlPanel.add(cirBtn); cirBtn.addActionListener(listener); triBtn = new JButton("三角形"); triBtn.addActionListener(listener); controlPanel.add(triBtn); squBtn = new JButton("正方形"); squBtn.addActionListener(listener); controlPanel.add(squBtn); clearBtn = new JButton("清除"); clearBtn.addActionListener(listener); controlPanel.add(clearBtn); this.add(controlPanel);
drawPanel = new JPanel(); drawPanel.setLocation(0, 40); drawPanel.setSize(800, 560); drawPanel.setBackground(Color.white); this.add(drawPanel);
//设置绘图 g = drawPanel.getGraphics(); g.setColor(color); }
//按钮点击事件 private ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("清除")) { Clear(); } else { Shape shape = ShapeFactory.CreateShape(e.getActionCommand()); shape.draw(g); } } };
/** * 清除画布 */ public void Clear() { g.setColor(Color.WHITE); g.fillRect(0, 0, drawWdith, drawHeight); g.setColor(color); offsetX = 0; offsetY = 0; }
public static void setOffset() { if(offsetX + shapeW < drawWdith) { offsetX += shapeW; } else { offsetX = 0; offsetY += shapeH; } }
}
2.创建类Shape,存储图形的坐标 import java.awt.*;
/** * Created by Administrator on 2019/3/6. */ public class Shape { protected int x; protected int y; protected int width; protected int height;
public Shape() { x = Stage.offsetX; y = Stage.offsetY; width = Stage.shapeW; height = Stage.shapeH; }
public void draw(Graphics g) { Stage.setOffset(); } }
圆形 import java.awt.*; public class Circle extends Shape { public void draw(Graphics g) { g.drawOval(x, y, width, height); super.draw(g); } }
三角形 import java.awt.*; public class Triangle extends Shape {
public void draw(Graphics g) { int[] px = {x + width/2, x, x + width}; int[] py = {y, y + height, y + height}; g.drawPolygon(px, py, px.length); super.draw(g); } }
正方形 import java.awt.*; public class Squral extends Shape { public void draw(Graphics g) { g.drawRect(x, y, width, height); super.draw(g); } }
3. 创建制造图形的工厂 public class ShapeFactory { public static Shape CreateShape(String name) { Shape shape; if(name.equals("圆形")) shape = new Circle(); else if(name.equals("三角形")) shape = new Triangle(); else shape = new Squral(); return shape; } }
4. main函数 public class Client { public static void main(String[] args) { Stage stage = new Stage(); } }
思考题:
简单工厂模式是用一个工厂类去创建新的对象,并返回。使用new的操作是需要生成对象的实例
遵循开闭原则设计出的模块具有两个主要特征: (1)对于扩展是开放的。这意味着模块的行为是可以扩展的。当应用的需求改变时,我们可以对模块进行扩展,使其具有满足那些改变的新行为。也就是说,我们可以改变模块的功能。 (2)对于修改是关闭的。对模块行为进行扩展时,不必改动模块的源代码或者二进制代码。模块的二进制可执行版本,无论是可链接的库、DLL或者.EXE文件,都无需改动。 软件系统是否有良好的接口(抽象)设计是判断软件系统是否满足开闭原则的一种重要的判断基准, 当新增矩形绘图方式时,需要改动模块的源代码,所以不符合开闭原则
|
简单工厂模式
最新推荐文章于 2021-02-28 17:11:28 发布