File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which pattern to use??

 
Abdul Basit Munda
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abdul Basit Munda
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
B.Sathish
Ranch Hand
Posts: 372
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Bose, this is an example of Visitor pattern applied to a composite structure
 
Stuart Ash
Ranch Hand
Posts: 637
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic