This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Separated interface Pattern and factory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Separated interface Pattern and factory" Watch "Separated interface Pattern and factory" New topic
Author

Separated interface Pattern and factory

Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Hi i read the following about the separated interface pattern and had a few questions regarding its implementation for implementing a factory:


As you develop a system, you can improve the quality of its design by reducing the coupling between the system's parts. A good way to do this is to group the classes into packages and control the dependencies between them.You can then follow rules about how classes in one package can call classes in another - for example, one that says that classes in the domain layer may not call classes in the presentation package.
However, you might need to invoke methods that contradict the general dependency structure. If so, use Separated Interface to define an interface in one package but implement it in another. This way a client that needs the dependency to the interface can be completely unaware of the implementation. The Separated Interface provides a good plug point for Gateway (466).


Now suppose i have two packages domain and Service in my project. The domain contains all object classes (nouns) non of which are derived. The Service contains the factory class. The factory as you are aware is suppose to return instances of different classes in the form of an interface or something which all the classes have in common. Now the problem here is that non of the domain objects have any class in common. One way i am thinking of implementing this is to declare an interface in the service layer which contains a definition of the common domain object methods. The domain object classes will implement this interface defined in the service package thus the factory class will be able to return the interface. Is this a correct approach , any suggestion or ideas would be appreciated.. Thanks..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

I'm not really sure what you'd gain by doing this. Do you really need a factory class that can return all your various domain classes? I'd have expected the service package to be exposing specific functionality, which would be related to specific classes. Maybe I've misunderstood what you're trying to achieve.

Either way, I also don't think the quote you gave is particularly relevant to the situation you describe. It's talking about a situation where you need a dependency that's going in the "wrong" direction. Say your domain package needed a dependency on the service package (which otherwise wouldn't exist). Then you could reduce the impact by defining an interface in the domain package describing what was needed, and then implementing this in the service package. The domain package would no longer be coupled to the service package.
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Yeah. I guess i was a bit confused with the description. I was actually going over the book Patterns of Enterprise Applications and came across the Separated Interface pattern. The UML diagram of the pattern is at
http://www.martinfowler.com/eaaCatalog/separatedInterface.html
I wanted to know how classes customer and order are related to the interface. It states that the implementation of the interface is in another package , so what is the relation b/w customer and order class with the interface ??
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Rajesh Khan wrote:
...
The UML diagram of the pattern is at
http://www.martinfowler.com/eaaCatalog/separatedInterface.html
I wanted to know how classes customer and order are related to the interface. It states that the implementation of the interface is in another package , so what is the relation b/w customer and order class with the interface ??

The Customer and Order class make use of the methods in the interface and those methods are actually implemented in the Impl. The idea here is that the Customer and Order class will depend on the interface and not on its implementation thereby the implementation can be altered without breaking the other classes. So you get loosely coupled components. Moreover you are encapsulating the type information as well from the Customer or Order class.

Mohamed Sanaulla | My Blog
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Mohamed Sanaulla wrote:
Rajesh Khan wrote:
...
The UML diagram of the pattern is at
http://www.martinfowler.com/eaaCatalog/separatedInterface.html
I wanted to know how classes customer and order are related to the interface. It states that the implementation of the interface is in another package , so what is the relation b/w customer and order class with the interface ??

The Customer and Order class make use of the methods in the interface and those methods are actually implemented in the Impl. The idea here is that the Customer and Order class will depend on the interface and not on its implementation thereby the implementation can be altered without breaking the other classes. So you get loosely coupled components. Moreover you are encapsulating the type information as well from the Customer or Order class.

So are you saying that customer and Order class would implement it ?? Could you please give a simple example.. I would really appreciate it.. Thanks..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

No, he means that Customer and Order would have a reference to an instance of the interface, and would call methods from the interface.
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Matthew Brown wrote:No, he means that Customer and Order would have a reference to an instance of the interface, and would call methods from the interface.


Something like this ??

Is this it ?? or did you have something else in mind ?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

It would be something like

you could alternatively have IWork as part of the class, like:


You are making use of the methods defined in the interface in your Customer/Order class.
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Thanks for the example. I still have one final doubt the book states

"One of the awkward things about separate interfaces is how to instantiate the implementation. It usually requires knowledge of the implementation class.
The common approach is to use a separate factory object, where again there is a Separated Interface for the factory. "


How would i go about creating a factory class from the above example ?? I mean a factory returns an interface/class which is common to both classes however in the above case the Customer and the Order class do not have any common interface they are concrete classes which do not extend or implement any common class ??
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

You use the factory to create an instance of some implementation of your interface. Something like:


And then when you need an instance of the interface you can make use of the factory- WorkFactory.getWork(). Now your classes which depend on IWork really need not know which instance of Impl you are using- you might be using CustomerWorkImpl or OrderWorkImpl. There might be some other Impls as well or some new Impls added but your code which depends on this interface would work smoothly as long as the functionality the Impl provides is as expected by the class using it.
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Thanks . I cant emphasize how helpful that has been and the example is great this does make a lot of sense..
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Mohammed i had some questions regarding separated interface pattern and needed some clarification. I am closing this thread and starting a new one. Hope you could make contributions to that one too... Thanks,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Separated interface Pattern and factory
 
Similar Threads
56 J2EE FAQS
mock question from yahoo group,please give the answers
Longer Post - ALL IBM ICE EXAM Q's for UML
Answers to IBM 486 sample test
Passed SCJD with 137/155