I was talking about relationships between objects. In
a database you have foreing keys, but what does java offer?
Java's object model is at least as capable as foreign keys, if that's
what you mean. You can implement any data in a relational model, or in
Java's object model... but it will be far easier to understand in a Java
object model.
That's only natural. The relational model is an attempt to coerce real-
world concepts into a mathematical concept so that data can be expressed
in a canonical way that's not too dependent on the way it's used by the
application, and so that optimizations can be applied to retrieve
arbitrary sets of data quickly. And those are important requirements,
but they are necessarily abstracted from the application...
intentionally. The Java object model, on the other hand, is designed to
build abstractions of real-world concepts within a problem domain (that
is, in a way that's convenient for the kind of work that's going to be
done).
Naturally, it's far easier to design using Java objects (which are
intended for that purpose), and then figure out how that maps to your
universal data model in the database after the fact. As for the
database side, it can sometimes be very important to design correctly
from scratch there, too, if the database will be shared by a number of
software systems with different tasks and goals; but if the database
will be private to the application, it's often best to just let an O/R
mapper or EJB container generate a schema for you, and tune from there.
If you're starting to design a system using class diagrams
it's much more easier to implement it in a relational
database.
Well, no it's not. I don't know what else to say. A class diagram is
used specifically because it keeps common concerns together; everything
relevant about a type of object in a software system and its
relationships to other objects (within the context of the diagram
itself) is described on that diagram. You simply can't express that
using a relational database design tool (such as, say, an ERD).
*After* you know how the core software architecture is going to work,
then the task is to discover which small *subset* of your software
design is relevant to persistence, and design that in. In a typical
software system, far more classes will be transient than persistent, and
some classes may be relevant to persistence in some ways, but also have
transient aspects to them.
When you try to implement the same class diagram
in a language like Java, it'll result in a bunch
of struct-classes containing just datamembers and
getters and setters. I always see that in projects.
Perhaps you work on the wrong projects, or perhaps your projects are
designed by people who don't understand software architecture and think
that UML class diagrams are equivalent to ERDs. Unfortunately, UML
diagrams can only help good designers communicate their designs; no
diagramming technique can cause bad designers to produce good design.
Starting with ERDs, though, can only make it harder for software
designers to communicate designs of dynamic software... unless the only
point of the software is to create, store, and retrieve data from
database tables; and how often is that the case?
In a lot of projects this approach seems not that
practical. For example, if you designed some class
diagram with a Person, an Insurance and everything
works fine and the customer wants insurances for
animals which do have a relation with an Insurance
but with a Person too... it surely creates bloat.
Surely, the correct approach would be to use inheritance to define
several types of insurance, each type of which would know what it
applies to. Otherwise, you end up with something silly like keeping
track of the address for the property that's covered by a life insurance
policy. How to store this in the database is another matter, but the
correct abstraction is basically obvious.
Perhaps people start adding a class Animal and
a class Human which inherit stuff from an
InsuredThing-class.
I'm pretty sure no competent OO designer would ever inherit a class
called "Person" from a class called "InsuredThing". That's just dumb.
there are a lot of options and when requirements change
you have to adapt a lot of code.
[...]
When things need to be changed, the only
thing you have to change is an SQL-query.
Java and SQL are both languages. In either case, you have to update
code when the means of representing data has changed. It's quite
unlikely that only one SQL query would need to be changed. In fact,
unless the application is particularly trivial, you'll probably end up
updating some code in Java that generates SQL queries to run against the
database. You can do that yourself, or you can let an O/R mapper such
as EJB-CMP (or Hibernate or some JDO implementation or whatever) do it
for you. That's up to you, but don't make it sound so trivial. It only
is if your application is trivial.
If you have a table X in the database, this doesn't mean
that you have to program a class called X.
Of course not. It's certainly important to remember that applications
only need to deal with potentially simplified subsets of data from a
cross-application database. (If the database is application-private,
then you probably just generated a schema from your code anyway so the
question is moot.)
However, neither can you ignore the data complexity at the application
level. That application is probably responsible for a good bit of logic
that depends on various details of the data, and needs to present that
to the user. Your creation of a class with a few methods in it is a
little oversimplifying.
With basic OO-theory and 'good practice' EJB-programming
you just cannot write elegant code like this
I won't speak for the EJB environment (which is rather restrictive in
terms of what can be done), but you certainly can write far more elegant
(and what's more, functional) code than that.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation