views:

359

answers:

7

In college I took on a class on modern physics, in which we learned about special relativity. I was completely blown away by how different frames of reference could actually observe physical properties of an object to be different and neither be incorrect. Over time, this concept has slowly been changing the way I program, to the point where now I tend to break up classes into 2 main categories, data objects and observing (function only) objects.

For the sake of this not becoming a elaborate and lengthy post for such a simple question, I'll try to explain what I mean through two examples.

First, take for instance this type of code, which I used to often write:

class Deck
{
    private Card[] cards;

    public void Shuffle()
    {
        // shuffle algorithm
    }

    // other deck related functions like draw, cut, etc...
}

I typically now write the same scenario as:

class Deck
{
    // by intention, i just mean some kind of immutable collection of cards
    private ReadonlyCollection<Card> _Cards;

    public Card[] Cards { get { return this._Cards; } }
}

interface IDeckHandler
{
    Deck Shuffle(Deck deck);
    // other deck related functions like draw, cut, etc..
}

class Dealer : IDeckHandler
{
    // IDeckHandler implementation
}

The Deck is no longer responsible for implementing the functions which can act on it. Or to make it fit the terminology, the deck is just a set of values and the way in which it is observed is the responsibility of the observer. Naturally, there can be many observers who perform the actions differently.

For the second example, I'll use something which people I have tried to explain this to have had an easier time with. Take the situation where we have colored letters on colored paper which spell out a word. We have an agent whose responsibility it is to read the word on the paper. Now assume the agent is some type of color blind. The image being emitted from the paper is the same, but the perception could be different. The observer does not have intimate knowledge of the object and cannot modify it, only respond with an interpretation of it.

As I have stated, this concept drives many of my development decisions. So back to the question, is this a published type of programming and if so, can you point me to some literature on it? There are some common and uncommon scenarios I have run into which are difficult to make decisions for, and certainly some things I just haven't thought of or run into yet which would hopefully have been examined in literature.

+7  A: 

It seems to me like you're implementing functional programming in an OOP way. Regardless of the Physics explanation about "special relativity" , The entire idea of OOP is basically encapsulation - You want to objects to know themselves how everything should be done. Basically what you're saying here is "no , there is only data and functions that work on the data" . What if the deck changes? you get a new Dealer? how will you know which dealer should be created to deal the new deck?

If you thought about a "switch" statement , then you're hammering OOP concepts into a functional programming template.

yossale
I understand the idea of encapsulation. I typically regulate it to hiding the structure in which the properties of my object are stored. For instance, there is no reason the deck couldn't be stored internally as a single 64 bit integer, while the exposed properties of the object are the same however. To answer your question however, if the deck changed, yes the dealer would have to change, but I am willing to accept that because I think that more emulates the real world.
NickLarsen
One of the major problems I have run into with this is that without intimate knowledge of storage of the value object, many of the algorithms which would operate on the value objects are much less efficient, and far less clever than they could be. For instance I implemented TicTacToe using this concept and storing the state as a single 32 bit integer. The process of checking for a winner could be a single bitwise AND if the winner determination was built into the state itself, but instead much access the exposed multidimensional board property. I'm trying to figure out how to overcome that.
NickLarsen
On your second comment: the problem you're describing is actually good design in many situations. Building logic around a specific (rather tricky) implementation fact (that state is stored as a single integer) is often bad. It's better to build the logic around the meaning of the state (the board). There are of course optimization situations when you want to access the implementation directly, but doing that is a premature optimization and leads to less readable and maintainable code imho.
Jakob
@Jakob: I agree with you completely about building logic around state being premature optimization, which is one of the attributes of this programming style I like the most. I especially like how it separates re-enforces the role of the observers to perform their actions on arbitrary implementations. Still the feat of optimization remains however, and in my opinion deserves to be overcome in a full design pattern specification. For conceptual readability however, it cleans up the development process.
NickLarsen
@yossale: I particularly like your first statement, `It seems to me like you're implementing functional programming in an OOP way`. The biggest struggle I have with defining this concept is where functional elements should be built into the OOP model. I do not understand your comment about the switch statement however.
NickLarsen
yossale
@yossale: basically what I get from your last comment is that during planning, I should decide if it should be purely functional, and go that route, or make it purely OOP. If thats the case, that is certainly something I can live with, though I think a design is _possible_ which eloquently mixes the two.
NickLarsen
@nickLarsen: There is not "Superior" way to write code . Functional is appropriate in some cases , and so does OOP. However , if you're using OOP , using the "correct" form (i.e encapulation , factories , interfaces) usually brings better results then trying to force OOP to do functional programing. If you want functional , write functional , why burden yourself with all the OOP overhead? And yes , sometimes there's no choice but to have something "functional" in an OOP design , but it's more often then not a sign of an OOP design issue.
yossale
@yossale: I'm not suggesting there is a "superior" way to write code. I am suggesting that there could be a design which allows for the mixing of the two styles in such a way that models of systems act more like the systems that they model. By that I mean data is encapsulated, and acts on the data are functional. I'm also NOT saying my examples illustrate that exactly, but do show how my programming has changed since I started thinking about these things.
NickLarsen
@nickLarsen: "data is encapsulated, and acts on the data are functional" means you're separating the information from the action . That's not an Object (in the pure sense) . An object contains both the data and the knowledge on how to work on it. That's a string tendencies to function programming , and should probably use a design that best utilizes these aspect . IMO , OOP is not.
yossale
+1  A: 

