• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question about SCJD

 
Yanxin Zhao
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi: everyone
I got a problem about the SCJD.
my instruction said that my Data.java must implments a interface they given me named DB. but I want to change something in this class. so I want to ask you that can I change the DB interface into another interface I needed ? of caurse just change a bit. such as I need throw another Exception(DBException), and do not let lock method return value(I do it in another class).
I am not sure could I do what I want to do ? the instrution neither told me I can change it, nor I can not change it.
 
Anton Golovin
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Yanxin Zhao:
Hi: everyone
I got a problem about the SCJD.
my instruction said that my Data.java must implments a interface they given me named DB. but I want to change something in this class. so I want to ask you that can I change the DB interface into another interface I needed ? of caurse just change a bit. such as I need throw another Exception(DBException), and do not let lock method return value(I do it in another class).
I am not sure could I do what I want to do ? the instrution neither told me I can change it, nor I can not change it.


If it says you must implement it and gives you want to implement, that is what they mean. I am doing the same assignment, I guess, probably a different version. But no, you cannot change the DB database, even though it would be convenient to be able to.
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Yanxin,
You cannot temper with or modify the interface given to you. You must implement it as is without any extra exceptions or change in the method signatures.
The good news is, you can add extra methods in your implementation class. You can add any public or private fields and methods.
If you want to do it in a perfect way, and take it a step higher, you can create an interface that extends the DB interface given to you by SUN. Then, you can implement your Data class implementing the new interface. They will check for the type. So you have to make sure that your Data class is a type of DB.
[ July 30, 2004: Message edited by: Hanna Habashy ]
 
Anton Golovin
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my assignment they gave me the specs with lock and unlock. So not I implement a second interface DBClient, which a a copy of the original but without lock and unlock. Design-wise, it looks bad.
 
Yanxin Zhao
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello:
what you said is really useful to me, thanks so much.
Yes, maybe I could not change which interface. I have a thought, I am not sure whether it is good or not. I want to do lock and unlock in another class named lockManeger too. so could I call the lockManeger in lock and unlock method in the class implemented DB.java directly ? as I have to implement these 2 method because of which class have been implemented DB.java.
I really do not know how to deal with this problem. I want to both lockManager and which class implemented DB.java are all work without any question.
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yanxin,
One solution to this issue is to create your LockManager class, and implements any business logic you want (lock(), unlock()), then use an instance of that class in the Data class, which implements the DB interface. Within the lock methods in the Data class, you can call the oppropriate methods in the lockManager instance.
This technique called composition and forwarding.
 
Yanxin Zhao
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,hanna. That's great, I think so.
 
Johnny Hunter
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's also called the Adapter pattern if I understand correctly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic