This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX:How to avoid hard coding? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX:How to avoid hard coding?" Watch "NX:How to avoid hard coding?" New topic
Author

NX:How to avoid hard coding?

Light Wan
Ranch Hand

Joined: May 15, 2002
Posts: 31
Hi all,
My assignment is URLyBird(1.3.1). The specification has already fixed the DBMain interface. How to pass the database schema to the client side without hard coding, I have no idea. I need help from you!

Regards
Xiaoguang


SCJP2
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You are required to implement DBMain, but you can also implement additional methods if you find them useful or necessary. For my assignment I defined a MetaData class for the info from the class header, and clients call a getMetaData() method on the server to retrieve this info.


"I'm not back." - Bill Harding, Twister
Light Wan
Ranch Hand

Joined: May 15, 2002
Posts: 31
For my assignment I defined a MetaData class for the info from the class header, and clients call a getMetaData() method on the server to retrieve this info

I use LocalDataAccess(for alone mode) and RemoteDataAccess(for network mode) to adapter the Data class. And following the OO principle - The Interface Segregation Principle (ISP), I reference the DBMain and Data in the above two classes with the following coding:

The specification required I should not change the DBMain interface. If reference the data using following coding:

This maybe violate the ISP principle. How to balance the advantages and disadvantages.
Regards, Xiaoguang
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
ISP suggests you try to reference these classes with an interface, not necessarily DBMain. You could create a new interface which extends DBMain and adds getMetaData(). That's what I did anyway. It's not changing DBMain, but creating another interface that adds to it. I tried to use Sun's interface only, but as far as I could tell it wasn't possible unless I hard-coded the metadata info - which I really didn't want to do. So extending Sun's interface was my best option, IMO.
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Jim,
You could create a new interface which extends DBMain and adds getMetaData(). That's what I did anyway.

That's was my first intention too but I was afraid of the automatic failure :
"Where this document uses the word "must" an absolute requirement is being described. (...) Portions of your submission will be analyzed by software; where a specific spelling or structure is required, even a slight deviation could result in automatic failure."
And under the title "Require Interface" :
"Your data access class must be called "Data.java", must be in a package called "suncertify.db", and must implement the following interface:" (called DBAccess in URLyBird 1.2).
So I feared that writing something like :
public interface ExtendedDBAccess extends DBAccess {...}
public class Data implements ExtendedDBAccess {...}
could fail depending on how their check software is written.
So all the methods I had to add were added in Data class directly.
Any idea on how that crual software does its job ? :-)
Regards,
Phil.
Light Wan
Ranch Hand

Joined: May 15, 2002
Posts: 31
Hi Jim,

Thank you very much. That's like a beautiful
Hi Philippe,
public interface ExtendedDBAccess extends DBAccess {...}
public class Data implements ExtendedDBAccess {...}

IMO, the above codes do not violate the "must" requirement - Data.java must implement the following interface(DBMain.java or DBAccess.java).
ExtendedDBAccess extends DBAccess and Data implements ExtendedDBAccess, so, Data implements DBAccess indirectly. The specification did not require Data should implements DBAccess directly.
Maybe i am wrong. Please append your opinion.

Regards, Xiaoguang
Light Wan
Ranch Hand

Joined: May 15, 2002
Posts: 31
Hi Jim,
Thank you very much. That's like a beautiful

I am sorry.
Hi Jim
Thank you very much, It's a perfect approach.
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Xiaoguang,
You are not wrong : Data is still a DBAccess (IS-A relationship kept), even if Data implements some ExtendedDBAccess instead.
I am hesitating simply because I don't know how they implemented their checking software. Is it worth while to take any risk there ?
Look at the following code :

Here is the output :