What you're talking about is one of the great "a HA!" moments that object oriented programmers run across. It's fun when it happens. I'd start with the gang of four "Design Patterns" book and branch out from there.

Michael Wilson
Would somebody who's never seen the book know what "gang of four" means?
Robert Harvey
@Robert Harvey: No, they wouldn't. But they could use Google to find out.
S.Lott
Robert: It can help when googling, since "design patterns" alone turns up 83 kerbillion hits.
Ken
"kerbillion," ironically enough, turns up only 1,490 hits. It's heterological.
Carl Manaster
Of all the design patterns I have studied, gang of four is one I have only heard of and not learned. Thank you for bringing it to my attention.
NickLarsen
+4  A: 

Interesting. Well, I think you are doing Model->Controller->View (MVC Pattern) but in this case you are only using the Controller and Model parts separately.

The gains here are clear if you have multiple scenarios in which the objects will be used, its a typical POJO+Managers way of doing things. In this case the objects themselves are dumb and without any functionality besides their own state.

The advantages are clear in the ways of separation of responsibility although the disadvantages are a bit more handling on the manager side of things.

If you think about it, if the objects don't do anything with themselves you are basically cutting out one level of indirection (the base level) and everything must be indirected to use. This means more code to handle unexpected situations, null objects and such. Perhaps more glue code than needed.

Flexible? Yes. Practical? Only you can answer.

Rui
+3  A: 

This design pattern can work well, particularly if the operations do not mutate the original data objects. In .NET, for instance, LINQ and its associated extension methods can be seen as general operations for dealing with enumerations, so that the enumerations themselves do not need to know how they may be used. The methods do not, however, mutate the collections being enumerated, but merely provide a new way to interpret, filter and group the enumeration.

If the functionality is mutating the original objects, however, then I tend to prefer that functionality to be encapsulated as methods on the object. That way the object is responsible for maintaining its own invariants and you insulate more of the implementation details from the clients of the object. The more implementation detail you must leak in order to work with the object, the more opportunity there is for it to be used incorrectly and cause bugs.

Dan Bryant
+1 for the distinction between functionality that does and does not mutate the original data object. After all: an observer usually does not meddle with the data, it just observes it. And something that does want to affect a change in the data should tell not ask. Tell the data class to do something (which changes its values) and not ask for the current value)s_ and then change some of them. Shuffling a deck is something best left to the deck.
Marjan Venema
@Marjan, of course, since the Deck is just a collection of Card instances, you could see Shuffle as an operation that returns a brand new Deck that happens to contain the same Card instances. It might not make sense to talk of a Deck shuffling itself, but you could alternately encapsulate both the mutable Deck and Shuffle operation into a Dealer who deals Card instances without even revealing that there's a Deck from which he's dealing. This way the other players can't mess with the Deck while the Dealer isn't looking.
Dan Bryant
I see what you mean. I am gonna munch on that idea some more...
Marjan Venema
I like your answer in that it focuses on mutability of objects, however in my model, all data objects are immutable. The state of no objects will ever need to change in their place because they are only ever observed, and a transformation takes place in that observation. Should the state of an object ever need to change, for instance in a place of persistence, the place of persistence should implement an observer (in its own frame of reference), which simply replaces the original with the latest transformation.
NickLarsen
@NickLarsen, it sounds like you would be very interested in functional languages, like Haskell (a pure functional language) or F# (a functional language with access to .NET types.) Observation and transformation of immutable state for the consumption of other observers is what a functional model is all about.
Dan Bryant
+5  A: 

