English 中文(简体)
Is this a known pattern?
原标题:

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.

最佳回答

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

问题回答

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.

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

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.

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

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

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().

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.





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签