0

I have a database class with the following interface:

public Database {
    //returns false if p (its ID) is already available
    //otherwise adds p the the list and returns true
    public boolean create(Person p);

    //returns false if p (its ID) was not found in the list
    //replaces the available Person q (q.ID == p.ID) with p and returns true
    public boolean update(Person p);

    //returns false if p (its ID) was not found in the list
    //marks the available Person q (q.ID == p.ID) as inactive returns true
    public boolean remove(Person p);
}

The Person is immutable and can be identified by a unique id. I have a EditDialog which looks like this:

Editing a Person and the available Save/Chancel/Remove functionalities

I wanted to reuse my code and so the EditDialog is used for create a Person or update it. So this dialog is initialized either by a available Person instance (Edit-Operation) or by a new one (Create-Operation).

Because I learned that there is always an evil user I wanted to avoid the following scenario:

  • Click "Create Person" button
    • New Person is created in Database
    • EditDialog for new Person is displayed
  • Click "Chancel" button
    • Person has to be removed (= marked inactive) from Database
    • EditDialog is disposed
  • repeat

Which would lead to many inactive Persons in the Database. So I decided to only create the Person in the Database, if the user clicks the "Save" button. Which leads to two possible scenarios for the Listener of the "Save" button:

  1. New Person should be created in Database
  2. Person should be updaated in Database

So the code of the Listener looks like the following:

if(!database.create(p) {
    database.update(p);
}

Which is not so pretty in my opinion. The Long and the short of it is, should the Database automatically

  • update a Person if it's availabe at invokation of create(Person)?
  • create a Person if it's not yet available at invokation of update(Person)?

Or is there another solution/practice I didn't think about?

4
  • Learn about JPA, Hibernate, or ORMs (Object Relational Mapping) in general
    – SJuan76
    Jul 6, 2014 at 15:55
  • 1
    @SJuan76 I use Simple XML for object serialization. But I think this doesn't matter at a design / programming-practice question.
    – Thomas
    Jul 6, 2014 at 16:14
  • I think you should delegate the responsability to the open edit dialog button. If you open the edit dialog with a new person so it is a new person then your save button knows what it have to do. Same for the Edit (in fact) because if the user already exists the user clicks on a user from a list (my guest) and you open the edit dialog knowing that the person already exists. Jul 6, 2014 at 16:53
  • Hi @Thomas, your question is interesting and I'm tempted to answer, but it would take me about 3 hours to articulate everything! Have a read of this: en.wikipedia.org/wiki/Multitier_architecture . Also, here's another hint: database.persons.exists(p)
    – MetaFight
    Jul 6, 2014 at 21:26

1 Answer 1

1

Your UI should tell the database layer what operation to perform. In the UI, you will obviously have two buttons, one which says "Create new Person" and the other saying, "Edit this Person". So you already know what action needs to be performed.

When you invoke the UI Dialog, pass in this state (Add or Edit) to the dialog. Then, based on the value of this variable, the dialog can call either Database.create() or Database.update() when you click Save.

4
  • 1
    I would not ask anything to the database directly from the UI, since it's a clear violation of the MVC design pattern. Even if you don't intend to strictly respect it, consider the fact that you will probably want to access to the database without using the GUI. In OOP, I would also avoid to pass states such as Add or Edit. Prefer the use of inheritance or, generally even better, composition to specialise the GUI's behaviour. In this context, you could consider the ActionListener as the Strategy of a Strategy Design Pattern.
    – mgoeminne
    Jul 7, 2014 at 6:34
  • @mgoeminne oh absolutely.. When i said "talk to the database", i did not mean it to be direct - of course you go through the proper channels. :)
    – metacubed
    Jul 7, 2014 at 12:55
  • @mgoeminne however i do think a simple bool is sufficient here - using a Command pattern seems to be overkill for such a simple use case.
    – metacubed
    Jul 7, 2014 at 12:58
  • @metacubed thanks for your answer. You did not explain why the automatic invocation of update (or create) should be avoided. (Should it anyway?)
    – Thomas
    Jul 10, 2014 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.