通过C#理解复合模式,Remove(T item)函数实现

Understanding Composite Pattern via C#, Remove(T item) function implementation

我正在研究复合模式,使用 Judith Bishop 的 C# 3.0 设计模式(第 55 页)中的示例。

我要解决的问题涉及 Composite class 中的函数: public IComponent Remove(T item) 下面的 IF 语句有什么意义

 if (_holder != null)
            {
                (_holder as Composite<T>)._items.Remove(p);
                return _holder;
            }

public IComponent<T> Remove(T item)
    {
        _holder = this;
        IComponent<T> p = _holder.Find(item);
        if (_holder != null)
        {
            (_holder as Composite<T>)._items.Remove(p);
            return _holder;
        }
        else
        {
            return this;
        }
    }

    //Recursively looks for an item
    //Returns its reference or else null
    public IComponent<T> Find(T item)
    {
        _holder = this;
        if(Item.Equals(item))
        {
            return this;
        }
        IComponent<T> found = null;
        foreach(IComponent<T> comp in _items)
        {
            found = comp.Find(item);
            if(found != null)
            {
                break;
            }
        }
        return found;
    }

就我而言,_holder 变量总是在 RemoveFind 函数中赋值,因此不能为 null。 他们是否意味着要检查 p 引用是否为空?

接口 IComponent

 public interface IComponent<T>
{
    void Add(IComponent<T> component);
    IComponent<T> Remove(T s);
    IComponent<T> Find(T s);
    string Display(int depth);
    T Item { get; set; }
}

组件class实现:

    class Component<T> : IComponent<T>
{
    public T Item { get; set; }

    public Component(T item)
    {
        Item = item;
    }

    public void Add(IComponent<T> item)
    {
        Console.WriteLine("Cannot add to Single item!");
    }

    public IComponent<T> Remove(T item)
    {
        Console.WriteLine("Cannot remove directly the Single item");
        return this;
    }

    public IComponent<T> Find(T item)
    {
        if (Item.Equals(item))
        {
            return this;
        }
        return null;
    }

    public string Display(int depth)
    {
        return string.Format("-{0}---{1}", depth, Item);
    }
}

复合class实现:

public class Composite<T> : IComponent<T>
{
    public T Item { get; set; }
    private List<IComponent<T>> _items = new List<IComponent<T>>();
    private IComponent<T> _holder;

    public Composite(T item)
    {
        Item = item;
    }

    public void Add(IComponent<T> item)
    {
        _items.Add(item);
    }

    //Finds the item from a particular point in the structure
    //and returns the composite from which it was removed
    //If not found, return the point as given
    public IComponent<T> Remove(T item)
    {
        _holder = this;
        IComponent<T> p = _holder.Find(item);
        if (_holder != null)
        {
            (_holder as Composite<T>)._items.Remove(p);
            return _holder;
        }
        else
        {
            return this;
        }
    }

    //Recursively looks for an item
    //Returns its reference or else null
    public IComponent<T> Find(T item)
    {
        _holder = this;
        if(Item.Equals(item))
        {
            return this;
        }
        IComponent<T> found = null;
        foreach(IComponent<T> comp in _items)
        {
            found = comp.Find(item);
            if(found != null)
            {
                break;
            }
        }
        return found;
    }
}

检查是针对 p 而不是 this,因为 this 不能为 null。

_holder = this;
    IComponent<T> p = _holder.Find(item);
    if (_holder != null)

这不能为空 所以 _holder 也不能为 null 但查找可以 return null 在执行删除之前检查查找结果是非常合乎逻辑的。

另一种可能是

_holder as Composite<T> is null ?

所以

if (_holder as Composite<T>==null) 

也是一张有效的支票。

很可能这是一个错误,也是因为

     IComponent<T> p = _holder.Find(item); //THIS LINE !
    if (_holder != null)
    {
        (_holder as Composite<T>)._items.Remove(p);
        return _holder;
    }

如果我们以某种方式担心 _holder 变成 null(反射、多线程...),为什么我们之前不在线检查?如果我们假设在这种情况下它可以以某种方式变成这样,那是什么保证我在那条线上 _holder 不是 而不是 null

所以,很可能是一个错误。