Singleton mode
Motivation is sometimes important to have only one instance of the class. For example, a system should have only one window management instance.
Singleton pattern is the simplest design pattern: the class is responsible for instantiating itself, ensuring that there is only one instance, and providing an entry to access this instance.
Purpose
1. Make sure that only one instance is created.
2. Provide an entry to access this instance.
Use final to ensure it is created once, and the private constructor ensures it is not instantiated. The getInstance method of public ensures that external access is possible. The following is the hungry mode:
public class Singleton { private static final Singleton instance = new Singleton(); private Singleton() {} public static Singleton getInstance() { return instance; } }
Lazy mode:
public class SingletonDemo { private static volatile SingletonDemo instance = null; private SingletonDemo() { } public static SingletonDemo getInstance() { if (instance == null) { synchronized (SingletonDemo .class){ if (instance == null) { instance = new SingletonDemo (); } } return instance; } }
Applicable scenarios and examples
1. Logger class prevents the creation of a Logger instance every time the log is printed.
2. Control class, generally there is only one control instance in the entire system.
Specific issues and implementations
1. Thread-safe, robust singleton mode should be thread-safe.
2. Lazy mode uses a double lock mechanism.
3. The Eagle mode uses static variables and is instantiated when the program is loaded, ensuring that there is only one instance.
4. Abstract factory and factory methods are usually designed as singleton patterns to ensure that there is only one factory.
5. When using serialization and deserialization, multiple instances will be created. Use the readResolve function to avoid this, but it is best not to use serialization.
public class Singleton implements Serializable { ... // This method is called immediately after an object of this class is deserialized. // This method returns the singleton instance. protected Object readResolve() { return getInstance(); } }
Key points
1. In multi-threaded programs, pay attention to data synchronization.
2. When serializing, use the readResolve method to return the instance to avoid multiple objects being created.
3. If loaded by multiple class loaders, multiple instances will be created.
Simple factory mode
The simple factory model of motivation is the basis and preliminary implementation of abstract factory and factory methods.
Purpose
1. Do not disclose the details of object instantiation to the client.
2. Create an object through a common interface.
accomplish
The implementation is very simple:
1. When a Client needs Product, it does not use new to create it, but provides a Product description to the Factory, allowing the Factory to provide a new Product.
2. Factory instantiates a Product to the Client.
3. Client uses abstract Product, without caring about the concrete implementation of Product.
Example
1. Drawing program for drawing shapes. The shape is the Product interface, and the triangles are the Concrete Product. We can create a factory and then create the product according to the customer's description. However, when adding new shapes, we need to modify the factory class.
Specific issues and implementations
1. When adding new products, the factory needs to be modified.
public class ProductFactory{ public Product createProduct(String ProductID){ if (id==ID1) return new OneProduct(); if (id==ID2) return new AnotherProduct(); ... // so on for the other Ids return null; //if the id doesn't have any of the expected values } ... }Generally, we use the if statement to judge the product description and instantiate different products. When there are new products, we need to add new judgments. This problem can be solved by abstract factory patterns.
Summarize
1. Use it only when you really need factory mode, otherwise it will just increase the complexity of the program. For example, when multiple objects have similar basic types, you can consider using simple factory mode to create objects uniformly.
2. Simple factories have more judgment branch statements, which violate the principle of opening and closing of modifications. Therefore, it is wise to use the simple factory mode for some fixed and simple programs, and use the abstract factory mode or factory method mode for some complex and frequently expanded programs.