You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从接口类访问其他类:如何实现患者仅查看自身血压且不篡改属性

Great question! This is a classic example of applying encapsulation and the principle of least privilege in object-oriented design—let's break down how to implement this cleanly for your nurse-patient system.

核心设计原则

The key here is to restrict data access strictly based on user role:

  • Nurses (like nurse1) get write/modify access to all patient attributes (blood pressure, measurement device, date) via your Interface class.
  • Patients (like patient1) only get read-only access to their own blood pressure, with zero ability to modify any attributes (their own or others').
具体类结构实现

Let’s walk through a concrete, language-agnostic implementation (using Java for code examples, but the logic translates to most OOP languages):

1. Patient Class (Encapsulated Core Data)

We’ll mark all patient attributes as private to block direct external access. Then we’ll expose only a read-only method for blood pressure, and restrict modification methods to only be accessible by nurse-facing classes:

public class Patient {
    // All attributes are private to enforce encapsulation
    private String patientId;
    private double bloodPressure;
    private String measurementDevice;
    private LocalDate measurementDate;

    // Constructor is restricted—only your Interface/Nurse classes can create patients
    Patient(String patientId) {
        this.patientId = patientId;
    }

    // Read-only access for the patient's own blood pressure (public so patients can call it)
    public double getBloodPressure() {
        return bloodPressure;
    }

    // Setters are package-private (only accessible to classes in the same package, like your Interface/Nurse)
    void setBloodPressure(double bloodPressure) {
        this.bloodPressure = bloodPressure;
    }

    void setMeasurementDevice(String measurementDevice) {
        this.measurementDevice = measurementDevice;
    }

    void setMeasurementDate(LocalDate measurementDate) {
        this.measurementDate = measurementDate;
    }

    // No getters for other attributes—patients can never access this data
}

2. Nurse Class

Nurses will use this class to interact with your system’s interface, creating patients and updating their attributes:

public class Nurse {
    private final String nurseId;
    private final PatientManagementInterface systemInterface;

    public Nurse(String nurseId, PatientManagementInterface systemInterface) {
        this.nurseId = nurseId;
        this.systemInterface = systemInterface;
    }

    // Nurse's method to assign all patient data
    public void assignPatientData(String patientId, double bp, String device, LocalDate date) {
        Patient targetPatient = systemInterface.getPatientById(patientId);
        if (targetPatient != null) {
            targetPatient.setBloodPressure(bp);
            targetPatient.setMeasurementDevice(device);
            targetPatient.setMeasurementDate(date);
        }
    }
}

3. PatientManagementInterface Class (Your Design's Entry Point)

This class acts as the single controlled entry point for all system interactions. It hides the internal Patient class implementation from patients entirely:

import java.util.HashMap;
import java.util.Map;

public class PatientManagementInterface {
    private final Map<String, Patient> patientRegistry = new HashMap<>();

    // Nurse creates a new patient object
    public Patient createPatient(String patientId) {
        Patient newPatient = new Patient(patientId);
        patientRegistry.put(patientId, newPatient);
        return newPatient;
    }

    // Nurse retrieves a patient to modify (package-private, so only Nurse class can call)
    Patient getPatientById(String patientId) {
        return patientRegistry.get(patientId);
    }

    // Patient-facing method to get their own blood pressure (requires valid patient ID)
    public double getOwnBloodPressure(String patientId) {
        Patient patient = patientRegistry.get(patientId);
        if (patient != null) {
            return patient.getBloodPressure();
        }
        throw new IllegalArgumentException("Patient ID not found");
    }
}
How This Enforces Your Requirements
  • Patient can only view their own blood pressure: Patients only have access to the getOwnBloodPressure method, which requires them to provide their own ID. They never receive a reference to the full Patient object, so they can’t access other attributes.
  • No tampering with attributes: All setter methods are restricted to nurse-accessible classes. Patients have no way to call these methods, so they can’t modify any data.
  • Nurse workflow alignment: Nurses use the interface to create patients and assign attributes exactly as your original design specifies.
Optional: Even Stricter Role Separation

If you want to eliminate any chance of patients accessing the full Patient class, create a read-only PatientView interface and return that to patients instead:

public interface PatientView {
    double getBloodPressure();
}

// Update Patient class to implement the interface
public class Patient implements PatientView {
    // ... existing code ...
}

// Add this method to PatientManagementInterface for patients:
public PatientView getPatientView(String patientId) {
    return patientRegistry.get(patientId);
}

Now patients only receive a minimal, read-only view of their data—no access to any underlying implementation details.

内容的提问来源于stack exchange,提问作者MarionaGlenni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:14:49