Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 将一个子对象中的更改传播到另一个子对象_Oop_Design Patterns - Fatal编程技术网

Oop 将一个子对象中的更改传播到另一个子对象

Oop 将一个子对象中的更改传播到另一个子对象,oop,design-patterns,Oop,Design Patterns,我遇到过几种情况,其中一个父对象包含多个对象,而对一个子对象中的信息所做的更改会影响其他对象 例如,考虑下面的情况: interface IUniverse { IStorage ShirtStorage; IList<IHuman> Humans; } Interface IStorage: { string Location; int Capacity; IList<IShirt> Items; } Interface IH

我遇到过几种情况,其中一个父对象包含多个对象,而对一个子对象中的信息所做的更改会影响其他对象

例如,考虑下面的情况:

interface IUniverse
{
    IStorage ShirtStorage;
    IList<IHuman> Humans;
}

Interface IStorage:
{
    string Location;
    int Capacity;
    IList<IShirt> Items;
}

Interface IHuman
{
    string Name;
    int Age;
    IList<IShirt> Shirts;
}
之后,上述接口的实现将不会在从衬衫存储中移除特定衬衫时从所有人身上移除任何东西

这种设计的缺点是,每当程序员从宇宙中删除一件衬衫时,他都必须手动删除每个人的每个引用

也就是说,程序员必须知道宇宙的确切结构,才能脱下一件衬衫。如果宇宙的结构变得非常复杂,以至于对特定衬衫的引用可能不仅仅出现在《人类》中,这可能被证明是错误和乏味的


其次,我们同样将
Add(IShirt)
Remove(IShirt)
方法引入接口
IStorage
IHuman

interface IUniverse
{
    IStorage<IShirt> ShirtStorage;
    IList<IHuman> Humans;
}

Interface IStorage
{
    string Location;
    int Capacity;
    IList<IShirt> Items;
    **void Add(IShirt);**
    **void Remove(IShirt);**
}

Interface IHuman
{
    string Name;
    int Age;
    IList<ICShirt> Shirts;
    **void AddShirt(IShirt);**
    **void RemoveShirt(IShirt);**
}
通过这样做,我们迫使界面的消费者接受移除衬衫不仅会影响衬衫的存储,还会影响
IUniverse
的其他成员

然而,缺点与第二种解决方案类似。除此之外,
IUniverse
实例最终在某种程度上成为上帝的对象。每一个我需要从宇宙中脱掉衬衫的地方,我都必须有一个宇宙的参照物

如果某个特定的GUI组件只是想显示
衬衫存储的信息,并允许与存储交互(即添加和删除衬衫),这会不会在GUI组件和
世界之间引入一些耦合,当唯一应该存在的耦合是GUI组件和
IStorage
的耦合时



我已经编写了几个应用程序,它们混合使用了所有三种解决方案。事实上,在不同的情况下,有些解决方案似乎比其他解决方案更好,但不一致是一种痛苦,因为在从一种设计切换到另一种设计时,我几乎总是忘记做某些事情。

每当我在oop环境中听到“传播更改”这句话时,我马上就会想。因此,我将引入另一个类来接管这个职责,并让您的域对象引发相关事件,而不是让您的域对象负责“同步”添加和删除衬衫。你们还坚持单一责任原则


HTH

每当我在oop环境中听到“传播变化”这个短语时,我马上就会想。因此,我将引入另一个类来接管这个职责,并让您的域对象引发相关事件,而不是让您的域对象负责“同步”添加和删除衬衫。你们还坚持单一责任原则

interface IUniverse
{
    IStorage<IShirt> ShirtStorage;
    IList<IHuman> Humans;
}

Interface IStorage
{
    string Location;
    int Capacity;
    IList<IShirt> Items;
    **void Add(IShirt);**
    **void Remove(IShirt);**
}

Interface IHuman
{
    string Name;
    int Age;
    IList<ICShirt> Shirts;
    **void AddShirt(IShirt);**
    **void RemoveShirt(IShirt);**
}
class Storage : IStorage
{
    IUniverse parentUniverse;
    string Location;
    int Capacity;
    IList<IShirt> Items;

    // ... Implementation for Add(IShirt item) is trivial

    void Remove(IShirt item)
    {
        this.Items.Add(item);
        foreach (IHuman human in this.parentUniverse)
            foreach(IClothing clothing in human.Clothings)
                if (clothing == item)
                    human.RemoveClothing(clothing);
    }
}
interface IUniverse
{
    IStorage ShirtStorage;
    IList<IHuman> Humans;
    void Add(IShirt);
    void Remove(IShirt);
}

Interface IStorage:
{
    string Location;
    int Capacity;
    IList<IShirt> Items;
}

Interface IHuman
{
    string Name;
    int Age;
    IList<IShirt> Shirts;
}