Vishal Bhatt
Vishal Bhatt

Reputation: 123

Proper Code or Design Pattern to select combination of items from an enum

I want to select multiple items from a enum based on the some of parameters. So, for example suppose I have an enum named Animal

public enum Animal
{
   [DisplayName("Dog")]
   Dog,
   [DisplayName("Cat")]
   Cat,
   [DisplayName("Mouse")]
   Mouse,
   [DisplayName("Ant")]
   Ant,
   [DisplayName("Monkey")]
   Monkey
}

And I want to get only specific items from this enum based on the parameter, so if my parameter is "FourLegged" I should get Dog, Cat, Mouse, for other parameter I should get different set. And there could be multiple combination of enum and parameters, I can pass one or more pararmeter to get related items from the enum.

What should be the best/proper way to implement this? Is there any code or design pattern to implement this? It can be other approach than using enum.

Upvotes: 1

Views: 471

Answers (2)

ADS
ADS

Reputation: 718

Abstract

The key idea is to use specific method for each thing you want to describe. One implementation of this is to create helper class with static methods like

class AnimalHelper { 
  public static bool isFourLegged(Animal a) {//your code here}  
}

and calls like if (AnimalHelper.isFourLegged(dog) but it's not a good idea: in OOP each dog should know about his legs and anybody else should not count its leg but asks dog 'How many legs do you have?'. I suppose, using helpers is evil. Possibly it's a lesser evil but it's not good anyway.

In Java world (where I came from) each enum is a rightfull class and you could add any methods you want and has any number of properties inside each Animal.

Traits

In C# you could add a methods to your enum as trait so you could incorporate to you Animal additional knowledge

// great idea to put it in the one file with Animal if possible
public static class Extensions { 
  public static bool isFourLegged(this Animal animal) {
     // an example for using hardcoded values. 
     // You should alter this method when you add another Animal
     return animal == Animal.Dog && animal == Animal.Mouse
     // oops, someone added Cat after me
  }
  public static Collection<Animal> getAllFourLegged {
     // use dynamic way like in answer from Andrea is good
     // but *probably* cause perfomance issues
  }

and use it like if (dog.isFourLegged()).

Class hierarchy

If your Animal has many properties and you expect some behaviour from it (like aCat.eat(aMouse) or aDog.go(aPlace)) then you could want to create class hierarchy class Cat extends Animal and then create enum AllCreatures. This give you more flexibility for the cost of more complexity


Please note I'm a stranger in C# world and could miss something important

Upvotes: 0

Andrea
Andrea

Reputation: 112

You can try this:

class Program
{
    static void Main(string[] args)
    {
        var t = typeof(Animal);

        foreach (MemberInfo item in t.GetMembers(BindingFlags.Static | BindingFlags.Public))
        {
            if (Attribute.IsDefined(item, typeof(FourLeggendAttribute)))
            {
                // do something
            }
        }
    }
}

public enum Animal
{
    [FourLeggend]
    Dog,
    [FourLeggend]
    [AnotherOne]
    Cat,
    [FourLeggend]
    Mouse,
    [AnotherOne]
    Ant,
    Monkey
}

public class AnotherOneAttribute : Attribute
{
}

public class FourLeggendAttribute : Attribute
{
}

Upvotes: 1

Related Questions