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.

4/26/2017: Alright alright.

I don’t see myself catching up on the “post per day” thing anytime soon. Hey, who knows. Maybe everything will change in the next month. Feels like everything did THIS month.

Seriously. I read a book that rocked my stripey socks. (ooh, shit, which reminds me, I’m gonna need to add it to the list of books read this year. I’m definitely going to keep up with THAT at the very least.) The book was “Trading Beyond The Matrix” and… I….don’t think I’m ready to talk about it. I finished it a couple days ago and will be digesting it for quite some time. It set me back on my heels.

Started a batch of rice wine in a 2g brew bucket, which will be lovely. Fortunately rice wine comes out in 5 weeks, plus time to clarify. So it’s not like any of the meads I have that have stopped bubbling but still all look like swamp water *casts a suspicious eye at a cluster of 1 gallon bottles*

I also discovered Mass Effect: Andromeda. While I see what everybody was whining (and whining and whining) about, it’s fun as HELL. Took me over 100 hours to finish the game and as much of the side-questing as I cared to. It’s just so damn big. I thought I was heading to the final battle when I saw in the save screen “47% complete.”

I’m trying to decide whether to crack the seal on the work stories. The office is getting completely out of hand lately and it’s amazing to watch the events unfold from my seat as (largely) a spectator.

I probably will. I’ll see if I can’t cobble something together.