同时等待 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.
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# 实现以下内容,如果不能,则可以用任何其他现代编程语言实现:
每个集合必须支持的基本接口(interface):
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) { ... }
}
行为方面:
线程安全装饰器:
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) { ... }
}
可观察装饰器:
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) { ... }
}
有序装饰器:
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) { ... }
}
只读装饰器:
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); }
}
到目前为止,上面只是伪代码,但目标是使客户端代码能够构造多种集合,以便每种集合可以精确地组合任意数量的存储方面>行为相关方面。如果能够在编译时构造这些复合类型,并且即使在运行时也能非常方便地生成这些复合类型,那就太好了。
问题是(任何现代编程语言都重要)如何?
最佳答案
我不明白你不明白的是哪一方面。 Java 集合已经有这个并且源是公开的。它们具有线程安全且不可修改的装饰器。此外,许多类型都有外观方法,例如“asList()”。
鉴于此,您的问题还剩下什么?
如何将未订购的东西制作成订购的?比如,如何创建一个将 HashMap 转换为 LinkedHaskMap 的装饰器?这样的装饰器可以使用内部数据结构来构建以维持顺序。
关于c# - 作为装饰器的集合 : pseudo-code implementation proposal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3133176/