Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

If I have a number of classes, lets say a variety of buildings of some sort. Each of these buildings have various properties and do some stuff etc

Now there is a set of operations I need to do on these buildings that are all conceptually the same. Lets call this conceptual group of operations Move building effluent. This functionality is essentially responsible for performing methods based on the information of each Building and then setting properties in other areas of the code as well as setting information on the building itself (can't help the need to set properties in other areas at the moment due to current system design limitations).

It's important to note that although these methods are conceptually the same and they set many of the same properties in various classes the way they calculate and get to these values are different (they do share some code however).

My question is should I put the code to do this into one class called say EffluentBuildingDistribution or should I put it into each buildings own class with appropriate methods to perform essentially the same code.

So either something like:

public class EffluentBuildingDistribution 
{
    public void DistributeEffluent()
    {
       // parameters example only
       DistributeBuildingHouse(var param1, var param2);
       DistributeBuildingDogHouse(var param1, var param2);
       DistributeBuildingSecondHouse(var param1, var param2);
    }

    // and various private methods to help work with the 3 methods some shared
    // others not    
}

or

public class HouseBuilding    {
   public void DistributeEffluent(var param1, var param2)
   {
      // do stuff
   }
}

public class DogHouseBuilding    {
   public void DistributeEffluent(var param1, var param2)
   {
      // do stuff
   }
}

Or perhaps neither of these options!.

I'm not sure if putting the code on the objects they are working on is the way to go, or putting it in a class that represents conceptually what we are trying to do. This is an example of my current dilemma however it's also a general responsibility question on where to proportion methods and actions etc

If it's not specific enough or needs more explanation etc let me know and I'll try to explain more of my issue.

EDIT: Due to some understandable confusion with my usage of the term Structure in my example, I since changed it to Building. Some of the answers below refer to Structure but they are still valid as I have not changed the core of the question.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

What about trying to make a generic version of it?

public class DistributeEffluent<T>
{
      public void DistributeEffluent()
      {
          // a generic algo working on T=HouseStructure as well as on T=DogHouseStructure... 
      }
}

How this has to look exactly depends on the programming language you are using (which you forgot to mention, I guess C# or Java?). You can realize this also by using template method pattern, which is essentially the same approach without generics. The thing you have to add here is a common interface or a common base class for your structure classes, so DistributeEffluent can work with this interface. In this in interface, there should be elementary methods implementing the individual behaviour of each structure, but nothing more.

share|improve this answer
    
Thanks doc. I probably didn't explain it that well but the actual algorithms for each method/structure are quite different so they do need their own code although they may have some shared parts. –  dreza Feb 26 '12 at 8:45
    
@dreza: the individual parts belong into your different structure classes, but only them. The shared parts belong either in a base class or in a separate class, like I showed you. And to my experience one will often find more shared parts to be refactored into common methods by thinking a while a about it. –  Doc Brown Feb 26 '12 at 8:55

I don't think I've ever said this before, but this looks like a prime candidate for the Visitor pattern.

the visitor design pattern is a way of separating an algorithm from an object structure on which it operates

In essence, the visitor pattern allows you to define the structure of the objects within the objects themselves (in an accept method), while allowing you to put all of your algorithms in the Visitor class (in a set of visit methods, which can then share code through private methods).

public class StructureVisitor
{
    public void visit (StructureHouse structure)
    {
        // do stuff to house here
    }

    public void visit (StructureDogHouse structure)
    {
        // do stuff to dog house here
    }

    public void visit (StructureSecondHouse structure)
    {
         // do stuff to second house here
    }
}

And then you have some parent class that has an accept method a bit like this:

public void accept (StructureVisitor visitor)
{
    this.House.accept(visitor);
    this.DogHouse.accept(visitor);
    this.SecondHouse.accept(visitor);
}

Each of the above accept methods would simply pass themselves on to the visitor class (unless there was more depth to the structure, obviously).

public void accept (StructureVisitor visitor)
{
    visitor.visit(this);
}
share|improve this answer
    
No there isn't any more depth. I've never used this pattern before, well not consciously anyway as far as I know so will have to read up more. cheers –  dreza Feb 26 '12 at 19:50
    
Well, if there's no depth to the structure, and it's not going to change frequently, the Visitor might be overkill. A Strategy may be a better option. It is a rare case where the Visitor can't be handled equally well by a more simple pattern. I think I was thrown a little by your first example and the word "structure" which is used in a different context here. –  pdr Feb 26 '12 at 20:02
    
ah fair point. I'll update the question as the term structure was more to make an example of what I was wanting to achieve –  dreza Feb 26 '12 at 20:11

I would probably create a DistributeEffluent interface and one or more implementations of it. Then, I would create a member of the interface in the structure classes to be able to use the operations.

Take a look at the Wikipedia article on Strategy Patterns.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.