Note: It is strongly recommended that you read the outline design of the OneMall project before reading this article

  • Design Pattern -OneMall Project Summary design
  • Design Patterns – Six design principles
  • Design pattern – singleton pattern

1. The demand

Since we need to connect to the API of each mall, we need to implement the API client of each mall in the Supplier module and provide a client for other modules to call. You only need one API client per mall. The client can then use the singleton pattern.

2. Implement

Code implementation:

public class JdApiSingleton { private static final JdApiSingleton self = new JdApiSingleton(); private JdApiSingleton() { } public static JdApiSingleton getJdApi() { return self; } public static GoodsDTO getGoodsInfo(String url, Map<String, Object> param) { String result = HttpClientUtil.get(url, param); return JSONObject.parseObject(result, GoodsDTO.class); } public static OrderDTO submitOrder(String url, Map<String, Object> param) { String result = HttpClientUtil.post(url, param); return JSONObject.parseObject(result, OrderDTO.class); }Copy the code

The singleton pattern is simpler, using a hunger-style design, privatizing constructors to ensure only one instance in the application, and then providing various other apis to implement the business logic.

3. The class diagram

The Singleton class diagram is relatively simple, and only one Singleton is required to implement the Singleton pattern. There is not just one way to implement the singleton pattern, but the hungry-handedly simplest implementation. If you are interested, you can search for other implementations. Since this article is about design patterns, we won’t discuss them in detail here.

4. The extension

Generating an instance in memory is simple, but in some cases we may need a fixed number of instances to improve application performance. In the case of multiple threads, for example, we might need to assign an instance to each thread. At this point we can generate a fixed number of instances in memory that match the size of the thread pool.

Code implementation:

Public class JdApiSingletonPool {private static final int MAX_NUM = 4; private static final List<JdApiSingletonPool> pool = new ArrayList<>(MAX_NUM); Private JdApiSingletonPool() {} static {for (int I = 0; i < MAX_NUM; i++) { pool.add(new JdApiSingletonPool()); }} public static JdApiSingletonPool getJdApiSingleton(int) public static jdapisingleton (int index) { if (index < 0 || index >= MAX_NUM) { return null; } return pool.get(index); } // other api ...Copy the code

We store all singletons by initializing a collection of singletons. This ensures that there are a fixed number of singleton instances in memory.

5. To summarize

Singleton Pattern: Ensures that there is only one instance of a class and that it is instantiated and made available to the entire system.

This definition has three meanings:

  1. There can be only one instance (private constructor)
  2. Self-instantiation (automatic initialization)
  3. Provide this instance to the entire system (provide a get method to get automatically initialized instances)

Advantages of the singleton pattern:

  1. There is only one instance, saving memory overhead. Reduced object creation and destruction time.
  2. Multiple occupancy of resources is avoided. Because there is only one instance, only one process can operate on the instance while the program is running.
  3. Global access. The singleton pattern requires that this instance be made available to the entire system, so it can be used to share certain resources.

Disadvantages of the singleton pattern:

  1. No interface, not easy to expand. If you need to add more apis, you need to modify the singleton pattern
  2. Conflicts with the single responsibility principle. A singleton instance generally does not implement only one business logic.

6. Example

  1. Java beans in the Spring container are the singleton pattern used. Each Bean is a singleton.
  2. JDBC connection objects in Java also use the singleton pattern. Each JDBC connection is a singleton.

Link to this article: zdran.com/20190818.ht…