其他分享
首页 > 其他分享> > 设计模式之享元模式

设计模式之享元模式

作者:互联网

0x01.定义与类型

在这里插入图片描述

/**
 * 享元对象接口
 */
public interface Flyweight {
    void operation(String extrinsicState);
}

/**
 * 享元对象工厂类,享元类
 */
public final class FlyweightFactory {

    /**
     * 享元类容器
     */
    private static Map<String, Flyweight> flyweights = new HashMap<>();

    public static Flyweight getFlyweight (String key) {
        if (flyweights.containsKey(key)) {
            return flyweights.get(key);
        } else {
            Flyweight flyweight = new ConcreteFlyweight(key);
            flyweights.put(key, flyweight);
            return flyweight;
        }
    }

    public static int size () {
        return flyweights.size();
    }
}

/**
 * 可以被共享的对象
 */
public class ConcreteFlyweight implements Flyweight{

    private String intrinsicState;

    public ConcreteFlyweight(String intrinsicState) {
        this.intrinsicState = intrinsicState;
    }

    @Override
    public void operation(String extrinsicState) {
        System.out.println(this.intrinsicState);
        System.out.println(extrinsicState);
    }
}


/**
 * 不被共享的对象
 */
public class UnsharedConcreteFlyweight implements Flyweight {

    private String allState;

    public UnsharedConcreteFlyweight(String allState) {
        this.allState = allState;
    }

    @Override
    public void operation(String extrinsicState) {
        System.out.println(this.allState);
        System.out.println(extrinsicState);
    }
}

/**
 * 应用与测试
 */
public class Test {

    public static void main(String[] args) {

        Stream.of("1", "1", "2", "2", "3").forEach(key -> {
            Flyweight flyweight = FlyweightFactory.getFlyweight(key);
            flyweight.operation("测试" + key);
        });

        System.out.println("size: " + FlyweightFactory.size());
    }
}
1---:测试1
1---:测试1
2---:测试2
2---:测试2
3---:测试3
size: 3

0x02.适用场景

0x03.优点

0x04.缺点

0x05.模式样例

假设一个公司中的每个部门的部门经理都要进行汇报不止一次

/**
 * 员工接口
 */
public interface Employee {
    void report();
}

/**
 * 员工工厂
 */
public class EmployeeFactory {

    private static final Map<String, Employee>  EMPLOYEE_MAP = new HashMap<>();

    public static Employee getManager(String department) {
        Manager manager = (Manager) EMPLOYEE_MAP.get(department);

        if (manager == null) {
            manager = new Manager(department);
            System.out.println("创建部门经理:" + department);
            String reportContent = department + "部门汇报:此次报告内容是。。。";
            manager.setReportContent(reportContent);
            System.out.println("创建报告: " + reportContent);
            EMPLOYEE_MAP.put(department, manager);
        }
        return manager;
    }
}

/**
 * 部门经理
 */
public class Manager implements Employee {
    /**
     * 内部状态
     */
    private String title = "部门经理";

    /**
     * 外部状态,需要在应用层引入,就是外部状态
     */
    private String department;

    private String reportContent;

    public Manager(String department) {
        this.department = department;
    }

    public void setReportContent(String reportContent) {
        this.reportContent = reportContent;
    }

    @Override
    public void report() {
        System.out.println(reportContent);
    }

}
/**
 * 测试与应用
 */
public class Test {

    private static final String[] departments = {"RD", "QA", "PM", "BD"};

    /**
     * 要注意线程安全的问题
     */
    public static void main(String[] args) {
        for (int i = 0; i < 10; i ++) {
            String department = departments[(int) (Math.random() * departments.length)];
            Manager manager = (Manager) EmployeeFactory.getManager(department);
            manager.report();
        }

        System.out.println(EmployeeFactory.size());
    }
}
创建部门经理:BD
创建报告: BD部门汇报:此次报告内容是。。。
BD部门汇报:此次报告内容是。。。
创建部门经理:PM
创建报告: PM部门汇报:此次报告内容是。。。
PM部门汇报:此次报告内容是。。。
创建部门经理:QA
创建报告: QA部门汇报:此次报告内容是。。。
QA部门汇报:此次报告内容是。。。
QA部门汇报:此次报告内容是。。。
QA部门汇报:此次报告内容是。。。
BD部门汇报:此次报告内容是。。。
PM部门汇报:此次报告内容是。。。
创建部门经理:RD
创建报告: RD部门汇报:此次报告内容是。。。
RD部门汇报:此次报告内容是。。。
PM部门汇报:此次报告内容是。。。
PM部门汇报:此次报告内容是。。。
4

在这里插入图片描述

0x06.相关的设计模式

0x07.源码中的享元模式

0x08.源码地址

0x09.参考

标签:享元,String,对象,模式,Flyweight,department,设计模式,public
来源: https://blog.csdn.net/Artisters/article/details/98472722