Eman Yalpsid
Eman Yalpsid

Reputation: 121

Java MVC, Factory and Observer patterns together

How should an architecture(packages,classes) of an app that must use the mvc, factory and observer patterns, look like?

The problem is about solving a labyrinth that can have two types of implementantions for the Labyrinth(model) interface : one that useses a bidimensional array(matrix) and one that uses a sparse matrix(a list that stores only the occupied cells).

The View part should be responsible with describing the text representation of a labyrinth. It should contain the methods: get/setLabyrinth, toString. I should also have a LabyrinthSolver interface from which I will create two implementations: an interactive one(using the keyboard) and an automated one(lee's algorithm).

The interface LabyrinthObserver describes an observer of the labyrinth exploration. One observer will print on the screen information during the exploration. One observer will store the solutions found by the exploration in a data structure sorted by the length of the solution.

Finally, the mazes(matrixImpl or sparseImpl) should be created using a factory.

I am having problems when designing the view: Because I can store a labyrinth in 2 ways(bidimensional matrix or as a List of occupied cells) how should I create the text representation of a labyrinth now knowing how my labyrinth will be stored? I am thinking about creating an interface with the method

createTextRepresentation( ? labyrinth)

But what type should my labyrinth parameter be? I have this problem with all my other classes: solver, observer ? How can I use these 3 patterns together?

P.S.: I am not asking for the actual implementations for these classes, just a general idea of how should I combine them together.

Upvotes: 1

Views: 588

Answers (1)

Tanmay Patil
Tanmay Patil

Reputation: 7057

Since View class has setter for labyrinth, createTextRepresentation method does not need labyrinth as a parameter.

Your problem is not with designing View, it is with design of model. Labyrinth interface needs to be expressive enough that text representation can be created using that interface without knowing implementation details.

So code shapes should look like

public class LabyrinthFactory {
    public Labyrinth createLabyrinth() { ... }
}

public class LabyrinthView {
    private Labyrinth model; // Avoid getters and setters. Honor encapsulation.

    public LabyrinthView(Labyrinth model) {
        this.model = model;
    }

    public String createTextRepresentation() { ... }
}

Upvotes: 1

Related Questions