Solved

Achitectural question - what is the best object model for this?

Posted on 2008-06-19
4
218 Views
Last Modified: 2013-11-24
I am deliberating about design issues on a project right now, and I'd be interested in hearing how someone else might tackle this.  Just as background, I am using EJB3, Hibernate, and JSF at the moment.  I am going to offer a somewhat abstract example that is actually pretty close to what I am trying to do.

I need to accommodate some object that represent different shapes.  They are defined as a series of points on a grid.  For example, there could be a square, a circle, or a polygon with an arbitrary number of sides.

In my mind, each shape could be pretty easily defined as a list of pairs of coordinates.  (Although the circle would need a single point and a radius, or something similar.)  Aside from that on exception, it's just an ordinal list of coordinate pairs.

Going forward - I am going to need to have a mechanism for each shape to be checked against a given point, to see if that point falls within any of the shapes on the grid.

My initial thoughts had been to have an abstract shape class to define the basic operations (get id, set id, getCollectionOfPoints, setCollectionOfPoints, etc.) and have each shape extend that class and provide it's own method to see if a set of provided coordinates fall within it's bounds.  A simple true or false response will be a sufficient answer, and the equation to check each shape type will never change.  The check method on the entity would then just be called from a session bean.

There is a part of me that doesn't like this idea though, because it feels like I am putting some degree of logic into an entity.  Is there a good way to accommodate these different types and still keep the actual work in a session bean?  (Please assume that entirely new shapes could be added to the system in the future, so rather than just adding a new entity with it's own method to check, the session bean would probably also need to be modified, which would be nice to avoid if possible.)

Does anyone have any thoughts or a better solution?
0
Comment
Question by:Perplexity
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
I am not very familiar with Java but the class organization that you are planning on looks good to me.

>Please assume that entirely new shapes could be added to the system in the future, so rather than just adding a
>new entity with it's own method to check, the session bean would probably also need to be modified, which would be
>nice to avoid if possible.
Again due to my unfamiliarity with Java I dont know what goes in a bean but if you are using run time polymorphism, there would be no code changes required apart from addition of the new shape class.

http://www.developer.com/tech/article.php/983081
0
 
LVL 13

Expert Comment

by:MicheleMarcon
Comment Utility
I suggest one single Shape class. BTW, are you looking to Polygon?

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Polygon.html

It looks like it has all your requirements. (contains(double x, double y) etc)
0
 
LVL 10

Accepted Solution

by:
Nellios earned 500 total points
Comment Utility
In order to specify the best practise for your project, we first need to answer a few questions.

a) Is the logic of the "contains" method common for multiple objects.
b) Is it likely to have the method "contains" change in the future?
c) Is there any possibility that you might want to change the logic of "contains" during runtime?

If you answer the above questions it will get clearer to you if it is really a bad practise to "couple" the contains logic to your entities.

If you decide to decouple those two, then you could choose the appropriate design pattern (maybe facade or decorator).


0
 

Author Closing Comment

by:Perplexity
Comment Utility
Thanks...I just needed a little help to think about it critically.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
either24  challenge 19 83
pre4 challenge 19 87
maxMirror challenge 10 88
strCount chalenge 3 50
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now