Saturday, 24 August 2013

factory method pattern in java

factory method pattern in java

Is it a good practice to have objects specific to some implementation as
member variables in a factory class ? For e.g., in the code below, s1 and
s2 are required for constructing OneChannel and secondChannel objects
respectively. Is it a good practice to declare these as member variables
inside the factory ? If not, what can e the other alternative.
public class CommunicationChannelFactoryImpl {
@Autowired
SomeClass s1;
@Autowired
SomeOtherClass s2;
public CommunicationChannel getCommunicationChannel(String channel,
Map<String, String> channelProperties) {
if(channel.equals("ONE") {
return new OneChannel(s1);
}
if(channel.equals("TWO") {
return new SecondChannel(s2);
}
}
}
Please note that s1 ad s2 are singleton objects

No comments:

Post a Comment