aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Extending Data Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Extending Data Class" Watch "Extending Data Class" New topic
Author

Extending Data Class

Chandru Ganesan
Ranch Hand

Joined: Feb 08, 2003
Posts: 37
Can somebody comment on the following design which extends Data class.
public interface DataClient {

// contains method declaration of only
// methods accessed by client app
}

public class RemoteData extends Data
implements DataClient {
// overrides lock() and unclock() methods
}
public class LocalData extends Data
Implements DataClient {
// contains nothing
}
The controller based on the mode gets the DataClient interface which is either RemoteData or LocalData
I decided to keep criteriaFind in the Data class

thanks
Chandru
Chandru Ganesan
Ranch Hand

Joined: Feb 08, 2003
Posts: 37
I added One more comment
I added the lock() and unlock() method to RemoteData class.
I remove lock()/unlock() from Data class.
DataClient interface does NOT define lock()/unlock() methods.
The Database connector class will either instantiate Data or RemoteData based on the mode.
I hihly appreciate your response
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Chandru Ganesan:
I remove lock()/unlock() from Data class.
DataClient interface does NOT define lock()/unlock() methods.
This looks very wrong. How do you propose to handle flight booking?
- Peter
Chandru Ganesan
Ranch Hand

Joined: Feb 08, 2003
Posts: 37
Peter,
Please ignore this design. I did this in a hurry.
I think in local mode no locking should be used. It is unnecessary overhead.
I modified my design and successfully tested my application as follows
Defined following 2 methods in Data class
a. criteriaFind(...)
b. reserverSeats(...)
I added reserveSeats() method because I dont want to expose update() method directly to user. reserveSeats() methods updates only the "Seats available" field
2. public interface DataClient {
// defines methods that clients can call
// this is a common interface for both
// local and network modes
// like criteriaFind(), reserveSeats()
}
2. public interface RemoteData
extends Remote, DataClient {
public vouid lock(...);
public void unlock(...);
}
3. public class RemoteDataImpl
extends UnicastRemoteObject
implements RemoteData {
....
}
4. public class DataAdapter
implements DataClient {
// for local communication only
// no locking
}
5. public class DataServerConnector {
public DataClient getRemoteObject(...)
public DataClient getLocalObject(.....)
}
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Chandru Ganesan:
Defined following 2 methods in Data class
a. criteriaFind(...)
b. reserveSeats(...)
Oh boy. This looks all wrong.
Now where did I have my boilerplate architecture diagram again... (rummage) ah, here.
GUI <==> Business Layer <==> Persistence
Adding a reserveSeats() method to your Data interface is mixing up the business layer API, which is
  • Abstract
  • Completely application specific
  • Coarse grained
  • with the persistence layer (database) API, which is
  • Low level
  • Completely generic
  • Fine grained
  • Can you see why this would be bad OO design? It would inhibit reuse as well, because what was originally a perfectly generic and reusable database class has now been ruined to become application-specific; the server code you write would also no longer be reusable beyond the cut-and-paste sense
    By requiring that you have a client-side object that implements "all the public methods in Data" (you should immediately think: "Interface!"), they are effectively saying "the persistence API should be exposed to the client application". That only makes sense if the business layer is part of the client application itself.
    Consider the alternative -- the business layer on the server. Then all you would and should expose on the server is a high-level, abstract API with methods such as reserveSeats and searchFlights. The client would not know or care about find(), criteriaFind(), lock(), unlock(), etc, or even that the backing store is a database at all. In an enterprise-type J2EE application this would be the way you'd probably do things, the business API would be implemented by a stateless session EJB. Behind the scenes, invisibly, it would use entity beans, JDO-based Java code, or something similar to interface with a relational database. You would never let the client talk to the database directly.
    This client-side Data requirement is there to force your design into a certain direction. One of the reasons for this is that there are a number of problems (such as locking and client identification) that Sun wants you to solve as part of the assignment. By taking the route you are going now, you are not only guilty of bad OO design but also of not at all addressing the design issues Sun wants you to address. I would expect this implementation to be failed (but I've been surprised before, so no guarantees). See also this thread and this thread.
    HTH
    - Peter
    [ February 10, 2003: Message edited by: Peter den Haan ]
    Chandru Ganesan
    Ranch Hand

    Joined: Feb 08, 2003
    Posts: 37
    I did realise that yesterday.
    This is what I did.
    Extended Data class to implement Lock and unlock methods for network mode.
    Provided empty method lock() and unlock() in Data for local mode. This does not lock any objects.
    Implemented criteriaFind() in Data class
    Implemented reserveSeats() in DataAdpater class.
    DataAdpater provides a thread safe wrapper to Data class. All database updates go thru the DataAdapter class.
    Now my design looks simple.
    What is your though on this?
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    I'm missing something. Does your networked Data subclass implement the contract for unlock() as specified in the javadoc?
    - Peter
    Chandru Ganesan
    Ranch Hand

    Joined: Feb 08, 2003
    Posts: 37
    Yes
    public class NetworkData extends Data {
    public void lock(int recNum) {
    // code to implement locking
    }
    public void unlock(int recNum) {
    // code for unlock
    }
    The DataAdpater class implements the DataClient interface.
    All DB updates to Data class are performed thru DataAdpater.
    DataAdpater instantiates Data class or NetworkData based on local or network mode
    In case of local mode the Data class' lock() and unlock() is just a dummy method call. I choose this option for simplicity.
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    Originally posted by Chandru Ganesan:
    Yes
    So where does it get its client ID identification from? Without such identification, you probably don't implement the API as documented in the javadoc.
    - Peter
    Chandru Ganesan
    Ranch Hand

    Joined: Feb 08, 2003
    Posts: 37
    The client id, record number, comes from the DataAdpater.
    The Model object stores recorddNumbers in an ArrayList. When a row from the JTable is selected the model object identfies the recordNumber. This is passed to the adapter.
    Hope this answers ur question.
    Chandru
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    Originally posted by Chandru Ganesan:
    The client id, record number, comes from the DataAdpater.
    Yes, but how does it end up at the server without you writing horrible code that is effectively likeAs I understand your design -- and my understanding may well be wrong -- you can't simply call lock(record, clientId) because Data doesn't implement it. You also can't simply call lock(record) because that wouldn't tell the server anything about the client ID. So that leaves you with awkward conditional code that polymorphism was designed to rid us of. Right or wrong?
    - Peter
    Chandru Ganesan
    Ranch Hand

    Joined: Feb 08, 2003
    Posts: 37
    Sorry for the communication gap here. Let me try to explain.
    public class NetworkData extends Data {
    public void lock(int recorNum) {
    ....
    }
    public void unlock(int recordNum) {
    ......
    }
    The recordNum comes from the Client application upon booking seats. When the user highlights a row the rowNo is translated to recordNum by the Model object.
    public class DataAdpater {
    public void bookSeats(int recordNum, int noOfSeats) {
    ....
    db.lock(recordNum);
    db.update(dataInfo);
    db.unlock(recordNum);

    }
    ....
    }
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    So now we're back where we were -- I don't see any way in this code that the server can identify which client holds which lock. And without that, how have you implemented unlock() as specified in the javadoc?
    - Peter
    PS. There is a way, but it's ugly. I'm not going to advocate that
    Chandru Ganesan
    Ranch Hand

    Joined: Feb 08, 2003
    Posts: 37
    I am not sure if you are aware of my locking implementation. Here it is.
    The NetworkData class uses a vector which stores all locked records. All client locks are placed in this static table. Before allocating a lock to a client it checks this table if the record number is already locked by some other client. If so, the thread blocks on this table.
    When NetworkData receives a request for unlock(recNum) it checks the table if the record number exists. If so it removes from this table.
    Chandru Ganesan
    Ranch Hand

    Joined: Feb 08, 2003
    Posts: 37
    Let me clarify that the lock table vector stores only the record number and not the record itself.
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    The javadoc states that unlock() should be ignored if the client doesn't hold the lock. Your implementation allows unlocking of locks held by other clients.
    - Peter
    Chandru Ganesan
    Ranch Hand

    Joined: Feb 08, 2003
    Posts: 37
    I see your point. But I dont see this happening in the application.
    for ex., in the DataAdpater
    public void reserveSeat(int recNum, int noOfSeats) {
    .....
    db.lock(recNum);
    db.update();
    db.unlock(recNum);
    ....
    }
    public void getRecord(recNum) {
    db.lock(recNum);
    db.getRecord();
    db.unlock(recNum);
    }

    In the above code unlock operation is performed only after the record has been successfully locked.
    Is it really necessary to keep a mapping between recNum and clientId?. If so what is the best strategy that I could adopt in this design.
    thanks
    Chandru
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    Originally posted by Chandru Ganesan:
    I see your point. But I dont see this happening in the application.
    Take another look at Data. Data has nothing to do with the Fly By Night project. It is a perfectly generic, reusable database class. So should your server be. Any answer that starts with "But the client..." is wrong. If it were right, why did you implement lock(-1), why didn't you junk that useless delete() method, why didn't you delete that completely superfluous extra constructor in Data?
    Regarding your question, search this forum for the word Connection and you'll get lots of hints.
    - Peter
    [ February 10, 2003: Message edited by: Peter den Haan ]
    Edward Wang
    Greenhorn

    Joined: Feb 10, 2003
    Posts: 2
    Hey guys,
    After I read your comments, I am confused.
    Could you please tell me why not access to Data class directly. I have finish coding. I am not sure about Local/network Mode. In my design plan. If I choose
    local mode, the client will use Data class directly. Otherwise it will communicate with Server, but at the Server site, Server will also use the Data class directly to finish some tasks
    Could you tell me where I am wrong? what is Adapter class? Where can I find this material. Thank you very much
    ED.
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    Ed, that's exactly what I did. That should be fine.
    - Peter
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Extending Data Class