Design Patterns - Multiple Instance Patterns

multi-instance mode

The multi-instance pattern is relative to the single-instance pattern, and its idea is similar to the single-instance pattern, except that the multi-instance pattern can have multiple instances, and these instances are also created and managed by themselves; generally, each instance has its own special When using the attribute;

package com.black.singleton;

public class Multiton {

    private static Multiton m1 = new Multiton();
    private static Multiton m2 = new Multiton();

    // 私有构造方法
    private Multiton() {

    }

    public static Multiton getIntance(int i) {

        if (i == 1) {
            return m1;
        } else {
            return m2;
        }

    }

}

The number of instances in the multi-instance mode is not necessarily a limited number, this is just a flexible way of using the single-instance mode, and various combinations can be made according to the needs to meet your own business needs;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324789864&siteId=291194637