用常用的六种设计模式写一个学生信息管理系统源代码

时间: 2023-07-01 18:12:53 浏览: 52
好的,以下是一个简单的学生信息管理系统的源代码,使用了六种常用的设计模式: 1. 单例模式(Singleton):确保系统中只有一个学生信息管理系统实例。 2. 工厂模式(Factory):根据用户选择的操作类型,创建对应的操作对象。 3. 策略模式(Strategy):将不同的操作封装成不同的策略类,通过选择不同的策略类来执行对应的操作。 4. 装饰器模式(Decorator):在基本操作类的基础上,添加一些额外的功能(如日志记录、异常处理等)。 5. 观察者模式(Observer):当学生信息发生变化时,通知所有观察者进行更新。 6. 迭代器模式(Iterator):通过迭代器模式来遍历学生信息列表。 ```java // 单例模式 public class StudentInfoManager { private static StudentInfoManager instance = null; private List<Student> studentList; private StudentInfoManager() { studentList = new ArrayList<>(); } public static StudentInfoManager getInstance() { if (instance == null) { instance = new StudentInfoManager(); } return instance; } // 工厂模式 public Operation createOperation(String type) { switch (type) { case "add": return new AddOperation(); case "delete": return new DeleteOperation(); case "update": return new UpdateOperation(); case "query": return new QueryOperation(); default: return null; } } public void addStudent(Student s) { studentList.add(s); notifyObservers(); } public void deleteStudent(Student s) { studentList.remove(s); notifyObservers(); } public void updateStudent(Student s) { for (int i = 0; i < studentList.size(); i++) { if (studentList.get(i).getId() == s.getId()) { studentList.set(i, s); notifyObservers(); return; } } } public List<Student> queryStudent() { return studentList; } // 装饰器模式 public void addStudentWithLog(Student s) { Operation operation = createOperation("add"); operation = new LogDecorator(operation); operation.execute(s); } public void deleteStudentWithException(Student s) throws Exception { Operation operation = createOperation("delete"); operation = new ExceptionDecorator(operation); operation.execute(s); } // 观察者模式 private List<Observer> observerList = new ArrayList<>(); public void addObserver(Observer observer) { observerList.add(observer); } public void removeObserver(Observer observer) { observerList.remove(observer); } public void notifyObservers() { for (Observer observer : observerList) { observer.update(studentList); } } // 迭代器模式 public Iterator<Student> createIterator() { return new StudentIterator(studentList); } } // 策略模式 interface Operation { void execute(Student s); } class AddOperation implements Operation { public void execute(Student s) { StudentInfoManager.getInstance().addStudent(s); } } class DeleteOperation implements Operation { public void execute(Student s) { StudentInfoManager.getInstance().deleteStudent(s); } } class UpdateOperation implements Operation { public void execute(Student s) { StudentInfoManager.getInstance().updateStudent(s); } } class QueryOperation implements Operation { public void execute(Student s) { StudentInfoManager.getInstance().queryStudent(); } } // 装饰模式 abstract class OperationDecorator implements Operation { protected Operation operation; public OperationDecorator(Operation operation) { this.operation = operation; } public void execute(Student s) { operation.execute(s); } } class LogDecorator extends OperationDecorator { public LogDecorator(Operation operation) { super(operation); } public void execute(Student s) { System.out.println("add student: " + s); operation.execute(s); } } class ExceptionDecorator extends OperationDecorator { public ExceptionDecorator(Operation operation) { super(operation); } public void execute(Student s) throws Exception { if (s == null) { throw new Exception("student is null"); } operation.execute(s); } } // 观察者模式 interface Observer { void update(List<Student> studentList); } class StudentListObserver implements Observer { public void update(List<Student> studentList) { System.out.println("student list updated: " + studentList); } } // 迭代器模式 interface Iterator<T> { boolean hasNext(); T next(); } class StudentIterator implements Iterator<Student> { private List<Student> studentList; private int index; public StudentIterator(List<Student> studentList) { this.studentList = studentList; index = 0; } public boolean hasNext() { return index < studentList.size(); } public Student next() { return studentList.get(index++); } } // 学生信息类 class Student { private int id; private String name; private String gender; public Student(int id, String name, String gender) { this.id = id; this.name = name; this.gender = gender; } public int getId() { return id; } public String getName() { return name; } public String getGender() { return gender; } public String toString() { return "id: " + id + ", name: " + name + ", gender: " + gender; } } ```

相关推荐

最新推荐

recommend-type

学生选课管理信息系统课程设计与实现

本人结合学生选课管理的实际需要,完成了对学生选课管理系统的需求分析、功能模块划分、数据库模式分析等,并由此设计了后台数据库以及前台应用程序。 文中首先对课题背景,数据库基础理论和Access进行了简单的讨论...
recommend-type

基于BS模式的学生信息管理系统(软件体系结构)

基于BS模式的学生信息管理系统(软件体系结构)
recommend-type

基于C/S结构的小型餐饮管理系统设计与实现

为使酒店管理(信息工程)专业的学生掌握设计和实现旅游信息管理系统的方法,对酒店餐饮管理工作的业务流程进行了分析,并在此基础上设计了可供学生在管理信息系统设计课程中使用的餐饮管理系统设计方案。通过教学...
recommend-type

学生信息管理系统需求分析

生管理工作是一个系统工程,贯穿于学生在校学习的整个过程和各个方面。从新 生入学开始,到毕业离校,包括学生学籍管理,学生成绩管理,学生在校期间的 奖惩情况管理等各个方面,具有工作量大、分类细、项目多和覆盖...
recommend-type

学生信息管理系统毕业设计论文

本论文从学校需求分析入手,对毕业设计软件的设计思想和程序实现的过程作出了一个比较系统的分析和讨论。在此部分中,我担任了学生成绩管理模块的设计工作。并和其他同学一起进行了比较细致的数据库建立和模块的划分...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。