OU blog

Personal Blogs

neil

seperation of concerns

Visible to anyone in the world
Edited by Neil Anderson, Sunday, 4 Dec 2011, 19:15

Today I wanted to stay in, but my wife dragged me out to exercise, "her human right to make-up". We came home cold and penniless. So I did a bit of coding.

I had a problem, which I will explain thus: we have an implementation of a board [which is part of the Model] that we wish to have the View to advertise, and the Controller to know about. Not too difficult you'd think, we use an enum to tell the view what options are available. But then the Controller has to keep up with the changes...

Or does it? And even if it does do we want to hard-code the Controller?

[Board is an Abstract Class, neither the controller nor the Vew should care about what the thing actually is; but as a board maker how do I tell the others that there's a new one? And do they have to alter their code?]

My first-thought was that I could add some methods to an enum, didn't work. But you can wrap a Class around said enum thingees. Throw in some methods and we might be on the right track. 

What I came up with is a class that exposes an enum of what's available and copes with, the inevitable, switch. You send me a switch, I'll send you an instance of what you want if you send me a String.  

public class Boards {
    private enum AvailableBoards {
        ENGLISH,
        FRENCH
    }
    public static Board getBoard(String board) {
        AvailableBoards available = AvailableBoards.valueOf(board);
        Board brd = null;
        switch (available) {
            case ENGLISH:
                brd = new English();
                break;
            case FRENCH:
                brd = new French();
                break;
        }
        return brd;
    }
}

Not finished, indeed I have a burning tree and there are several returns that need to be crafted better.

Still I think that a Class like this [at the top of a package] is a self-documenting boundary for the package. It reminds me of the [rarely-seen] javascript .config.

Permalink Add your comment
Share post

Comments

New comment

Are you creating a crawler, a browser or a creature (in a game)?
neil

New comment

game

my wren

New comment

Have to admit when I first read your post the only part I understood was that you went out with your wife today; but I thought that you really like the computer stuff and no-one ever answers you when you post about it, so I thought I would try find out what you were talking about. I had to google enums and thingees first, then some other things, and I remembered there was a trial spore creature on my computer too. So hoped I'd narrowed it down to three things smile

 

Phillip John White

New comment

Hi Neil,

Here are my ranmdom initial thoughts, based on almost no understanding (just our fun with the Solitaire project during M257):

1. Does the View really need to advertise the available boards,...

2. or does it just expose a method which accepts certain values of the argument and returns an error if the value of the argument is not valid?

3. If we are really advertising, then don't we need to send a message with a list of available boards.

4. If 3. above would be unnecessary overkill, then perhaps 2. is fine.

5.In general, code only what is necessary now to pass the unit tests, don't code just-in-case for future.

6. If you can't write a test then don't code it and remove that superfluous code.

7. Later, if you think of a test, then code the test. Only then, code the implementation.

Phil

 

 

neil

New comment

@Phil

I didn't explain myself very well. I'll try to do better in future wink [Which may be a wee while as I'm going to need to have to think about and I'm thinking too much about solitaire!]

That said your points were well made and got me thinking.

Hope you did well on M208 btw.

arb

neo