java实现医疗管理系统,包含抽象工厂模式,单例模式,装饰模式,策略模式,观察者模式,并给出具体实现代码
时间: 2024-03-23 13:40:27 浏览: 19
好的,下面是Java实现医疗管理系统中应用抽象工厂模式、单例模式、装饰模式、策略模式、观察者模式的具体实现代码。
1. 抽象工厂模式
```
// 抽象工厂接口
public interface MedicalFactory {
Patient createPatient();
Doctor createDoctor();
MedicalRecord createMedicalRecord();
}
// 具体工厂类
public class MedicalFactoryImpl implements MedicalFactory {
@Override
public Patient createPatient() {
return new PatientImpl();
}
@Override
public Doctor createDoctor() {
return new DoctorImpl();
}
@Override
public MedicalRecord createMedicalRecord() {
return new MedicalRecordImpl();
}
}
// 抽象产品类
public interface Patient {
void showInfo();
}
// 具体产品类
public class PatientImpl implements Patient {
@Override
public void showInfo() {
System.out.println("This is a patient.");
}
}
// 抽象产品类
public interface Doctor {
void showInfo();
}
// 具体产品类
public class DoctorImpl implements Doctor {
@Override
public void showInfo() {
System.out.println("This is a doctor.");
}
}
// 抽象产品类
public interface MedicalRecord {
void showInfo();
}
// 具体产品类
public class MedicalRecordImpl implements MedicalRecord {
@Override
public void showInfo() {
System.out.println("This is a medical record.");
}
}
```
2. 单例模式
```
// 单例类
public class DBConnectionPool {
private static DBConnectionPool instance = null;
private DBConnectionPool() {
// 初始化数据库连接池
}
public static DBConnectionPool getInstance() {
if (instance == null) {
synchronized (DBConnectionPool.class) {
if (instance == null) {
instance = new DBConnectionPool();
}
}
}
return instance;
}
}
```
3. 装饰模式
```
// 抽象组件类
public interface PatientInfo {
void showInfo();
}
// 具体组件类
public class PatientInfoImpl implements PatientInfo {
@Override
public void showInfo() {
System.out.println("This is patient information.");
}
}
// 抽象装饰器类
public abstract class PatientInfoDecorator implements PatientInfo {
protected PatientInfo patientInfo;
public PatientInfoDecorator(PatientInfo patientInfo) {
this.patientInfo = patientInfo;
}
@Override
public void showInfo() {
patientInfo.showInfo();
}
}
// 具体装饰器类
public class AuthorizationDecorator extends PatientInfoDecorator {
public AuthorizationDecorator(PatientInfo patientInfo) {
super(patientInfo);
}
@Override
public void showInfo() {
patientInfo.showInfo();
System.out.println("This is authorization information.");
}
}
// 具体装饰器类
public class LogDecorator extends PatientInfoDecorator {
public LogDecorator(PatientInfo patientInfo) {
super(patientInfo);
}
@Override
public void showInfo() {
patientInfo.showInfo();
System.out.println("This is log information.");
}
}
```
4. 策略模式
```
// 抽象策略接口
public interface DBAccessStrategy {
void accessDB();
}
// 具体策略类
public class MySQLAccessStrategy implements DBAccessStrategy {
@Override
public void accessDB() {
// 使用MySQL访问数据库
}
}
// 具体策略类
public class OracleAccessStrategy implements DBAccessStrategy {
@Override
public void accessDB() {
// 使用Oracle访问数据库
}
}
// 上下文类
public class DBAccessContext {
private DBAccessStrategy strategy;
public DBAccessContext(DBAccessStrategy strategy) {
this.strategy = strategy;
}
public void setStrategy(DBAccessStrategy strategy) {
this.strategy = strategy;
}
public void accessDB() {
strategy.accessDB();
}
}
```
5. 观察者模式
```
// 抽象主题接口
public interface MedicalSubject {
void registerObserver(MedicalObserver observer);
void removeObserver(MedicalObserver observer);
void notifyObservers();
}
// 具体主题类
public class PatientInfoSubject implements MedicalSubject {
private List<MedicalObserver> observers;
private PatientInfo patientInfo;
public PatientInfoSubject() {
observers = new ArrayList<>();
}
public void setPatientInfo(PatientInfo patientInfo) {
this.patientInfo = patientInfo;
notifyObservers();
}
@Override
public void registerObserver(MedicalObserver observer) {
observers.add(observer);
}
@Override
public void removeObserver(MedicalObserver observer) {
observers.remove(observer);
}
@Override
public void notifyObservers() {
for (MedicalObserver observer : observers) {
observer.update(patientInfo);
}
}
}
// 抽象观察者接口
public interface MedicalObserver {
void update(PatientInfo patientInfo);
}
// 具体观察者类
public class PatientInfoObserver implements MedicalObserver {
@Override
public void update(PatientInfo patientInfo) {
System.out.println("Patient information has been updated.");
}
}
```