简单工厂模式

实验目的与任务

目的:熟悉UML的使用,熟悉简单工厂模式。

任务:按照实验内容要求,完成使用UML和java实现简单画图功能。

预习内容

复习UML课程的内容,熟悉简单工厂模式的使用。

实验内容及要求

  • UML回顾: (推荐使用MyEclipse10以上版本)

1、请用UML简单画出泛化关系、关联关系、聚合关系、合成关系、依赖关系、实现关系,并给出相关代码。

  • 完成:
    1. 有如下场景:现有一绘图工具,能够支持圆形、三角形、正方形等的绘图,每个图形都有绘图和擦除两个方法,请结合简单工厂模式实现该工具。

 

实验结果(可续页)

  1. 泛化关系

public class A{

}

public class B extends A{

}

 

  1. 关联关系

public class School{

    public Student stu;

}

public class Student{

}

 

  1. 聚合关系

public class Car{

public Engine engine;

public Wheel wheel;

}

public class Engine{

}

public class Wheel{

}

 

  1. 合成关系

public class Head 

    private Mouth mouth; 

    public Head() 

    { 

    mouth = new Mouth(); 

    } 

    …… 

 

public class Mouth 

    …… 

 

  1. 依赖关系

public class Driver 

    public void drive(Car car) 

    { 

        car.move(); 

    } 

    …… 

public class Car 

    public void move() 

    { 

        ...... 

    } 

    …… 

}  

 

  1. 实现关系

public interface Vehicle  

    public void move(); 

public class Ship implements Vehicle 

    public void move()  

    { 

    …… 

    } 

 

 

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();

    }

}

 

 

思考题:

  1. 简单工厂模式就是不使用new来进行对象的实例化,为何代码中还是会出现new操作?

简单工厂模式是用一个工厂类去创建新的对象,并返回。使用new的操作是需要生成对象的实例

 

  1. 请绘制实验内容2中的类图,如果新增一矩形的绘图方式,如何影响源代码,试分析是否符合“开-闭”原则?

 

 

遵循开闭原则设计出的模块具有两个主要特征:

(1)对于扩展是开放的。这意味着模块的行为是可以扩展的。当应用的需求改变时,我们可以对模块进行扩展,使其具有满足那些改变的新行为。也就是说,我们可以改变模块的功能。

(2)对于修改是关闭的。对模块行为进行扩展时,不必改动模块的源代码或者二进制代码。模块的二进制可执行版本,无论是可链接的库、DLL或者.EXE文件,都无需改动。

软件系统是否有良好的接口(抽象)设计是判断软件系统是否满足开闭原则的一种重要的判断基准,

当新增矩形绘图方式时,需要改动模块的源代码,所以不符合开闭原则

 

 

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值