Layered Initialization
This is the class that begins the initialization.
This class is instantiated by the client class which passes
a string to be parsed into the constructor. The parsing of
that string will determine which concrete class will
be used by this service (the iSpecializedService variable).
Layered initialization refers to the fact that part of
the initilization will occur in this classes constructor
and the rest will occur in the ISpecializedService
constructor.
Calls to perform the service after initialization will
be handeld by this class and/or delegated to the
ISpecializedService class it contians.
(none of that is impemented in this example)
-----------------------------------------------------
class CommonService{
private ISpecializedService iSepecializedService;
private SpecializedServiceFactory specializedServiceFactory = new SpecializedServiceFactory();
private String protocol;
public CommonService(String aString){
//common logic/service
if(aString.startsWith("http:"))
protocol = "http";
else if(aString.startsWith("mailto:"))
protocol = "mailto";
else
throw new IllegalArgumentException();
iSepecializedService = specializedServiceFactory.createService(protocol,aString);
}
}
-----------------------------------------------------
This factory class uses the protocol argument to
determine which concrete class to return.
-----------------------------------------------------
class SpecializedServiceFactory{
public ISpecializedService createService(String protocol,String aString){
if(protocol.equals("http"))
return new HttpService(aString);
else if(protocol.equals("mailto"))
return new MailtoService(aString);
else
throw new IllegalArgumentException();
}
}
-----------------------------------------------------
The public interface used by the CommonService class.
-----------------------------------------------------
public interface ISpecializedService{}
-----------------------------------------------------
Concrete service class.
This class has different parsing rules then the
HttpService class.
-----------------------------------------------------
class MailtoService implements ISpecializedService{
public MailtoService(String aString){
if(!aString.endsWith("z"))
throw new IllegalArgumentException();
}
}
-----------------------------------------------------
Concrete service class.
This class has different parsing rules then the
MailtoService class.
-----------------------------------------------------
class HttpService implements ISpecializedService{
public HttpService(String aString){
if(!aString.endsWith("a"))
throw new IllegalArgumentException();
}
}
-----------------------------------------------------
This is a test stub to initialize the objects.
Switch the bad strings around and sys out will show
the exeptions thrown by the correct concrete class.
-----------------------------------------------------
class Test{
public static void main(String[] args){
System.out.println("class: Test, method: main");
//if protocol is http string must end with 'a'
//if protocol is mailto string must end with 'z'
String goodHttpString = "http://blahbalha";
String badHttpString = "http://blahbalh";
String goodMailtoString = "mailto://blahbalhaz";
String badMailtoString = "mailto://blahbalha";
String useme = badMailtoString;
CommonService commonService= new CommonService(useme);
}
}
-----------------------------------------------------
Here is a tar with the above java classes in it.
layeredInitialization.tar