aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Strategy & Dependency Injection 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 "Strategy & Dependency Injection" Watch "Strategy & Dependency Injection" New topic
Author

Strategy & Dependency Injection

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Problem Description

I have 2 types of web artifacts, Image and Flash.
And their serialization format is different depending on the chosen format.

I am working within a dependency injection framework and I want to know
how to use the Strategy pattern in conjunction with it.



Issue

I plan to inject the serializers into the respective objects like this:

My question: Am I overdoing it with this design? Is there a more correct way to implementing this ? Thanks

Pho
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

I think I will put the different strategy into a factory. So your Artifact doesn't have to care about all the different ImageSerializer.

So in your Artifact's the method will looks like this:

And the above code can even go into an AbstractClass to remove duplication.

With that you have the injections done into the factory object. You can do case switch or Map, whatever you like.

This way your Artifact doesn't have to know about all available format to support. If you add new format, you just update the factory class. With your existing design, you have to update all your Artifact based classes.

Remember the DRY principle.
[ May 08, 2008: Message edited by: Wirianto Djunaidi ]
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Pho: Am I overdoing it with this design?

No, i dont think so. I think the Image class must be unaware of the algorithm used to serialize and this is one way of decoupling the data and behaviors.

Pho: Is there a more correct way to implementing this ?

I think you have not implemented the Strategy pattern correctly.
The problem is that you have made the Image aware of different Serializers. Since you have a Serializer interface, you can make the Image class aware only of the interface and not the implementations like JSON and XML.
In the serializeTO() method you must not pass the Format as you would have already decided upon the serializer based on the format.
You must directly call the serializers serialize method.

The code will look like this:


(Ideally, a strategy should always be of the above form.)

Now, the above will work if an image object while creation knows what it can be serialized into(otherwise you have to either pass it into the serializeTo method or keep on flipping the serializers!)

The other way can be to do an Inversion of control on this design and let the Serializer take the Image as the method argument.
(This way you can follow the Visitor pattern in a way. This will be like a JSON/XML serializer for all the artifacts.)


apigee, a better way to API!
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2906
In terms of processing are you more likely to save a single artifact in different formats or save a number of artifacts in the same format?

In term of code maintenance are you more likely to add more artifacts or more formats? Will each artifact have to support all formats?


"Don't succumb to the false authority of a tool or model. There is no substitute for thinking."
Andy Hunt, Pragmatic Thinking & Learning: Refactor Your Wetware p.41
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strategy & Dependency Injection
 
Similar Threads
testing domain-model in DDD (subclass vs. interface-impl)
strategy and template pattern
Creating Unit Tests (with mocks and shunts!)
Service Locator and Dependency Injection
Java web application which maintains a dedicated socket with a c++ daemon server