4/30/2017: And now for something completely different…a Java question? (edit: whups.)

EDIT: Okay this is funny. I figure I’ll do the reddit thing and just make an additive edit to this. I’ll add it to the end so you get the full hilarity out of it…

For reasons I don’t even want to try and excuse right now, I’ve been working on getting my java chops up. But there are clearly a couple things that are more strange than even I expected.

Trying to do something and I’m clearly thinking about it in an unjava like way.

Hopefully I have distilled this example down to it’s simplest (but no simpler) form.

I have a Model object, currently an in-memory object store. We’ll say it contains “Person” objects.

    class Person {
          String firstName;
          String lastName;
    }

    class Model {
          public ArrayList people;
    }

I have a MenuRunner class that takes (what I’ve been calling) command objects and presents a menu of options to the user that they can select from. When the user selects an option, the instantiated command object needs to be executed against the model object as context.

    class MenuRunner {
        ArrayList commands;
        public void execute_command(int cmd) {
            commands.get(cmd).execute();
        }
    }

    interface Command {
        public int execute(Model model);
    }

    class CommandAddFoo {
        private Model context;  // I'd LIKE this to be "reference to Model object owned elsewhere"
        public int execute() {
            Person foo = new Person();
            foo.firstName = "Foo";
            foo.lastName  = "Barington";
            context.people.add(foo);
        }
    }

In languages that have pointers or indirect references, I’d create the command object with a reference/pointer to the model context object on which it was destined to operate. That way the MenuRunner has no proprietary knowledge of the model or the commands themselves.

BUT in Java, that seems to create a copy of the model object. So any modifications I make inside the execute method are pointless.

So what’s the proper javarific way to give the command object the context to do it’s job?

Am I just thinking about this wrong?


Turns out I was more right than I could’ve hoped and more wrong than I thought possible (in this particular instance. Generally I’m aware of how apocalyptically wrong I can be.) Everything about the approach below works wonderfully. It’s actually spectacularly Javarific. But I had a second command object, “CommandList” that was supposed to display all the items in the model. It decidedly did NOT. So a failure in the display led me to erroneously consider a deep architectural error.

Rule #1 people: Make SURE you’re seeing what you think you’re seeing.

I’m normally a big Test Driven Development guy. I’m shitty at it, but enthusiastic. I’ve been breaking into Java and started using IntelliJ (and oh my GOD do the Jet Brains guys write some amazing tools) and I hadn’t gotten around to setting up xUnit testing yet. That would absolutely have saved my ass.

Leave a Reply

Your email address will not be published. Required fields are marked *