Question

I'm working on a android app, and I have a user class that has properties about the user. I also have a class that has views (i.e. TextViews) that shows the appropriate property on the screen, these are for GUI purposes. This GUI class needs the properties of the user class so I can access those too. So I am left with a decision to make that I am not sure. I want to pick the one that's better in programming practices. I don't want to merge all the properties including the GUI together in one class.

The way I see it is, I could either have the GUI class extend the user class or have the GUI class have a private global variable which holds the user object.

Does anyone know which is better and why?

Was it helpful?

Solution

Using encapsulation is better. It makes no sense for a GUI class to extend a 'user' class -- the two don't possess any sort of "is-a" relationship. It does make sense, however, for the GUI class to maintain a user object to work with.

In other words, it doesn't make sense to say a GUI object is a user object, but it does make sense to say a GUI object contains user a user object.

Lastly, you generally want to keep your front-end and back-end as separate and modular as possible. Having your GUI class extend your user class undermines this principle.

OTHER TIPS

I don't see any of your choices as a good programming practice according to MVC architecture (which is what Android uses).

Neither having the GUI class(part of UI layer) extend the User class(part of Model layer) or having in the GUI class a reference to a User is a good idea(IMHO). The interaction between the Model and the UI should be delegated to your Controller(the Activity)

As a side note, there are other languages like Objective-C were you could use a Category. With categories you could decorate your UI classes for doing this kind of things, without violating the architecture.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top