the ClientFactory is a factory,but neither a Singleton Factory,nor a thread-safe factory.
At any point,when ClientFactory.getInstance().getClinet() is invoked,it will return a new
instance,so it is not absolutely a Singleton Factory.If we fix the method like this
private IClient iclient;
public IClient getClient() {
if ( iclient == null ){
iclient = new TestClient();
}
return iclient ;
}
Then the factory is a singleton factory,but it is not thread-safe.
Assume if there are more than one threads invoke getInstance,all of the threads will find
that the client factory instance is null,so they will construct the instance respectively,
and the problem is the same with the method getClient().
It is very easy to fix it,you can declare these two method as synchronized.
First of all,if you really want to use factory parrtern,don't forget to hidden the client's Constructor
private TestClient(){
}