Some aspects of this programming style are covered in Data-Oriented Programming (a development style that focuses on the layout and transformation of data).

My main problem with this style is that if there are implicit assumptions/constraints for a given type (e.g., say the card deck must never have 2 jokers in a row after a shuffle), you must duplicate those constraints/checks throughout all of the manager types since the data type you're operating on is totally dumb -- it can't look after itself, it's just a data bag. You can extract the duplicate logic into a separate method, but it's generally a bit harder to write good, clean code using this method.

Compare this to implementing a Deck.Shuffle method that takes an IDeckShuffle strategy. In this scenario, you can perform the shuffle and then add invariant checks as a post step to ensure that no matter what shuffle strategy was used, the deck will never enter an invalid state; the code that enforces integrity is in one place and is easy to verify and update.

Also, since the call to IDeckShuffler.Shuffle(...) comes from inside the Deck, the deck has access to all hidden fields and encapsulated state. As such, you can expose the minimum details to the deck shuffler implementation instead of defaulting to passing a Deck. Instead, you may pass IEnumerable<Card> or something even less specific, as opposed to passing the whole data bag by default.

Anyway, the form of development you're inquiring about is basically procedural programming. As such, it's harder to hide information and encapsulate things. In performance-critical systems this can be an acceptable trade-off (group all data by type, then iterate over it using the manager 'process' functions = good cache coherency).

In general development, I stay away from this style of programming as it severely hinders my ability to manage complexity. Ayende had a good post about this a while back. Although he's talking about a data storage object and the operations that act on it, the principle is exactly the same -- the separation of data and the functions that act on that data and the problems therein.

Mark Simpson
While I agree with your alternative, I disagree that this is procedural programming. To me, it's much more like (object-namespaced) functional programming, as [yossale mentioned in his answer](http://stackoverflow.com/questions/3415667/is-there-some-literature-on-this-type-of-programming/3415747#3415747).
strager
@Mark Simpson: without posting a multiple comment of my experience with the methods and patterns you suggest, imagine a TicTacToe board as an array in which spaces can either be X or O or empty. Now expand this board structure to also support connect four, othello, etc... In my scenario, the board is just a board of arbitrary size with varying rules on how the board is affected during each move depending on the game being played.
NickLarsen
@Mark Simpson: as for your example with the 2 jokers next to each other, the same situation is true, and in fact cards are a great example... its just a deck of cards.
NickLarsen
+3  A: 

What you're doing is separating the data of the concept from the operations that act on that concept. What the system is from what the system does. This opens the door for many different scenarios, in which you change the behavior of the system by using different behavior classes. These behavior classes can also be reusable for different data classes. Many patterns address this problem, like Visitor, Command, Strategy, or Observer.

But there's something deeper at play here. Maybe we need another concept in our (mainstream) programming languages (or maybe just in our minds), that will allow us to separate, and reuse, these bahaviors.

The DCI architecture addresses these issues, and presents roles, or traits (pdf), as the fundamental unit of behavior and code reuse.

Jordão
+1. Very interesting articles / paradigm.
neuro
+3  A: 

This is the typical way most applications are laid out. I think of classes as forming a dichotomy - data container objects and strategy objects. Data container objects are carriers of information whilst strategies are encapsulations of various kinds of algorithms that can be applied on the data containers.

Command pattern is very close to the strategy pattern. Strategies also tend to manifest themselves as controllers, facades and the like.

Data Container objects manifest as entities, model objects, value objects, data transfer objects etc.

Gang of four is a good start but you might want to look into other classic design patterns treatises for further elaboration. Depending on your programming language predilection, you might want to consider more specific pattern books as well. Amazon has a ton of lists on design patterns.

I had talked about class dichotomy in this article.

raja kolluru
+1 Great article.
Jordão
@raja kolluru: that is a great article. I think the strategy pattern is the closest thing to answering my question that anyone has posted yet. I particularly like the stateless manner in which they are intended to be implemented. @Jordão's answer speaks about how this traversed deeper than any of the patterns listed on this thread, which I think also applies to the strategy pattern. In that I mean the strategy pattern is unregulated to the possibility of logic, whereas my concepts restrict functionality to interpretation, which I see as a more reusable concept.
NickLarsen
@raja kolluru: I just want to reiterate how wonderful your article is.
NickLarsen
Thanks Nick and Jordao. Appreciate it.
raja kolluru