Regards,
Philippe.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Philippe and others,
I think an automated testing tool should still work even if you extended the interface and then implemented the extended interface.
But if you do want to remove the slightest chance of this causing a problem, then just create another class that extends Data class, and put your additional methods in it. For example:
  • Data class implements (DBMain.java or DBAccess.java).
  • ImprovedData extends Data implements (ImprovedDBMain.java or ImprovedDBAccess.java).

  • Regards, Andrew
    [ July 10, 2003: Message edited by: Andrew Monkhouse ]
    [ July 10, 2003: Message edited by: Andrew Monkhouse ]

    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    Vlad Rabkin
    Ranch Hand

    Joined: Jul 07, 2003
    Posts: 555
    Hi,
    I have the the same problem on deceding whether to hard-code or put it in Interface.
    I thought already what Phillipe said:
    The software will check the interface. Actually, nothing wrong should happen. My concern is why
    should we deliver this Schema to the client.
    The schema has (at lieast in my assignement) only following:
    Database Fild names,
    names length,
    field length.
    The only thing what I can use is field length (to validate input on the client).
    I beleive that is not a matter of server to tell the client how the field must be called. Can U imagine clients that call the fields the same the are called in Oracle database?!
    Since only field length is for the client usefull - does it make send to define on Server a special method for it???
    Any ideas?
    Regards,
    Vlad
    Philippe Maquet
    Bartender

    Joined: Jun 02, 2003
    Posts: 1872
    Hi Andrew,
    Many thanks for your solution. I love it and I'll (probably) adopt it.
    Vlad, the fact that you publish the schema or not is your design choice.
    Can U imagine clients that call the fields the same the are called in Oracle database?!

    Your server's clients probably not, but your Data class's clients (your server's business tier) for sure, if you want to avoid hardcoding field indexes. For example, how can you know in you book() method that "owner" is the 6th field if you don't implement in Data a method like "public int fieldIndex(String fieldName)" ?
    IMHO, the point is that the Data class must :
  • meet Sun's requirements as stated in the DBAccess interface, but also
  • meet your needs as the URLyBird application programmer


  • Just two examples taken from URLyBird 1.2.1 :
    1� In the DBAccess method description of "public long[] findByCriteria(String[] criteria", you don't find any clue on how to implement it (normal, it's just an interface). Sequential search ? Indexes on some fields to boost performance ? That's free choice, but if you choose - as I did - to implement indexes, you do need to add in Data somme method like "public void setIndexedFields(String fieldNames)" or the like, to tell Data which fields you want to index. Here I choose Data because it isn't a "data-access" method.
    2� In the "background" section of my instructions, it is stated that :
    They take bookings only within 48 hours of the start of room occupancy.
    Do I need to take that statement in consideration ? Well, it's in the specs (I consider that the whole "instructions.html" content are the specs), so I must do. As an application developer, I asked myself :
  • Does the database only contain bookable records (no past and no future further that 2 days) ?
  • Or do I need to "filter" the records to make sure they fall within the "bookable" range ?
  • Is is likely that that 2-days-range may change in the future ?


  • As Sun discourage us to ask questions, I am working on the assignment as if my IT manager :
  • was on vacations in South Patagonia,
  • with its mobile phone left home,
  • while the application must be running as he expects the day he comes back to work (my upload time).


  • So, still with my "application developer"' hat, I decide to wrap that uncertainty within some property(ies) and to implement that record filtering. But how to do that ?
    If I take the DBAccess interface "as-it-is" (findByCriteria does not support ranges), I must call it twice (it's a luck there are no more than two days in the range ), get two long arrays and keep their intersection. But as I had a chance to talk to the Data class writer (talking to oneself is good practice for any developer ), I changed my hat : range-search is definitely a Data functionality and a Data implementation will :
  • outperform any code written outside
  • be reusable

  • So I implemented in Data an overloaded method "public long[] findByCriteria(String[] criteria, String[] rangeCriteriaFrom, String[] rangeCriteriaTo)".
    Andrew, this is typically one more "data-access" method, so your solution to declare it in an ImprovedDBAccess interface implemented in an ImprovedData class extending Data is ... perfect ! No break in the requirements and support for extended functionality. Thanks again !
    Cheers,
    Phil.
    Vlad Rabkin
    Ranch Hand

    Joined: Jul 07, 2003
    Posts: 555
    Hi Philippe,
    Upss, I have totally bypassed that fact about 48 Hours...
    Thanks Philippe!
    Andrew, have U implemented this logic (with 48h) also?
    Vlad
    Vlad Rabkin
    Ranch Hand

    Joined: Jul 07, 2003
    Posts: 555
    Hi,
    I decided not to implement 48h. As Phillipe said, it is in a specification document, but is NOT PART OF REQUIREMENT. I am a "stupid developer" and it is not my business to make extra work and define business processes.
    My task is to prepare overall architecture for future enhancements. So I developed search mechanismus so that it 48h problem can be added easily in my code.
    What do you think about this approach?
    Have U all implemented last 48h hotel room search?
    Vlad
    Philippe Maquet
    Bartender

    Joined: Jun 02, 2003
    Posts: 1872
    Hi Vlad,
    I didn't say that ! The equation I believe in is :
    instructions.html == the specs == the requirements
    BTW, I don't think you are a
    "stupid developer"
    .
    Before you passed the SCJP you were a junior Java programmer. Since then you are somewhere between a "senior programmer" and a "junior developer" (which doesn't mean "stupid". The next step is this one, and I assert - from 17 years development experience - that the assignment we are working on is a senior-developer-level one.
    Let's put our humility in the cellar !
    Regards,
    Phil.
    Vlad Rabkin
    Ranch Hand

    Joined: Jul 07, 2003
    Posts: 555

    Well, Thank you for your compliment!
    Phillipe,
    I am afraid I need you help about Search approach,
    because after your mail got a bit confused and suppose I have taken some things too simple
    1) I don't use real DB names neither in Client, nor in Data class. I work only with the arrays of String from request and Fields length from Schema.
    I use a simple linear approach:
    I go throught each record and than compare it.
    I have defined special Finder class with following C-tor:
    public SimpleStringFinder(String[] criteria).
    and methods:
    public boolean isMatching(Object[] values)
    Since I cashed all database in a HashMap it works very fast and stable, so that I don't need to use indexed search approach.
    2)
    To make update I get String array for a record from the table and change element 6 (customer) and execute update. I have defined constant for the customer in my ClientProperties class. It is not perfect(it is card-coded), but I thought it is pretty Ok,
    because it actually doesn't matter what your hard-code: names of columns or its numbers.
    in JDBC for instance you can use following:
    - ResultSet.getString(int columnIndex)
    - ResultSet.getString(String columnName)
    I just use first approach.
    So, I don't have to use such methods like int getColumnIndex(String dbColumnName) and I don't
    to extend Data.
    Now, could U please help me understand the following:
    1) If we just forget about indexed search, where would you use the names of DB Columns : on the client, Data, or its extention and how? Can you give me a sample?
    2) Do U beleive the indexed search is needed when the whole database is cashed?
    2) What do you think specification means by saying we have to flexible search mechanism?
    Very many thanx,
    Vlad
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    Hi
    Vlad: Andrew, have U implemented this logic (with 48h) also?

    I completed the Fly By Night Services assignment, so I didnt have that requirement.
    From my experience as a developer, the client often expects you to implement anything mentioned in any part of the specifications (preamble included) and anything mentioned in meetings during discussion of the specifications. Based on this, I would tend to include the 48 hour rule.
    To correlate this to my assignment: there was a requirement that the user be able to search based on Origin and Destination. There was also an example showing a search on Carrier. So I decided to explicitly code so that the user was presented with searches on all three fields, even though only two were explicit. I certainly didnt loose any marks because of this.
    Regards, Andrew
    Philippe Maquet
    Bartender

    Joined: Jun 02, 2003
    Posts: 1872
    Hi Vlad,
    1) If we just forget about indexed search, where would you use the names of DB Columns : on the client, Data, or its extention and how? Can you give me a sample?
    Just in the business tier, to be able to access the column values array by column name. Working with constants works fine ... till the data format changes. So putting column indexes in variables seems better to me. Data knows its meta-data information. Why not simply publish it ? And what about fields lengths ? Constants too ?

    2) Do U beleive the indexed search is needed when the whole database is cashed?

    I really don't know, because we have no idea about :
  • the database size
  • how the database will grow up in the future (do they archive past booked records, "cleaning" the live work db, or not ?)
  • how many concurrent transactions may we expect ? (CSRs today, the web tomorrow)


  • Si I prefer to make my Data class as scalable as possible.
    3) What do you think specification means by saying we have to flexible search mechanism?

    I think that the findByCriteria method as declared in DBAccess is what they call a "a flexible search mechanism".
    Cheers,
    Philippe.
    Vlad Rabkin
    Ranch Hand

    Joined: Jul 07, 2003
    Posts: 555
    Hi Andrew,
    Hi Phillipe,
    Working with constants works fine ... till the data format changes.

    Well the same I can say about names: column names can be changed also. Ok, I agree:
    Honestly saying I never use in JDBC getString(String columnName), because it makes the code not readable...

    Phillipe, you told me two times that U use column names in business layer. In my case it is Data (implementation of DB ) and DataRemote (RemoteImplementation of Data). So, it means in your GUI Client you don't use column names, U use also just an array of String. Is it correct?

    Vlad
    Vlad Rabkin
    Ranch Hand

    Joined: Jul 07, 2003
    Posts: 555
    Hi Phillipe,
    Thinking about idea to re-index arrays elements, to avoid problems while database structure changed I have got new idea:
    1) To do everything what you that in Data class.
    2) Add additional methods in the class which extends Data:
    int[] find(Record[] criteria)
    Record read(recNo)
    update (int recNo, Record record, long cookie)
    and so on.
    The Record class will contain all fiels (name, location and so on).
    In this case the client will have nothing to do with structure of Database. It will have a clear structure of request without need to work with confusing arrays.
    And such methods like getindexbyName() will be just internally used in Data or the class which extends it.
    What do you think about?
    Vlad
    pepito grillo
    Greenhorn

    Joined: Jul 11, 2003
    Posts: 8
    Hi Vlad,
    It's a pleasure:
    1)No, I didn't extended the interface but documented the need to create a "clone" one capable to throw RemoteExceptions. This "RemoteDB" interface was implemented by the object published in the RMI registry. A single instance of the DB implementing class handles all the work for the whole system, including local and/or network requests.
    2)I didn't exactly hard-coded fields. Instead of that, the whole solution was designed as a generic framework (as generic RDBMS working with table definitions) for which some specific classes(the one that strictly take care of the assignment file/table rules) handle specific operations such as client searches, and data validation (even if it was strictly not a requirement), using for that, yes at the end, some public constants.
    But be carefull, as many know now, too much (or strange) complexity is not always worth the pain
    Hope it helped.
    Best regards,
    Gael
    Vlad Rabkin
    Ranch Hand

    Joined: Jul 07, 2003
    Posts: 555
    Hi Gael,
    Thanx for the answer. It helps a lot.
    Vlad
    Mark Spritzler
    ranger
    Sheriff

    Joined: Feb 05, 2001
    Posts: 17249
        
        6

    Only do easy assignments.
    Mark


    Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
    How to Ask Questions the Smart Way FAQ
     
    Consider Paul's rocket mass heater.
     
    subject: NX:How to avoid hard coding?
     
    Similar Threads
    Auto setting JavaDoc in Eclipse 3.2
    JPanel, no sytax error but error output
    change path
    servlet can't recognize httpSession
    magic cookie ?