Which pattern to use?

Hi,

I want to create an API for the GUI piece of my application to hook up to. The GUI will basically query a database and display the results, it will also be used to edit database rows. Are there any patterns that I can use for this activity so that either the front or back end technologies can change without effecting my API and so I can change or add new queries without effecting the code?

Thank you in advance.

[437 byte] By [java_swing_dudea] at [2007-11-26 20:15:08]
# 1
The latest issue of the Java Developers Journal (JDJ) has a nice article on separation of logic. Take a peek at it and it might answer some of your questions. http://java.sys-con.com/read/325197_f.htmGood luck.
filestreama at 2007-7-9 23:21:41 > top of Java-index,Java Essentials,Java Programming...
# 2
please don't [url= http://forum.java.sun.com/thread.jspa?threadID=5143296&messageID=9529680#9529680]cross-post[/url]
georgemca at 2007-7-9 23:21:41 > top of Java-index,Java Essentials,Java Programming...
# 3
don't invent the wheel, use for example Spring framework to achieve this. http://www.springframework.org/docs/reference/spring-middle-tier.htmlThis framework is based on xml configuration files which could help you manage your application without having to modify your code.
java_2006a at 2007-7-9 23:21:41 > top of Java-index,Java Essentials,Java Programming...
# 4
I don't really want to use Spring at the moment, I just want to do this using J2SE if possible.
java_swing_dudea at 2007-7-9 23:21:41 > top of Java-index,Java Essentials,Java Programming...
# 5
> I don't really want to use Spring at the moment, I> just want to do this using J2SE if possible.how does Spring violate that wish?
georgemca at 2007-7-9 23:21:41 > top of Java-index,Java Essentials,Java Programming...
# 6
In essence, it doesn't but I really do want to introduce this technology at this point
java_swing_dudea at 2007-7-9 23:21:41 > top of Java-index,Java Essentials,Java Programming...
# 7
If you're not ready for Spring, how about Hibernate for the persistence?You have nothing to lose but the chains that bind you!
DrLaszloJamfa at 2007-7-9 23:21:41 > top of Java-index,Java Essentials,Java Programming...
# 8
I completely agree, although I have an extremely limited time to do this and simply don't have the time to learn these technologies now. I just need a good design for this initially and move to these technologies later
java_swing_dudea at 2007-7-9 23:21:41 > top of Java-index,Java Essentials,Java Programming...
# 9

> I completely agree, although I have an extremely

> limited time to do this and simply don't have the

> time to learn these technologies now. I just need a

> good design for this initially and move to these

> technologies later

no need for any particular patterns. simply following good OO principles is key here. keep your UI logic and your business logic and your data access logic separated. a good test is to write 2 interfaces, for example a GUI and a command-line client. if you find you have to tweak code to fit different interfaces, your logic has leaked. but basically, you want a layer of separation between the UI and the data layer. the middle layer mediates between the 2. a good starting point is get rid of any data access logic from your UI code. it shouldn't care less whether its data comes from a database, flat files or is just generated at random by your middle layer

georgemca at 2007-7-9 23:21:41 > top of Java-index,Java Essentials,Java Programming...
# 10
Thanks, can I use something like DAO in j2SE?
java_swing_dudea at 2007-7-9 23:21:42 > top of Java-index,Java Essentials,Java Programming...
# 11
> Thanks, can I use something like DAO in j2SE?yep. that would be a good start
georgemca at 2007-7-9 23:21:42 > top of Java-index,Java Essentials,Java Programming...
# 12
Is this the best route to follow?
java_swing_dudea at 2007-7-9 23:21:42 > top of Java-index,Java Essentials,Java Programming...
# 13
> Is this the best route to follow?What, "simply following good OO principles"?Certainly, "keep your UI logic and your business logic and your data access logic separate" is something any good solution will try to achieve.Message was edited by: DrLaszloJamf
DrLaszloJamfa at 2007-7-9 23:21:42 > top of Java-index,Java Essentials,Java Programming...
# 14
So are there any specific patterns I can use for this in J2SE?
java_swing_dudea at 2007-7-9 23:21:42 > top of Java-index,Java Essentials,Java Programming...
# 15
What, like MVC?
DrLaszloJamfa at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 16
I don't know, that is why I am asking lol! Which pattern(s) would you recommend? MVC / DOA / AN Other
java_swing_dudea at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 17
> I don't know, that is why I am asking lol! Which> pattern(s) would you recommend? MVC / DOA / AN OtherI recommend the SolutionLookingForAProblem pattern.~
yawmarka at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 18
I that your suggestion?
java_swing_dudea at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 19

> I that your suggestion?

My suggestion is that you're asking the wrong question with respect to patterns. Design patterns are repeatable solutions to a particular kind of problem. Once you've identified the problem, look for a pattern; not the other way around. Don't waste your time trying to shoehorn a bunch of patterns into your application; concentrate instead on designing your app so it makes sense.

That said, it's probably a good idea to be familiar with some common patterns, so you'll have a lexicon of solutions for problems you're likely to encounter. To that end, here are some excellent resources:

[url=http://www.patterndepot.com/put/8/JavaPatterns.htm]The Design Patterns Java Companion[/url]

[url=http://www.wickedlysmart.com/HeadFirst/HeadFirstDesignPatterns/HeadFirstPatternsIndex.html]Head First Design Patterns[/url]

[url=http://home.earthlink.net/~huston2/dp/patterns.html]Huston Design Patterns[/url]

[url=http://www.mindspring.com/~mgrand/pattern_synopses.htm]Overview of Design Patterns[/url]

~

yawmarka at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 20
This is very useful and I understand where you are coming from. I thought there may be a pattern I could use as a template for a solution.
java_swing_dudea at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 21
Patterns are not templates.
DrClapa at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 22
I fully understand the arguments against shoe-horning code into a pattern but I simply would like a pattern that would help solve my problem as a pointer to go about creating exactly what I need. Can anyone point me to a pattern that best suits my original post?
java_swing_dudea at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 23
MVC
MrPicklesa at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 24
If we follow the line of questioning here to its logical conclusion:Q: What pattern?A: No, template.Q: What template?A: No, framework.Q: What framework?A: Spring+Hibernate
DrLaszloJamfa at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 25
Thanks
java_swing_dudea at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 26
if you think a "pattern" will solve your problems AND you have to ask for a pattern instead of knowing which one to use, you're on the wrong track.
jwentinga at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 27
> I fully understand the arguments against shoe-horning code into a pattern...The problem isn't that you're shoe-horning code into a pattern; it's that you're trying to shoe-horn a pattern into your code.~
yawmarka at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 28

> > I fully understand the arguments against

> shoe-horning code into a pattern...

>

> The problem isn't that you're shoe-horning code into

> a pattern; it's that you're trying to shoe-horn a

> pattern into your code.

and that unfortunately, is an all-too-common phenomenon.

filestreama at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...
# 29

Well,

You may try Template method since you are actively looking for a "template" design pattern.

In an abstract,

class abstract DisplayRecordSet {

public result;

public void retrieveRecord() {

connectDB();

executeQuery();

closeDB();

}

abstract connectDB();

abstract closeDB();

abstract executeDB();

}

class ConcreteRecord extends DisplayRecordSet {

public connectDB() {

//connect to your DB

}

public closeDB() {

//close connection

}

public executeQuery() {

//run your query and set to the result variable

}

}

From above, you can add more queries as you like buy adding new subclasses.

Well, on the other hand, this may cause a lot of classes to be create when queries increases. But you can always apply another pattern to solve that issue.

Hope that helps.

-chiew boon

Chiew_Boona at 2007-7-21 17:53:52 > top of Java-index,Java Essentials,Java Programming...