实验1:UML与面向对象程序设计原则

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、掌握面向对象程序设计中类与类之间的关系以及对应的UML类图;

2、理解面向对象程序设计原则。

 

[实验任务一]:UML复习

阅读教材第一章复习UML,回答下述问题:

面向对象程序设计中类与类的关系都有哪几种?分别用类图实例说明。

4种:泛化,关联,依赖,实现

 

 

 

 

 

[实验任务二]:单一职责原则

登录模块在实际项目开发中很常见,请按照教材28页(PPT49页)利用单一职责原则重构后的类图实现这一模块。

实验要求:

1.提交源代码和对应的数据库文件(注意将此模块保存,以备以后使用);

2.注意编程规范。

DBUtil

package org.example;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class DBUtil {

 

    public static String db_url = "jdbc:mysql://localhost:3306/first?serverTimezone=GMT%2B8&useSSL=false";

    public static String db_user = "root";

    public static String db_pass = "wwsa20030207";

 

    public static Connection getConn () {

        Connection conn = null;

 

        try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            conn = DriverManager.getConnection(db_url, db_user, db_pass);

        } catch (Exception e) {

            e.printStackTrace();

        }

 

        return conn;

    }//end getConn

 

    public static void close (Statement state, Connection conn) {

        if (state != null) {

            try {

                state.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

 

        if (conn != null) {

            try {

                conn.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

 

    public static void close (ResultSet rs, Statement state, Connection conn) {

        if (rs != null) {

            try {

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

 

        if (state != null) {

            try {

                state.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

 

        if (conn != null) {

            try {

                conn.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

 

    public static void main(String[] args) throws SQLException {

        Connection conn = getConn();

        PreparedStatement pstmt = null;

        ResultSet rs = null;

        String sql ="select * from user";

        pstmt = conn.prepareStatement(sql);

        rs = pstmt.executeQuery();

        if(rs.next()){

            System.out.println("连接成功");

        }else{

            System.out.println("连接失败");

        }

    }

}

LoginForm

package org.example;

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class LoginForm extends JFrame {

 

    private static final long serialVersionUID = 1L;

    private  UserDao dao = new UserDao();

    //设置按钮组件

 

    private  JButton jb=new JButton("登录");    //添加一个登录按钮

    private JButton button=new JButton("重置");    //添加一个确定按钮

    //设置文本框组件

    private JTextField username = new JTextField();//用户名框

    private JPasswordField password = new JPasswordField();//密码框:为加密的***

 

    JLabel user_name=new JLabel("账号:");//设置左侧用户名文字

    JLabel pass_word=new JLabel("密码:");//设置左侧密码文字

 

    public void init()

    {

        /* 组件绝对位置  */

        user_name.setBounds(50, 70, 300, 25);

        pass_word.setBounds(50, 130, 200, 25);

 

        username.setBounds(110, 70, 300, 25);//设置用户名框的宽,高,x值,y

        password.setBounds(110, 130, 300, 25);//设置密码框的宽,高,x值,y

 

        button.setBounds(315, 225, 90, 20);//设置确定按钮的宽,高,x值,y

        jb.setBounds(95, 225, 90, 20);//设置确定按钮的宽,高,x值,y

 

 

        /* 组件透明化*/

        user_name.setOpaque(false);

        pass_word.setOpaque(false);

 

 

        //监听事件

        jb.addActionListener(new ActionListener(){        //为确定按钮添加监听事件

 

            @SuppressWarnings("deprecation")

            public void actionPerformed(ActionEvent arg0) {

                validate(username.getText().trim(),password.getText().trim());

            }

        });

 

 

        //重置按钮

        button.addActionListener(new ActionListener(){        //为重置按钮添加监听事件

            //同时清空namepassword的数据

            public void actionPerformed(ActionEvent arg0) {

                // TODO 自动生成方法存根

                username.setText("");

                password.setText("");

            }

        });

 

    }

 

    public void display()

    {

        JFrame f =new JFrame();

        f.setTitle("登录页面");

        //窗口退出行为

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗口大小不可变

        f.setResizable(false);

        //设置窗口打开居中

        f.setLocationRelativeTo(null);

        //窗口大小

        f.setSize(500, 300);

 

        init();

 

        //添加组件

        Container contentPanel= new Container();//添加一个contentPanel容器

        contentPanel.setLayout(null);//设置添加的contentPanel容器为流布局管理器

        contentPanel.add(user_name);

        contentPanel.add(pass_word);

        contentPanel.add(username);

        contentPanel.add(password);

        contentPanel.add(jb);

        contentPanel.add(button);

 

        f.add(contentPanel);

        //展示窗口

        f.setVisible(true);

    }

 

 

    public  void  validate(String username,String password)

    {

 

        if(username.trim().length()==0||password.trim().length()==0){

            JOptionPane.showMessageDialog(null, "用户名,密码不允许为空");

 

            return;

        }

 

        if(dao.findUser(username, password))

        {

 

            JOptionPane.showMessageDialog(null, "登录成功!");

 

 

        }else {

            JOptionPane.showMessageDialog(null, "用户名或密码错误");

 

        }

 

    }

 

}

MainClass

package org.example;

 

public class MainClass {

 

    public static void main(String[] args)

    {

        LoginForm loginForm=new LoginForm() ;    //调用

        loginForm.display();

 

    }

 

}

UserDao

package org.example;

 

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

public class UserDao {

    public boolean findUser(String username, String password) {

        //准备SQL语句

        String sql = "select * from user where username ='" + username + "'";

        Connection conn= DBUtil.getConn();

        //创建语句传输对象

        Statement state = null;

        ResultSet rs= null;

        int flag=0;

        String c_password=null;

        try {

            state = conn.createStatement();

            rs = state.executeQuery(sql);

            while(rs.next()) {

                ++flag;

                c_password=rs.getString("password");

            }    if (flag == 0) {

                return false;

            }

            if (!password.equals(c_password)) {   //判断密码

                return false;

            }

        }catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally {

            DBUtil.close(rs, state, conn);

        }

        return true;

    }

}

Sql文件

/*

 Navicat Premium Data Transfer

 

 Source Server         : localhost

 Source Server Type    : MySQL

 Source Server Version : 80031 (8.0.31)

 Source Host           : localhost:3306

 Source Schema         : first

 

 Target Server Type    : MySQL

 Target Server Version : 80031 (8.0.31)

 File Encoding         : 65001

 

 Date: 23/10/2023 09:14:58

*/

 

SET NAMES utf8mb4;

SET FOREIGN_KEY_CHECKS = 0;

 

-- ----------------------------

-- Table structure for user

-- ----------------------------

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user`  (

  `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,

  `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL

) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

 

-- ----------------------------

-- Records of user

-- ----------------------------

INSERT INTO `user` VALUES ('123zhang', '123456');

 

SET FOREIGN_KEY_CHECKS = 1;

 

 

[实验任务三]:依赖倒转原则与合成复用原则

在一画图软件中提供了多种大小不同的画笔,并且可以给画笔指定不同的颜色,某设计人员对画笔进行了如上图所示的设计。通过分析,可以发现增加画笔的种类和颜色会使得系统中类的数目急剧增加,请根据合成复用原则和依赖倒转原则对上述设计进行重构。

实验要求:

1. 提交源代码;

BigPen

package org.example.test2;

 

public class MianClass {

    public static void main(String[] args) {

//颜色大小随意组合

        Pen pen=new Pen();

//组合小型红色钢笔

        pen.setSize(new SmallPen());

        pen.setColor(new RedPen());

//组合大型绿色钢笔

        pen.setSize(new BigPen());

        pen.setColor(new GreenPen());

    }

}

Color

package org.example.test2;

 

public abstract class Color {

    public abstract void colorPen();

}

GreenPen

package org.example.test2;

 

public class GreenPen extends Color{

    public void colorPen() {

        System.out.println("绿色");

    }

}

MainClass

package org.example.test2;

 

public class MianClass {

    public static void main(String[] args) {

//颜色大小随意组合

        Pen pen=new Pen();

//组合小型红色钢笔

        pen.setSize(new SmallPen());

        pen.setColor(new RedPen());

//组合大型绿色钢笔

        pen.setSize(new BigPen());

        pen.setColor(new GreenPen());

    }

}

MiddlePen

package org.example.test2;

 

public class MiddlePen extends Size {

    //中型

    public void sizePen() {

        System.out.println("中型");

    }

}

Pen

package org.example.test2;

 

public class Pen {

    //钢笔

    private Size size;

    private Color color;

    public Size getSize() {

        return size;

    }

    public void setSize(Size size) {

        this.size = size;

    }

    public Color getColor() {

        return color;

    }

    public void setColor(Color color) {

        this.color = color;

    }

}

RedPen

package org.example.test2;

 

public class RedPen extends Color {

    public void colorPen() {

        System.out.println("红色");

    }

}

Size

package org.example.test2;

 

public abstract class Size {

    public abstract void sizePen();

}

SmallPen

package org.example.test2;

 

public class SmallPen extends Size {

    //小型

    public void sizePen() {

        System.out.println("小型");

    }

}

 

 

2. 画出重构后的类图。

posted @ 2023-11-06 08:48  会秃头的小白  阅读(18)  评论(0编辑  收藏  举报