集合作为装饰器:伪代码实现提议 [英] Collections as decorators: pseudo-code implementation proposal

查看:205
本文介绍了集合作为装饰器:伪代码实现提议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同时等待问题的答案我想讨论可能的实施计划/细节,或者一般来说甚至回答如何实施以下内容以及需要哪些工具/技术:

Meanwhile waiting for the answer on the question I would like to discuss the possible implementation plan/details or in general even answer how hard would be to implement the following and which tools/techniques are necessary for that:

(摘自提及的问题):

(excerpt from the referred question):


假设您需要实现许多
(子)类型的集合。
方面之一是存储相关列表
数组等,而另一个是
与行为相关已排序仅移除
,可见的

Suppose you need to implement many (sub)types of collections. One of the aspects is storage-related: list, array etc, while the other is behavior-related: ordered, remove only, observable (the one that fires an event upon every change) etc.

显然(再一次),需求
直接映射到知名的
Decorator 设计模式,其中与存储相关的
方面
列表数组 b $ b 行为有序可观察等)。

Obviously (again), the requirement maps directly to the well-known Decorator design pattern, where the storage-related aspect (list, array) will be decorated by multiple behavioral (ordered, observable etc).

到目前为止,我想提出一些(类似Java的)伪代码中的一些合理的实现,同时询问是否可以在Java或C#中实现下面的代码,如果没有,那么在任何其他现代编程语言:

So far, I would like to propose some reasonably short implementation in (Java-like) pseudo-code, while asking if it's possible to implement the following in Java or C# or, if not, then in any other modern programming language:

每个集合的基本接口必须支持:

Basic interface every collection must support:

interface Collection {
    [mutator]
    public void add(object o);

    [mutator]
    public void remove(object o);

    [accessor]
    public object get(int i);
}

存储方面:

实施:

class List : Collection {
    [mutator]
    public void add(object o) { ... }

    [mutator]
    public void remove(object o) { ... }

    [accessor]
    public object get(int i) { ... }
}

实现:

class Array : Collection {
    [mutator]
    public void add(object o) { ... }

    [mutator]
    public void remove(object o) { ... }

    [accessor]
    public object get(int i) { ... }
}

行为方面:



Thread-safe decorator:

typename<T> : where T is Collection
class ThreadSafe : Collection {
    private T m_source;
    private object m_lock = new object();

    [mutator]
    public void add(object o) { 
        using (m_lock) {
            m_source.add();
        }
    }

    [mutator]
    public void remove(object o) { ... }

    [accessor]
    public object get(int i) { ... }
}

> Observable 装饰器:

class ChangeEvent {
    public Collection Source { get; private set; }
    public Method UpdateType { get; private set; }
}

interface Observer {
    public notifyChange(ChangeEvent e);
}

typename<T> : where T is Collection
class Observable : Collection {
    public Observer Observer { get; set; } // additional property
    private T m_source;

    [mutator]
    public void add(object o) {
        if (Observer != null) {
            var event = new ChangeEvent() { Source = this, UpdateType = GetCurrentMethod() };
            Observer.notifyChange(event);
        }
        m_source.add(o);
    }

    [mutator]
    public void remove(object o) { ... }

    [accessor]
    public object get(int i) { ... }
}

> decorator:

Ordered decorator:

typename<T> : where T is Collection
class Ordered : Collection {
    private T m_source;

    [mutator]
    public void add(object o) {
        int idx = findProperPosition(); // assumed possible using the base Collection interface
        ...
        m_source.add(o);
    }

    [mutator]
    public void remove(object o) { ... }

    [accessor]
    public object get(int i) { ... }
}

/ em> decorator:

Read-only decorator:

typename<T> : where T is Collection
class ReadOnly : Collection {
    private T m_source;

    [mutator]       
    public void add(object o) { throw IllegalOperationException(...); }

    [mutator]
    public void remove(object o) { throw IllegalOperationException(...); }

    [accessor]
    public object get(int i) { return m_source.get(i); }
}

到目前为止,上面只是伪代码,客户端代码构造多种集合,以便每种类型只能组合一个存储方面任何数量的行为相关方面。如果能够在编译时构建这些复合类型并超级方便地在运行时生成那些复合类型,那将是相当不错的。

So far, the above is pseudocode only, but the goal is to enable the client code to construct many kinds of collections, so that each kind can combine exactly one storage aspect any number of behavior-related aspects. Would be pretty nice to be able to construct those composite types in compile-time and super-handy to generate those even at run-time.

问题是如何(任何现代编程语言计数)?

The question is how (any modern programming language counts)?

推荐答案

我不明白你怎么看不懂的方面。 Java集合已经存在,源是public的。有装饰器线程安全和不可修改。此外,许多类型都有诸如asList()等级联方法。

I don't understand what aspect of how that you don't understand. The Java collections has this already and the source is public. The have decorators for thread-safe and unmodifyable. Also, many types have fascade methods such as "asList()".

还有什么问题吗?

如何使未排序的东西?像,如何创建一个装饰器,将一个HashMap变成LinkedHaskMap?这样的装饰器可以用内部数据结构来建立以保持顺序。

How to make something that is not ordered ordered? Like, how to create a decorator that turns a HashMap into a LinkedHaskMap? Such a decorator could be built with an internal data structure to maintain order.

这篇关于集合作为装饰器:伪代码实现提议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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