DidIReallyWriteThat
DidIReallyWriteThat

Reputation: 1033

Architecture for Interface/Abstraction

I'm running into my least favorite problem, and that is rewriting code. Here is the structure I have currently designed.

public interface ICanMove
{
    int speed { get; set; }

    void Walk();
    void Run();
}

public class Worker : ICanMove
{
    speed = 5;

    public void Walk()
    {
    //Example code Move(speed);
    }
}

public class Warrior : ICanMove
{
    speed = 3;

    public void Walk()
    {
    //Example code Move(speed);
    }
}

The problem here is multiple classes use the interface the same way. But they won't always do it the same way. Is there a way to implement this..more correctly. The other option i have is an abstract class which would work for this example, but not when I have even more interfaces(ICanAttack,ICanBuild)

With abstract class

public abstract class LivingCreature : ICanMove
{
    public abstract int speed {get;set;}

    public void Walk()
    {
       //Example code Move(speed);
    }

    public void Run()
    { 

    }
}

But then my problem becomes I have to implement multiple interfaces on a base class which not every object that inherits from that base class will need 100%

Upvotes: 0

Views: 626

Answers (3)

Ryan Ternier
Ryan Ternier

Reputation: 8804

I'd suggest following the strategy pattern for this approach.

You're Walk/Run should be interfaces for movement.

For example, you might have a Robot who can only "roll" and not run/walk. So the movement of that robot should be Roll.

The first chapter in the following book will help you understand this pattern in respect to your current code sample.

http://www.amazon.ca/Head-First-Design-Patterns-Freeman/dp/0596007124 (this is in Java, but easily translatable to C#) This book is also online for free on many sites.

Online version (Free): http://www.dcc.ufrj.br/~comp2/TextosJava/Head%20First%20-%20Design%20Patterns.pdf

This SO question also talks about the Strategy pattern with Ducks and Flying. This page is also part of the book. Duck example strategy pattern - Head first design pattern

For example:

public interface IMovement
{
    int speed { get; set; }
}

public class Worker
{
    speed = 5;
    IMovement Movement;
   public Warrior(IMovement m)
    {
        this.Movement = m;
    }
    public void Move()
    {
      this.Movement.Move();
    }
}

public class Warrior
{
    speed = 3;
    IMovement movement;

    public Warrior(IMovement m)
    {
        this.Movement = m;
    }

    public void Move()
    {
      this.Movement.Move();
    }
}

void foo()
{
     IMovement m = new FlyingMovement();
     Worker w = new Worker(m);

    IMovement swimmingMovement = new SwimmingMovement();
     SwimmingWorker sw = new SwimmingWorker (swimmingMovement);
}

Sorry , I'm on my phone atm. However, this will give you more versatility in your code.

Upvotes: 1

Andrew Hewitt
Andrew Hewitt

Reputation: 331

Another approach could be to use helper classes (I read a great example using Michael knight and kitt the other day. Nailed it).

So you have a class (Iperson) which can be helped by arbitrary classes that implement something like 'influence(Iperson)'.

The 'warriorhelper' helper could influence the person by 'strikefoe' and the builderhelper by 'buildhome'.

Again, hope this helps!

Ps. Still in pub on a tablet. Please excuse typos and lack of code examples.

Upvotes: 0

Andrew Hewitt
Andrew Hewitt

Reputation: 331

(On a tablet in the pub atm, so excuse typos and lack of code examples)

Well, the analogy could run something along the lines of superheroes: they all have a number of special abilities but all are arbitrary and different. The abilities are "bestowed" upon them by an external event (peter Parker can climb walls because he was bitten by a spider).

So, your class structure could be such that you have a iHasAbility interface which implements a list of iAbility. You can bestow those abilities through a 'Bestow(liability)' method, and then have some way to enact those abilities... 'Enact("climbwall")'.

So... Composition rather than abstraction. The abilities become abstract rather than the subject.

Hope this helps or even just sparks a thought.

Upvotes: 1

Related Questions