[JavaScript][OOP/Constructors/Modules] Confusion regarding application design

I checked to make sure this hasn't been asked and I can't find anything (nor on SO), makes we wonder whether or not I'm over thinking the issue or approaching it in the wrong way but I shall continue!

I am creating a thing using leafletjs (similar to google maps if you're not familiar with leaflet) and I'm having some difficulty coming up with a design that adheres to OOP principles. Essentially there is a map with markers and there is a UI that toggles/filters these markers. The Application works fine, that isn't an issue but I'm struggling with the design under the hood.

Basically, I have two constructors one for the Map (which contains the map itself along with markers etc) and one for the UI. The UI needs to access the Map in order to show/hide markers yet I've been told this is 'bad', i.e. different objects shouldn't have direct access to each other or be too tightly coupled. But my UI is entirely dependent on the Map, if the map didn't exist it would be pointless even having a UI. Because of the way leaflet works i need to "Map > removeMarker()" etc. and I need my UI to toggle these things because the UI (a sidebar) is a separate component of the system and as such should be a separate object (as far as I can tell).

In short should I do something like this:

function MainMap(options, locations) {
    var that = this;
    this.options = options;
    this.locations = locations;
    this.map = new Map(); //passing reference to this scope
    this.ui = new Ui(that);  //passing reference to this scope
}

function Map() {
    // controls the map, maplayers, markers and popups
    // stores map, layers, markers, popups as objects inside Map
}

function Ui(parent) {
    this.parent = parent
    // controls the ui: toggling markers, other general ui functionality
    // Methods will needs access to data/objects stored in either MainMap or Map in order to add/remove them from the map
}

This is perhaps giving Ui access to too much (if I were to add anything else to MainMap they would have access to everything else in there too) so should I give Ui a reference to Map explicitly ()perhaps when the constructor is called? Doesn't this still leave my objects 'too tightly coupled'.

Sorry for the long post my question I guess is this: Am I trying to following the OOP guidelines too strictly and I should just pass a reference of Map too Ui? Or is there a more elegant way of doing this? (I also read that getters/setters/controller objects should be avoided as well). I've basically reached a point were every bit of 'advice' I've read or come across essentially prohibits my objects from talking to each other and I'm hella confused.

Posted May 29, 2016, 5:35 PM UTC

27
wgunther•

Generally, classes depending on other classes isn't a bad thing at all--it's necessary because with good OO design since quite a lot of things are abstracted to be a class (ideally). As long as the classes remain encapsulated and well structured, everything is fine.

However, the modern thought it that the UI layer should not directly interact with data; instead, it should be a very thin layer which just displays data from an outside source, and maybe notified the outside source when something changes in input fields. The largest reason for this is that if UI is coupled with functionality, it becomes increasingly hard to test and refactor functional code.

All of the above is equally true for the "model", which is your data's class representation, usually persistently stored in some database. If you put too much logic here, it's very hard to test, and it's very hard to change how things are represented.

There are two trendy patterns for this: MVC and MVVM. The former, the UI (the view) and model are controlled by a Controller, which owns the communication between the two and a lot of of the business logic (although some business logic is bound to get put in the model itself, or a layer above the model, if it makes sense to associate the logic more with the data itself). With MVVM, the controller is replaced by the view doing a little bit more, and usually relies on data binding to allow the view to interact a "view model", which replaces the controller.

1
evilpingwinOP•

Thanks for this. This thing about the Ui not interacting with data though. I've read this before but surely it needs to to interact in some way, in this example its just showing and hiding its not removing anything just changing visibility states, is this 'too much interaction' going by this principle? The UI can't only display data surely? The UI is the way in which the user manipulates the application, not simply a view of said application.

1
wgunther•

I've read this before but surely it needs to to interact in some way, in this example its just showing and hiding its not removing anything just changing visibility states, is this 'too much interaction' going by this principle?

So, yeah, in a "purely" MVC way, yes. The View should not be responsible for changing visual states on its own accord. It shouldn't say "Oh, now this array is different, so we gotta get rid of this table row". Instead, it would offer some public interface to manipulate its visual states, and the controller would recognize that something in the view needs to change, and use that public interface to change it. With MVVM, the view might do a little bit more, and say this visual state is tied to this data in the view model, and when that data changes, it will change the visual state itself, but this should be thin (the data it is looking at should be more or less an exact representation of what is displayed; the view shouldn't need to compute anything using it).

So, for instance, say we want to tie the text in a textbox with a field in some class. The view isn't looking at the underlying reason that field is changing, or deciding what it should say based on that field. With MVC, the controller would tell the view "hey, this textbox should say blah now"; with MVVC the text would be bound to some field of the view model, and the modification of that field would result in the textbox changing in the databinding layer (which should be transparent to the application developer).

1
jrandm•

My opinion: Yes, you're way too concerned about some "pure" approach to OOP.

From your little example, MainMap really seems like overkill to me - maybe if you wanted a neat 'MainMap.start() sort of entry point to the application?

Your UI has to have some way to interact with the data it's modeling, and that is almost always specific to the data, so don't stress too much about the UI being tightly coupled to whatever it's displaying (note that this is not the same as the Map being coupled to the UI). You might want to look into the MVC-family architectures to see if one makes sense to you, as you'll see lots of OO patterns there and one will probably fit with what you're doing anyway.

Perfect is the enemy of good (or working!), remember. If it makes sense to you (and/or others on your team), its easy to work on and maintain, then you're probably fine.

1
evilpingwinOP•

Thanks!

I'll tell you a little secret. I hate OOP. But it does have some nice elements and the entire world seems to use this style in a great many contexts so I tend to stick with it.

The MainMap didn't exist in the original iteration in honesty, I can easily pass the references I need without having that 'father' constructor I think.

1
jrandm•

Haha, I generally hate OOP too, but my disgust is probably a conglomeration of disgust from working with Java years ago (I hear it's gotten better, but XML and AbstractFactoryManagerFactory kinda shit sticks with you).

It is definitely important to learn, and I've since worked with a few applications where it really made a lot of sense (Entity-based systems in games/graphics mostly), but thankfully FP and saner multi-paradigm approaches are getting back in vogue. OOP's popularity was very a much an only-tool-a-hammer situation, I think.

EDIT:

Expanding on what I saw in your question in the other reply thread, the UI is the View and gets data or sends changes to a Controller (which you presumably don't have ATM) that actually interacts with the Map (your Model). This separation lets the View/UI only worry about how to format/display data and sending changes to one place and similarly the Map can evolve/change without worrying about breaking the UI. The controller sits in the middle and interfaces between them, often doing basic data transformations (like if either end gets upgraded) or sanity checks (not sending bad data to the model, limiting data coming out of the model to what the view needs). It's good for dividing the workload and keeping pieces abstracted, the UI and Map development can work separately, and any potentially breaking changes between them can be handled in the controller. For a smaller application or one intended to always be used together (ie: intentionally coupled), you can slide the Controller logic into the UI or Map as it makes sense. This can be messy, but oftentimes quicker/simpler depending upon the size of the actual project.

Last point: Testing the UI/Map both separately and together is easier when you have a standardizing interface, as every point of contact can easily have your test data injected in and you can confirm it has the correct output on the other side, and allows for monitoring each step of the application when they're fulling integrated. You can easily step into it and see which part is (slow/buggy/broken) with the clear isolation of components.

1
evilpingwinOP•

My philosophy is: Bigger the problem, bigger the hammer!

EDITFORYOUREDIT:

This is very helpful, thank you!

1