将第3题修改为4层架构,写出对应代码
时间: 2024-12-24 16:18:41 浏览: 3
要将第3题中的功能修改为4层架构(表示层、业务逻辑层、数据访问层和实体层),可以按照以下步骤进行设计和实现:
### 1. 实体层 (Entity Layer)
定义出版社的实体类 `Publisher`。
```java
public class Publisher {
private int id;
private String name;
private String postalCode;
private String address;
private String phoneNumber;
// 构造函数
public Publisher() {}
public Publisher(int id, String name, String postalCode, String address, String phoneNumber) {
this.id = id;
this.name = name;
this.postalCode = postalCode;
this.address = address;
this.phoneNumber = phoneNumber;
}
// Getter 和 Setter 方法
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getPostalCode() { return postalCode; }
public void setPostalCode(String postalCode) { this.postalCode = postalCode; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
public String getPhoneNumber() { return phoneNumber; }
public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; }
}
```
### 2. 数据访问层 (Data Access Layer)
定义数据访问接口 `PublisherDAO` 和其实现类 `PublisherDAOImpl`。
```java
import java.util.List;
public interface PublisherDAO {
boolean deletePublisher(int id);
List<Publisher> getAllPublishers();
}
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class PublisherDAOImpl implements PublisherDAO {
private Connection getConnection() throws SQLException {
// 假设使用 JDBC 连接数据库
String url = "jdbc:mysql://localhost:3306/publisher_management";
String user = "root";
String password = "password";
return DriverManager.getConnection(url, user, password);
}
@Override
public boolean deletePublisher(int id) {
try (Connection conn = getConnection()) {
String sql = "DELETE FROM publisher WHERE id = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
int rowsAffected = stmt.executeUpdate();
return rowsAffected > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
@Override
public List<Publisher> getAllPublishers() {
List<Publisher> publishers = new ArrayList<>();
try (Connection conn = getConnection()) {
String sql = "SELECT * FROM publisher";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Publisher publisher = new Publisher(
rs.getInt("id"),
rs.getString("name"),
rs.getString("postal_code"),
rs.getString("address"),
rs.getString("phone_number")
);
publishers.add(publisher);
}
} catch (SQLException e) {
e.printStackTrace();
}
return publishers;
}
}
```
### 3. 业务逻辑层 (Business Logic Layer)
定义业务逻辑类 `PublisherService`。
```java
public class PublisherService {
private PublisherDAO publisherDAO;
public PublisherService(PublisherDAO publisherDAO) {
this.publisherDAO = publisherDAO;
}
public boolean deletePublisher(int id) {
return publisherDAO.deletePublisher(id);
}
public List<Publisher> getAllPublishers() {
return publisherDAO.getAllPublishers();
}
}
```
### 4. 表示层 (Presentation Layer)
定义图形界面类 `DeletePublisherUI`。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DeletePublisherUI extends JFrame {
private JTextField idField;
private JButton deleteButton;
private JLabel resultLabel;
private PublisherService publisherService;
public DeletePublisherUI(PublisherService publisherService) {
this.publisherService = publisherService;
setTitle("删除出版社");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
panel.add(new JLabel("出版社编号:"));
idField = new JTextField();
panel.add(idField);
deleteButton = new JButton("删除");
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
if (publisherService.deletePublisher(id)) {
resultLabel.setText("删除成功!");
} else {
resultLabel.setText("删除失败!");
}
}
});
panel.add(deleteButton);
resultLabel = new JLabel("");
panel.add(resultLabel);
add(panel);
setVisible(true);
}
public static void main(String[] args) {
PublisherDAO publisherDAO = new PublisherDAOImpl();
PublisherService publisherService = new PublisherService(publisherDAO);
new DeletePublisherUI(publisherService);
}
}
```
以上代码实现了将第3题的功能修改为4层架构的设计。每一层都有明确的责任分工,使得代码更加模块化和易于维护。
阅读全文