用Java编写一个三种设计模式一个体系结构的简单程序
时间: 2023-08-31 15:06:53 浏览: 93
设计模式java简单实例
好的,下面是一个使用Java编写的简单程序,它包含三种设计模式和一个体系结构:
体系结构:MVC(模型-视图-控制器)
设计模式:
1. 工厂模式(Factory Pattern)
2. 观察者模式(Observer Pattern)
3. 单例模式(Singleton Pattern)
程序功能:实现一个简单的学生信息管理系统,包括添加学生、删除学生、修改学生信息、查询学生信息等功能。
代码如下:
1. 学生类(Model)
```java
public class Student {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
// getter and setter methods
// ...
}
```
2. 学生管理类(Controller)
```java
import java.util.ArrayList;
import java.util.List;
public class StudentManager {
private List<Student> studentList;
public StudentManager() {
studentList = new ArrayList<>();
}
public void addStudent(Student student) {
studentList.add(student);
notifyObservers();
}
public void removeStudent(Student student) {
studentList.remove(student);
notifyObservers();
}
public void updateStudent(Student student) {
// update student info
notifyObservers();
}
public List<Student> getStudentList() {
return studentList;
}
// observer pattern
private List<Observer> observers = new ArrayList<>();
public void attach(Observer observer) {
observers.add(observer);
}
public void detach(Observer observer) {
observers.remove(observer);
}
public void notifyObservers() {
for (Observer observer : observers) {
observer.update();
}
}
}
```
3. 学生信息显示类(View)
```java
public class StudentView implements Observer {
private StudentManager studentManager;
public StudentView(StudentManager studentManager) {
this.studentManager = studentManager;
studentManager.attach(this);
}
public void display() {
List<Student> studentList = studentManager.getStudentList();
// display student info
// ...
}
@Override
public void update() {
display();
}
}
```
4. 学生信息输入类(View)
```java
import java.util.Scanner;
public class StudentInputView {
private StudentManager studentManager;
public StudentInputView(StudentManager studentManager) {
this.studentManager = studentManager;
}
public void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Input name: ");
String name = scanner.nextLine();
System.out.print("Input age: ");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("Input gender: ");
String gender = scanner.nextLine();
Student student = StudentFactory.createStudent(name, age, gender);
studentManager.addStudent(student);
}
}
```
5. 学生工厂类(Factory)
```java
public class StudentFactory {
public static Student createStudent(String name, int age, String gender) {
return new Student(name, age, gender);
}
}
```
6. 主程序
```java
public class Main {
public static void main(String[] args) {
StudentManager studentManager = new StudentManager();
StudentView studentView = new StudentView(studentManager);
StudentInputView studentInputView = new StudentInputView(studentManager);
// test
studentInputView.input();
studentInputView.input();
studentInputView.input();
// ...
}
}
```
以上代码中的设计模式分别是:
1. 工厂模式:StudentFactory类用于创建学生对象,通过工厂模式可以实现对象的创建与使用的分离。
2. 观察者模式:StudentManager类是被观察者,StudentView类是观察者,通过观察者模式可以实现学生信息的显示与学生信息的修改、添加、删除之间的解耦。
3. 单例模式:在以上代码中没有显式地使用单例模式,但是可以将StudentManager类设计为单例模式,以确保在整个程序中只有一个StudentManager对象,从而避免出现多个对象对同一份数据进行修改的情况。
阅读全文