如何处理GlazedLists对共享发布者和锁定的PluggableList要求 [英] How to deal with GlazedLists's PluggableList requirement for shared publisher and lock

查看:109
本文介绍了如何处理GlazedLists对共享发布者和锁定的PluggableList要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在Java项目中使用GlazedLists,该项目广泛使用bean绑定(MVVM模式).

I have just started using GlazedLists in a Java project which uses beansbinding extensively (MVVM pattern).

PluggableList允许我将源列表绑定到表,然后在运行时更改源列表.为了做到这一点,每个源列表必须共享相同的ListEventPublisher和ReadWriteLock,因为PluggableList必须与其源共享一个锁和plublisher.我可以通过创建一个静态发布者并将其锁定在拥有潜在源列表的类中来完成此操作,并使用这些静态值在该类的每个实例以及PluggableList中创建列表,如下面的伪代码所示:

PluggableList is allowing me to bind a source list to a table, and then change the source list at runtime. In order to make this happen every source list must share the same ListEventPublisher and ReadWriteLock, since PluggableList must share a lock and plublisher with it's source. I accomplish this by creating a static publisher and lock in my class that owns the potential source lists, use those static values to create the list in every instantiation of the class as well as the PluggableList, as shown in the pseudo code below:

public class ModelClass
{
    final static EventList          LIST               = new BasicEventList();
    final static ListEventPublisher LISTEVENTPUBLISHER = LIST.getPublisher();
    final static ReadWriteLock      READWRITELOCK      = LIST.getReadWriteLock();

    final EventList                 sourceList         = 
            new BasicEventList(LISTEVENTPUBLISHER, READWRITELOCK);
}


public class UiControllerClass
{
    final PluggableList pluggableList = 
        new PluggableList(ModelClass.LISTEVENTPUBLISHER, ModelClass.READWRITELOCK);

    // ... call pluggableList.setSource(someSourceList) 
}

我对此有两个担忧:

(1)由于UiController中组件的特定要求,我必须在模型中做出决定.这似乎违反了MVVM模式.

(1) I have to make a decision in the Model because of a specific requirement of a component in the UiController. This seems to violate the MVVM pattern.

(2)如果列表太多且经常访问,则共享锁可能会影响列表的性能,因为它们都共享相同的锁.否则,这些列表中的每一个都应该能够独立运行,而不必彼此关心.

(2) The shared lock potentially impacts the performance of the lists if there are very many and they are accessed frequently, since they all share the same lock. Each of these lists should otherwise be able to operate independently without caring about each other.

我做错了吗?有没有一种更好的方法可以使PluggableLists工作,而ModelClass不必了解特殊的UiControllerClass要求,也可以避免潜在的性能损失?

Am I going about this incorrectly? Is there a better way to make PluggableLists work without the ModelClass having to know about a special UiControllerClass requirement and without the potential performance hit?

推荐答案

我想出了一个优雅的解决方案,该解决方案保留了MVVM模式并消除了对共享锁和发布者的需要.

I came up with an elegant solution that preserves the MVVM pattern as well as eliminates the need for a shared lock and publisher.

我创建了一个自定义列表转换​​,该转换扩展了PluggableList并覆盖了它的setSource方法.然后,新的源列表将与由PluggableList创建的新列表同步(它将具有与PluggableList相同的发布者并锁定).

I created a custom list transformation that extends PluggableList and overrides it's setSource method. The new source list is then synchronized with a new list created by the PluggableList (it will have the same publisher and lock as the PluggableList).

public class HotSwappablePluggableList<T>
        extends PluggableList<T>
{
    private EventList<T>         syncSourceList    = new BasicEventList<>();
    private ListEventListener<T> listEventListener = null;

    public HotSwappablePluggableList()
    {
        super(new BasicEventList<T>());
    }

    @Override
    public void setSource(final EventList<T> sourceList)
    {
        getReadWriteLock().writeLock().lock();
        try
        {
            if (listEventListener != null)
            {
                syncSourceList.removeListEventListener(listEventListener);
            }

            syncSourceList = sourceList;

            final EventList<T> syncTargetList = createSourceList();
            listEventListener = GlazedLists.syncEventListToList(syncSourceList, syncTargetList);

            super.setSource(syncTargetList);
        }
        finally
        {
            getReadWriteLock().writeLock().unlock();
        }
    }
}

这篇关于如何处理GlazedLists对共享发布者和锁定的PluggableList要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