UML与面向对象程序设计原则
面向对象程序设计中类与类的关系都有哪几种?分别用类图实例说明。
1. 继承关系
初中生类(B)继承小学生类(A)
2. 实现关系
指的是一个class类实现 interface接口(可以实现多个接口)的功能;实现是类与接口之间最常见的关系;
3. 依赖关系
司机使用汽车
4. 关联关系
被关联类B以类的属性形式出现在关联类A中,也可能是关联类A引用了一个类型为被关联类B的全局变量。
5. 聚合关系
聚合体现的是整体与部分的关系,此时整体与部分之间是可分离的,它们可以具有各自的生命周期,部分可以属于多个整体对象,也可以为多个整体对象共享。
6. 组合关系
体现整体与部分间的关系,但此时整体与部分是不可分的,整体的生命周期结束也就意味着部分的生命周期结束。
单一职责原则
登录模块在实际项目开发中很常见,请按照教材28页(PPT49页)利用单一职责原则重构后的类图实现这一模块。
public class DBUtil {
public static String url = "jdbc:mysql://localhost:3306/data?useUnicode=true&characterEncoding=utf8";
public static String user = "root";
public static String pass = "a123456";
public static Connection getConn () {
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, user, 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("连接失败");
}
}
}
public class MainClass {
public static void main(String[] args)
{
LoginForm loginForm=new LoginForm() ;
loginForm.display();
}
}
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);
password.setBounds(110, 130, 300, 25);
button.setBounds(315, 225, 90, 20);
jb.setBounds(95, 225, 90, 20);
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(){
public void actionPerformed(ActionEvent arg0) {
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.setLayout(null);
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, "用户名或密码错误");
}
}
}
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;
}
}
建表语句
CREATE TABLE user (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);
[实验任务三]:依赖倒转原则与合成复用原则
在一画图软件中提供了多种大小不同的画笔,并且可以给画笔指定不同的颜色,某设计人员对画笔进行了如上图所示的设计。通过分析,可以发现增加画笔的种类和颜色会使得系统中类的数目急剧增加,请根据合成复用原则和依赖倒转原则对上述设计进行重构。
源代码
public abstract class Color {
public abstract void colorPen();
}
public class GreenPen extends Color{
public void colorPen() {
System.out.println("绿色");
}
}
public class RedPen extends Color{
public void colorPen() {
System.out.println("红色");
}
}
public abstract class Size {
public abstract void sizePen();
}
public class SmallPen extends Size{
public void sizePen() {
System.out.println("小型");
}
}
public class MiddlePen extends Size{
public void sizePen() {
System.out.println("中型");
}
}
public class BigPen extends Size{
public void sizePen() {
System.out.println("大型");
}
}
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;
}
}
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());
}
}