GeeCON Prague 2014*
The moose likes OO, Patterns, UML and Refactoring and the fly likes which pattern to use?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "which pattern to use??" Watch "which pattern to use??" New topic
Author

which pattern to use??

Abdul Basit Munda
Greenhorn

Joined: Sep 07, 2005
Posts: 10
Hi,
I am having problem figuring out that , is there any pattern i can use for my problem to make nicer design. Here is my problem

My Application is based on components these are custom swing components. There is a MainComponent which contains subcomponents and each sub component can contain other subcomponents. Now suppose if i want the data in each component to be thrown out as an xml. what i do is i call method "toXML" on maincomponent and it does the traversing through subcomponents and call toXML method on each of them. All of these components are using interface AbstractComponent which contains method toXML.

So similarly if i want to set data on the componenet , data is taken from Xml, i will just call setData on maincomponent and then the maincomponent setData will be responsible for traversing through its subcomponent and calling setData method.

Umm i not sure ..if i m doing this way is a most efficient in terms of design? if you can recommend me any design pattern or you think this design is good??

Thanks a lot
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Well your traversing logic is called Chain of Responsibility.

Now, I do have an issue with creating subclasses to the main Swing classes. They did that here and makes it very difficult to maintain, fix problems, and add new functionality. The Swing API has plenty of classes to handle all UI situations.

Your case of needing XML representation should be seperated from UI classes. If you need to get XML representation of the values on a UI, then I suggest making a seperate class to do this. The Decorator pattern is one possible solution for you. Have a Decorator that takes a UI, and gets the values and tranforms them into XML.

My 2 cents.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Abdul Basit Munda
Greenhorn

Joined: Sep 07, 2005
Posts: 10
Thanks for the reply, well UI doesn't know really anything about XML, it take the valuse from its model and the model does all the parsing of XML and then tell the view by firing event that model changed so UI updates it self.

So i dont know if still it considered to be inappropriate design?

Thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Yes, I still would remove XML Technology form the Model. The model should only have the Model, the data of information. Not how to translate it into XML.

When I say UI, I mean the whole MVC parts for visual, model information, and controller-responding to actions.

Mark
Abdul Basit Munda
Greenhorn

Joined: Sep 07, 2005
Posts: 10
Ummmm however the only problem is each component have to parse differently , so suppose i have component A and component B , and i have two modesl model A and model B , similarly i have two different way of describing values for each of the component A and component B , so i was wondering , then i would need two differnt kind of parsers so if i have x components i will need x parsers??
is there any other better way to do it?


Thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Well there are other tools out there for mapping Data objects to XML. There is JAXB, Castor XML, XMLEncoder and XMLDecoder in Java 1.4.

Anyway, your model should not contain XML technology specific code. It needs to be seperated out to a different class. It can be in any way. I personally liked the Decorator way. There nothing stopping you from creating two Decorators for the same Model object, that will produce different XML Schema.

Mark
Raghubir Bose
Ranch Hand

Joined: Feb 18, 2006
Posts: 88
But Mark,
Doesn't this problem sound more like a visitor pattern?Definition wise visitor pattern says
"The purpose of the Visitor Pattern is to encapsulate an operation that you want to perform on the elements of a data structure. In this way, you can change the operation being performed on a structure without the need of changing the classes of the elements that you are operating on. "

The Object Graph that is desribed over here can be visited by the visitor and any method can be called on it??

--what do you say?


I will meet the fairy queen soon !
B.Sathish
Ranch Hand

Joined: Aug 18, 2005
Posts: 372
I agree with Bose, this is an example of Visitor pattern applied to a composite structure
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by R Bose:
But Mark,
Doesn't this problem sound more like a visitor pattern?Definition wise visitor pattern says
"The purpose of the Visitor Pattern is to encapsulate an operation that you want to perform on the elements of a data structure. In this way, you can change the operation being performed on a structure without the need of changing the classes of the elements that you are operating on. "

The Object Graph that is desribed over here can be visited by the visitor and any method can be called on it??

--what do you say?


No response from Mark on this?


ASCII silly question, Get a silly ANSI.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Visitor doesn't sound like a bad idea, but to be sure *I* had to work on the code. Applying patterns without feedback from the actual code is rather dangerous, in my opinion...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
GeeCON Prague 2014
 
subject: which pattern to use??