简体   繁体   English

Java设计:锁定和监视报告

[英]Java Design: Locking and Monitor reports

I have the following requirement. 我有以下要求。

Introduction 介绍

The system is report/content management system. 该系统是报告/内容管理系统。 It allows user to do CRUD operations on reports. 它允许用户对报告执行CRUD操作。

Business Logic/UI Component 业务逻辑/ UI组件

When a user is editing a report, other users can't edit the report but view only. 当用户编辑报表时,其他用户无法编辑报表,只能查看报表。

It contains a page with a table that monitor the locked report for view. 它包含一个页面,其中包含一个监视锁定报告以供查看的表。

Challenger 挑战者

1) How should I implement this "locking" Mechanism ? 1)我应该如何实现这种“锁定”机制? 2) What are the design pattern and API that will assist me? 2)有哪些设计模式和API可以帮助我?

My current implementation 我目前的实施

I will have a report service class It will contain a hashmap of all the reports that are locked(with information of user for lock management) 我将有一个报告服务类它将包含所有被锁定的报告的哈希映射(包含锁定管理的用户信息)

I have completed SCJD and was considering using my lock mechanism, but I realize I do not need the wait "locking". 我已经完成了SCJD并正在考虑使用我的锁机制,但我意识到我不需要等待“锁定”。

The only problem I worried is concurrency issue when "locking" the report(adding the lock into map), I believe that can be easily solve by using synchronization. 我担心的唯一问题是“锁定”报告时的并发问题(将锁添加到地图中),我相信可以通过使用同步轻松解决。

For the monitoring of locked report table, I plan to implement observer pattern in the report service class. 为了监视锁定的报告表,我计划在报告服务类中实现观察者模式。 For each user/backingbean it will "subscribe" to the report service. 对于每个用户/后备,它将“订阅”报表服务。

Any inputs? 有什么投入? ???? ????

Answer is simple... We can manage this problem with 2 classes. 答案很简单......我们可以用2个班来管理这个问题。

Features of each class is given below 每节课的特点如下

ReportUtil: ReportUtil:
(1) track whether any report is open in write mode (1)跟踪是否在写入模式下打开任何报告
(2) create object of report based on access mode available (2)根据可用的访问模式创建报告对象

Report: 报告:
(1) open read only or writable report based on access given (1)根据给定的访问权限打开只读或可写报告
(2) While closing, reset the flag in ReportUtil class, if the current report was open in write mode. (2)关闭时,如果当前报告在写入模式下打开,则重置ReportUtil类中的标志。

Client: 客户:
To test ReportUtil and Report classes. 测试ReportUtil和Report类。


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!!! 快乐学习!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM