Hi,
I'm fresh in java (started learning in december, by myself because
already in "enterprise scope"), and i'm having a little trouble.
I have to make a website, in jsp, and currently working on the
business model.
I'd like to reuse the Objects i write for the frontend in the backend,
namely the ones in the frontend generally have only getters, and the
ones in the backend also have setters.
So for the moment what i do is having two packages, namely frontend
and backend, and the backend Objects all extend from the frontend
ones.
Subtenante, I've read your post a couple of times, and I'm afraid I
don't understand. So this post is really a request for more
information.
Firstly: what do the words, "Frontend," and, "Backend," mean to you?
I've a feeling that they are well-defined terms in your field, but I'm
unfamiliar with them. (I don't know JSP, so maybe they're well-known
JSP terms, in which case you can stop reading here as all my advice
will be irrelevant.)
Secondly, I presume the system you're working on is not gigantic:
there are no fixed rules for the number of classes per package, but 50
is generally an upper limit. I presume you're not dealing here with
more than 100 classes in these two packages.
Except that the frontend ones depend on each other : for example an
article will have an author Object in its attributes. But Author in
frontend has only getters.
An article has an author: what is it that you're designing? A
reference library of some sort?
I truly do not understand why a frontend (whatever that is) should
only have getters and a backend should only have setters.
When i do the backend version, it seems that if i directly extend from
the frontend class Article, the Author object used will be the one
from the frontend package also.
Re-use is great, of course. You want to maximise your functionality
per line of code. But I don't understand how inheriting from one
package to another automatically brings this benefit.
If one class in backend inherits from a class in frontend, and then
that backend class is instantiated, there is only one object
instantiated: there are not two objects created, one in the backend
and one in the frontend. You can consider the instantiated object as a
mash of the backend object and the frontend object: but it's just one
object, not two.
The only solution for the moment that i see is to create the Author
class as default (non public - non private), and write all the getters
setters for it. In the frontend I also write all the setters for the
Author in the Article class.
I don't understand this: perhaps you could supply more information, or
details of the specific classes?
But if I were you, and this is just a project to get you used to Java,
then I'd forget about re-use. Re-use opportunities will present
themselves unmistakably (you'll be writing the same methods in
different places) and when they present themselves, deal with them
then.
I'd be far, far more interested in designing a solution to the problem
you're trying to solve as simply as possible, but with the flexibility
to change it to any ideas for the future that you might have.
Also, just as classes are encapsulated behaviour, think of packages as
encapsulated behaviour of a rougher granularity. If you have a
reference library application (as guessed) then you'll probably have
the following funtionality:
- Create a library.
- Add a work (with author, and perhaps cross-reference to other
works).
- Delete a work.
- Search for a work (or all works by an author, or any other
attribute).
- Save library.
These are all rough-grained behaviours. And though I wouldn't put each
in its own package (as Wojtek does - and feel free to chose his
approach), I'd imagine that you'd have the following packages
(presuming you're using Model-View-Controller, which you're maybe not
using, but it's a good way to separate the core functionality from the
way this is presented to the user, and you did say that you're working
on the, "Business model," which would be the model here):
com.subtenante.controller - parses commands from user and kicks off
execution in the model.
com.subtenante.model - holding package for main functionality.
com.subtenante.model.library - holds main functionality for the
library
com.subtenante.view - holding package for the presentation of your
library
com.subtenante.view.gui - GUI specifics for your library (if any).
com.subtenante.view.console - text specifics for your library.
Of coures, using JSP limits your view (you almost certainly won't have
a console view) but it demonstrates how packages should hold related
behaviours.
This also reinforces my lack of comprehension of your frontend and
backend.
And that com.subtenante.model.library will be further sub-packaged
depending on the application you're designing. It could be guessed
further that a major part of your library could be the search you use
to interrogate the library, and so maybe the library would have a
further sub-package:
com.subtenante.model.library.search
This is especially true if you could consider using a totally
different search engine in future.
Also, perhaps the indexing you use is a substantial piece of behaviour
that could be replaced by a different system of indexiing in future;
in this case you'd probably also have:
com.subtenante.model.library.index
If you could also envisage some sort of authentication whereby only
certain users could view the works of certain authors (ok, maybe a bit
Nazi, but you get the point) then you might want to allow you users to
log-on and base authentication on them, in which case you might have a
slew of sub-packages, beginning with:
com.subtenante.model.library.user
com.subtenante.model.library.security
And what if you want to charge people for use of your libary? Well,
then have:
com.subtenante.model.user.credit
com.subtenante.model.user.restrict
com.subtenante.model.user.history
Again, the credit sub-package will contain all that behaviour that
checks the user's back-account to ensure that he has enough money to
view the work he seeks. The history sub-package will hold the
functionality to show all the works the user has ever viewed.
These are, of course, just guesses, but the intent is to show that the
two packages frontend and backend don't really describe what those
packages do (outside those expert circles that understand those
terms).
Another way to look at it is this: if you have a requirement to change
something in your system in a few months time, will the person
implementing the change have a good idea where to look just based on
the package names?
Using MVC (sort of) as described above, the programmer will know that
a change to the GUI will not affect the com.subtenante.model sub-
packages so much.
Whether a GUI change will impact frontend or backend more, I have no
idea.
Then, when i go to the backend, i can extend and add the setters for
the Author, which are available.
But i'm not sure of this design. Anybody have suggestions on this kind
of problem ? A name of pattern to throw me i could dive into ?
Thanks.
..ed