c# - Design approach for class objects with many common and some unique methods -
in example below, have 2 instances of terrier
class, derives dog
.
1 instance referenced variable of type terrier
.
using variable, have access members of terrier class.
on other hand, variable type dog
can reference members of dog
class, though reference points instance of terrier
.
terrier bubba = new terrier("bubba", 2, "happy"); bubba.growl(); // can call terrier.growl dog jack = new terrier("jack", 17, "surly"); jack.growl(); // error: can't call growl method
i need implement class mypets
has list<pets>
can hold either of cat
object or dog
object.
both these object have common methods makenoise()
unique methods not in base class cat has method climbtree()
.
mypets
class have method iterate through list<animals>
, calls makenoise()
method , climbtree()
method.
what should best approach achieve this, using abstract base class or other method?
with regards comment, along these lines should solve issue:
public class visitor { public void doitterate(cat c) { console.writeline(c.tostring()); c.makenoise(); c.climbtree(); } public void doitterate(dog d) { console.writeline(d.tostring()); d.makenoise(); } } public abstract class pet { public pet(string name, int age, mood mood) { this.moodofpet = mood; this.name = name; this.age = age; } public string name { get; private set; } public int age { get; private set; } public mood moodofpet { get; private set; } public abstract void makenoise(); public override string tostring() { return this.name + " " + this.age + " years old , feels " + this.moodofpet; } public abstract void accept(visitor v); } public enum mood { surly, happy } public abstract class dog : pet { public dog(string name, int age, mood mood): base (name, age, mood) { } public override void makenoise() { console.writeline(this.name + " woofing"); } public override void accept(visitor v) { v.doitterate(this); } } public class sheepdog : dog { public sheepdog(string name, int age, mood mood): base (name, age, mood) { } } public class cat : pet { public cat(string name, int age, mood mood): base (name, age, mood) { } public void climbtree() { console.writeline(this.name + " climbing"); } public override void makenoise() { console.writeline(this.name + " meowing"); } public override void accept(visitor v) { v.doitterate(this); } } public class terrier : dog { public terrier(string name, int age, mood mood): base (name, age, mood) { } public void growl() { console.writeline(this.name + " growling"); } public override void makenoise() { growl(); } } public class mypets { private visitor visitor = new visitor(); public mypets() { pets = new list<pet>(); } public list<pet> pets { get; private set; } public void addpet(pet p) { pets.add(p); } public void itterate() { foreach (pet p in pets) { p.accept(visitor); } } }
it's standard oop design using abstract methods overloaded in more concrete classes later on.
edit it's using visitor pattern
running following code:
mypets pets = new mypets(); pets.addpet(new cat("bob", 2, mood.surly)); pets.addpet(new terrier("jack", 17, mood.surly)); pets.addpet(new sheepdog("bubba", 2, mood.happy)); pets.itterate();
produces these results:
bob 2 years old , feels surly
bob meowing
bob climbing
jack 17 years old , feels surly
jack growling
bubba 2 years old , feels happy
bubba woofing
Comments
Post a Comment