1

我有以下要求。

介绍

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

业务逻辑/UI 组件

当用户编辑报表时,其他用户不能编辑报表,只能查看。

它包含一个带有表格的页面,该表格用于监控锁定的报告以供查看。

挑战者

1)我应该如何实施这种“锁定”机制?2) 可以帮助我的设计模式和 API 是什么?

我目前的实现

我将有一个报告服务类它将包含所有已锁定报告的哈希图(带有用于锁定管理的用户信息)

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

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

对于锁定报表表的监控,我打算在报表服务类中实现观察者模式。对于每个用户/支持bean,它将“订阅”报告服务。

有什么输入吗????

4

1 回答 1

3

Answer is simple... We can manage this problem with 2 classes.

Features of each class is given below

ReportUtil:
(1) track whether any report is open in write mode
(2) create object of report based on access mode available

Report:
(1) open read only or writable report based on access given
(2) While closing, reset the flag in ReportUtil class, if the current report was open in write mode.

Client:
To test ReportUtil and Report classes.


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
    }

}

Output: Report open in Write mode Report open in Read only mode Report open in Read only mode Report open in Write mode


I don't know why we want to use Hash table here. May be I didn't understand your requirement. Also, I have used synchronized methods to escape from synchronization problems.

If your requirement was to track all the users who are using the report, please let me know.

Happy Learning!!!

于 2012-11-18T09:25:05.057 回答