Java设计:锁定和监视报告



我有以下要求。

简介

该系统是报告/内容管理系统。它允许用户对报告进行CRUD操作。

业务逻辑/UI组件

用户正在编辑报告时,其他用户不能编辑报告,而仅查看。

它包含一个带有表的页面,该页面监视锁定的报告以查看。

挑战者

1)我应该如何实现此"锁定"机制?2)哪些设计模式和API可以帮助我?

我当前的实施

我将有一个报告服务课它将包含所有已锁定报告的标志性(包含用于锁定管理的用户信息)

我已经完成了SCJD,并且正在考虑使用我的锁定机制,但是我意识到我不需要等待"锁定"。

我担心的唯一问题是"锁定"报告(将锁添加到地图中)时,我相信可以通过同步轻松解决。

对于监视锁定报告表的监视,我计划在报告服务类中实现观察者模式。对于每个用户/支持Bean,它将"订阅"报告服务。

有任何输入吗?????

答案很简单...我们可以用2个类来管理此问题。

下面给出了每个类的功能

reportutil:
(1)跟踪是否以写入模式打开任何报告
(2)根据可用访问模式创建报告对象

报告:
(1)仅根据给定的访问
开放阅读或可写的报告(2)关闭时,如果当前报告以写入模式打开。

客户端:
测试Reportutil和报告课程。


import java.util.LinkedList;
public class ReportUtil {
    private static boolean bIsWriteLockAvaialable = true;
    public static synchronized Report getReport() {
        Report reportObj = new Report(bIsWriteLockAvaialable);
        if(true == bIsWriteLockAvaialable) {
            bIsWriteLockAvaialable = false;
        }
        return reportObj;
    }   
    public static void resetLock() {
        bIsWriteLockAvaialable = true;
    }
}

public class Report {
    private boolean bICanWrite = false;
    public Report(boolean WriteAccess) {
        bICanWrite = WriteAccess;
    }
    public void open() {
        if(bICanWrite == true) {
            //Open in write mode
            System.out.println("Report open in Write mode");
        }
        else {
            //Open in readonly mode
            System.out.println("Report open in Read only mode");
        }
    }
    public synchronized void close() {
        if(bICanWrite == true) {
            ReportUtil.resetLock();
        }
    }
}

public class Client {
    public static void main(String[] args) {
        Report report1 = ReportUtil.getReport();
        report1.open(); //First time open in writable mode
        Report report2 = ReportUtil.getReport();
        report2.open(); //Opens in readonly mode
        Report report3 = ReportUtil.getReport();
        report3.open(); //Opens in readonly mode
        report1.close(); //close the write mode
        Report report4 = ReportUtil.getReport();
        report4.open(); //Opens in writable mode since the first writeable report was closed
    }
}

输出:在写模式下报告打开在仅阅读模式下报告打开在仅阅读模式下报告打开在写模式下报告打开


我不知道为什么我们要在这里使用哈希表。可能我不明白你的要求。另外,我已经使用了同步方法来摆脱同步问题。

如果您的要求是跟踪所有使用报告的用户,请告诉我。

快乐学习!

相关内容

最新更新