views:

289

answers:

8

I am trying to separate behaviour from data completely in my classes and came up with this simple solution:

class ClassAData
{
    public int Property1;
    public string Property2;
    public bool Property3;
}

class ClassA : SomeInterface
{
    public ClassAData Data;

    //behaviour
    public int CalculateSomething(int value)
    {
        ...
        return result;
    }
    public string SomeOtherMethod(){...}           
}

(proper encapsulation would of course be applied...)

I was wondering if this is known by something or used in a common pattern? Also what are the shortcomings if there are any?

Edit: Perhaps I should have been clearer about where I intend to use this. I do not advocate using this for every class in every situation. I plan to use this in a service-oriented application where ClassA is the actual domain object and ClassAData would be a DTO that is transferred between the service and presentation layers. This approach avoids a fair bit of code duplication especially if there are many classes with lots of properties.

+4  A: 

its a known anti-pattern - the anemic domain model see http://martinfowler.com/bliki/AnemicDomainModel.html and http://en.wikipedia.org/wiki/Anemic%5FDomain%5FModel to see what problems it can cause

mcintyre321
No! Have a second look. ClassA is the actual domain object (with behaviour). This has nothing to do with this anti-pattern. The main reason why I want to separate data in this manner is to avoid duplicating code for DTOs, which are used between the service and presentation layers.
+6  A: 

I'm not sure why you would want to separate behavior from data. Object oriented programming specifically joins data with the behaviors associated with that data. I've personally never seen data entirely separated from behavior in the manner you are doing.

Matt Hamsmith
Sometimes it is better to separate algorithmic behavior from data carriers. You see this quite a lot in filter based designs (e.g VTK)
Stefano Borini
+1  A: 

Strategy Pattern (or possibly the Template Pattern). Any chance you have the Head First Design Patterns book - it explains the Strategy Pattern so very well in the first chapter.

Upper Stage
Wouldn't the strategy pattern imply that you can exchange the strategy (e.g. by passing the strategy to the constructor)? Whereas here it is fixed.
Fabian Steeg
Yes; I made the assumption that he would change his code to accept behavior (strategy). Of course, with the Template pattern, the behavior is more "fixed."
Upper Stage
But for the template pattern, shouldn't there be subclassing involved (to have subclasses specify how the template method is filled)? I'd say what you are referring to is just plain composition.
Fabian Steeg
Of course you are correct; the Template pattern does rely on subclassing. And with a nice JS framework, subclassing mirrors subclasses in stronger typed languages. See http://www.extjs.com/blog/2009/11/11/advanced-plugin-development-with-ext-js/ for a nice writeup of using the Template pattern in ExtJS.
Upper Stage
A: 

It's in a way a common pattern espacially if the "data" part is being generated from outer source.

I would recommend using a partial class for this approach

ArielBH
A: 

This actually kind of sounds like you're trying to separate your model from your controller, a la MVC.

Tim S. Van Haren
A: 

Your object then has no methods that act on or for the object. Therefore it defeats the purpose as being object orientated. True object orientation involves properties and methods that work with the object. Otherwise you are just going to design a class that has gets() and sets().

JonH
+3  A: 

What you are doing is to separate behavior from data. It is similar to the strategy pattern, with the difference that it's reversed. I would not say it's a bad idea (could be useful if your data has different storage techniques but the same behavior). As some pointed out, it has also strong connotations of MVC. I would not say, instead, that is an anemic antipattern. You do have behavior, you are just not storing data on the same class instance.

Summing up I would not condemn it, and it is similar to strategy and MVC

Stefano Borini
A: 

If you would change the member in ClassA to a ClassAData*, then it would be useful for keeping binary compability, or implementing shared data between objects (with e.g refcounts and copy-on-write).

As the code is written in your question, I really see no upside to it, that could not also be provided by godd code formatting and comments.

gnud