0

So say I have a class

public abstract class Mammal { ... }
public class Wolf : Mammal
{
   // concrete methods and 
 }

But I don't want to do something like:

List<Wolf> myWolfPack = new List<Wolf>();

I sort of want to say:

WolfPack myWolfPack = new WolfPack();

Which means I basically need to write a WolfPack class:

****public class WolfPack : IList<Wolf>
{
    // properties, methods that implements IList

  // WolfPack-specific methods
}****

The reason I want to do this is because I wolf packs have specific properties (e.g. a leader, an omega).

  WolfPack-specific methods
    Wolf Leader { get;set; }
    Wolf Omega { get;set; }

So here are my newbie questions:

What is this called? Is there a name for wanting to add methods to a collection/list/existing concrete object? If I'm just using a private List inside my WolfPack class, should I instead just say: public class WolfPack : List

? Seems weird to do this with a concrete class, no? Are there any downsides to this?

Sorry if my words are weird.

  • 1
    You can inherit from collection like: http://stackoverflow.com/questions/5376203/inherit-listt – Ted Mar 31 '14 at 17:52

3 Answers3

6

I suggest you use composition instead of inheritance. Something like this:

public class WolfPack
{
    public IReadOnlyList<Wolf> Members { get; private set; }
    public Wolf Leader { get; private set; }
    public Wolf Omega { get; private set; }
}

Any operations specific to a WolfPack collection of Wolf should be methods on the WolfPack class. This does not require any modification to the .NET collection class used internally.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
1

Encapsulation is probably what you're looking for. However, to answer

Is there a name for wanting to add methods to a collection/list/existing concrete object?

directly, you probably want extension methods.

I would argue that WolfPack may seem, at first glance, to be an IList<Wolf>, but are you sure that a WolfPack must expose every member that IList<> will require? Chances are, it won't. You probably want to use a List<Wolf> inside your WolfPack implementation as a private field.

NathanAldenSr
  • 7,841
  • 4
  • 40
  • 51
0

You don't have to inherit from the collection class. There are reasons why inheritance is considered problematic - Prefer composition over inheritance?

Instead, your could pack class can be composed of items:

public class WolfPack {

   // composed of items
   private List<Wolf> _wolves = new List<Wolf>();

   // lets you add items
   public void Add( Wolf wolf ) {
      this._wolves.Add( wolf );
   }

   public IEnumerable<Wolf>() {
      return this._wolves;
   }

   public Wolf Omega { get; set; }

...

This way you hide the way pack is composed of items and, in future, you could even completely change the implementation (to use another type of list or to change the way wolves are added to the pack for example) with/without changing the interface. On the other hand, inheriting from List would determine the class interface and would not let you change/remove specific methods/properties.

Community
  • 1
  • 1
Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106