Note that there are some explanatory texts on larger screens.

plurals
  1. POCollections as decorators: pseudo-code implementation proposal
    primarykey
    data
    text
    <p>Meanwhile waiting for the answer on the <a href="https://stackoverflow.com/questions/3131880/collections-as-decorators-in-c">question</a> 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:</p> <p>(excerpt from the referred question): </p> <blockquote> <p>Suppose you need to implement many (sub)types of collections. One of the aspects is <strong>storage-related</strong>: <em>list</em>, <em>array</em> etc, while the other is <strong>behavior-related</strong>: <em>ordered</em>, <em>remove only</em>, <em>observable</em> (the one that fires an event upon every change) etc.</p> <p>Obviously (again), the requirement maps directly to the well-known <strong>Decorator</strong> design pattern, where the <strong>storage-related aspect</strong> (<em>list</em>, <em>array</em>) will be decorated by multiple <strong>behavioral</strong> (<em>ordered</em>, <em>observable</em> etc).</p> </blockquote> <p>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:</p> <p>Basic interface every collection must support:</p> <pre><code>interface Collection { [mutator] public void add(object o); [mutator] public void remove(object o); [accessor] public object get(int i); } </code></pre> <p><strong>Storage</strong> aspect:</p> <p><em>List</em> implementation:</p> <pre><code>class List : Collection { [mutator] public void add(object o) { ... } [mutator] public void remove(object o) { ... } [accessor] public object get(int i) { ... } } </code></pre> <p><em>Array</em> implementation:</p> <pre><code>class Array : Collection { [mutator] public void add(object o) { ... } [mutator] public void remove(object o) { ... } [accessor] public object get(int i) { ... } } </code></pre> <p><strong>Behavioral</strong> aspect:</p> <p><em>Thread-safe</em> decorator:</p> <pre><code>typename&lt;T&gt; : 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) { ... } } </code></pre> <p><em>Observable</em> decorator:</p> <pre><code>class ChangeEvent { public Collection Source { get; private set; } public Method UpdateType { get; private set; } } interface Observer { public notifyChange(ChangeEvent e); } typename&lt;T&gt; : 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) { ... } } </code></pre> <p><em>Ordered</em> decorator:</p> <pre><code>typename&lt;T&gt; : 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) { ... } } </code></pre> <p><em>Read-only</em> decorator:</p> <pre><code>typename&lt;T&gt; : 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); } } </code></pre> <p>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 <strong>storage</strong> aspect any number of <strong>behavior</strong>-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. </p> <p>The question is how (any modern programming language counts)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload