English 中文(简体)
设计问题: 一个班子有两个角色。 第一是制造案例,另一是类型
原标题:Design Issue: Two roles for a class. First for instances creating and the other for types

Consider this class diagram:

 +--------+     * +------------------+ * afraid-of             +------------+
 | Garden |------>|    Animal        |<------------------------| Baby       |
 +--------+       +------------------+                         +------------+
                        ^
                        |
                 +----------+------+
                 |                 |
          +------+------+   +------+-----+
          | Cat         |   | Wolf       |
          +-------------+   +------------+

动物有两个作用:

  1. They will be walking around the garden (Class Instance)
  2. The baby is afraid of some animals (Class Type)

What baby should keep? One instance of each animal it s afraid of? (sounds like poor design) The type name? (I always try to avoid refactoring)

如何解决这一问题? (如果使用C#,我只提到它,因为我希望这里有一种普遍、无语言设计模式或想法)

成就

最佳回答

If babies are always afraid of the same animals, you don t have to keep anything. You can have a method:

public bool IsAfraid(Animal animal)
{
    return animal is Wolf;
}

If babies are afraid of different animals in different places and times, you should make every animal have a property AnimalType (flag enum). The baby will then have a property AfraidOf, of type AnimalType. And then, checking if you are afraid of an animal is simple:

bool afraid = this.AfraidOf.HasFlag(animal.AnimalType);

最后,您的表象中不同类型的关系,可能与不同类别的关系有不同类型的联系。

问题回答

A Baby can have a list of animals he/she is afraid of. I would specify independent methods to add and remove animals from the list coz with time passing some animals can be removed or added. It is possible to change the list of animals directly through the property, but I would prefer specific methods in this case.

public class Animal { }
public class Cat : Animal { }
public class Wolf : Animal { }

public class Baby
{
    public List<Animal> ScaryAnimalsList { get; private set; }

    public Baby()
    {
        ScaryAnimalsList = new List<Animal>();
    }

    public void AddAnimalToScaryList(Animal animal)
    {
        ScaryAnimalsList.Add(animal);
    }

    public void RemoveAnimalFromScaryList(Animal animal)
    {
        if (ScaryAnimalsList.Contains(animal))
            ScaryAnimalsList.Remove(animal);
    }

    public bool IsAffraidOf(Animal animal)
    {
        return ScaryAnimalsList.Contains(animal);
    }
}

And this is how to use it

class Program
{

    static void Main(string[] args)
    {
        Cat cat = new Cat();
        Wolf wolf = new Wolf();

        var babyJo = new Baby();
        babyJo.AddAnimalToScaryList(wolf);
        babyJo.IsAffraidOf(wolf);   //true
        babyJo.IsAffraidOf(cat);    //false

        var babySam = new Baby();
        babySam.AddAnimalToScaryList(wolf);
        babySam.AddAnimalToScaryList(cat);
        babySam.IsAffraidOf(wolf);   //true
        babySam.IsAffraidOf(cat);    //true

        var babyBob = new Baby();
        babyBob.IsAffraidOf(wolf);   //false
        babyBob.IsAffraidOf(cat);    //false
    }
}

Another option would be to add features to your animals.

public class Animal
{
    public virtual bool EatsSmallChildren { get { return false; } }
}

那么,如果动物是 s,而不是婴儿是否害怕,那么你的婴儿就可以检查。

这给你的动物阶层带来了负担,并带来了一些概念上的争.,但也非常直截了当,容易理解和维护。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签