Search code examples
design-patternsoopfunctional-programming

Is there some literature on this type of programming?


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.


Solution

  • 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.