Home > Back-end >  The singleton pattern and factory pattern together
The singleton pattern and factory pattern together

Time:09-29

Please give an example to explain, require that the singleton pattern and factory pattern together

CodePudding user response:

Do a piece of factory
Use on the server must consider thread-safe singleton factory, complete example is as follows: public class Test {private static Test instance=null; Private Test () {} synchronized public static Test getInstance () {if (instance==null) instance=new Test (); return instance; }}

CodePudding user response:

The singleton pattern is combined with a factory pattern:
Simple is the factory, there is only one instance is the global only a factory,
Another is the products of the factory, some is only one instance, or the production of all products is the only (piece of),
More pull egg is more than one factory, but production is the only global, (no matter how many factories, only one instance of the product is out)
  • Related