设计模式之享元模式

242 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

  • 个人简介:微信公众号关注:SteveCode。为您分享更多的知识学术。生于忧患死于安乐
  • 专注Java技术干货分享,Java基础技术、数据结构、相关工具、Spring全家桶、intellij idea......

享元 模式

在这里插入图片描述

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

结构

在这里插入图片描述

/*******************************************************************************
 * Package: com.example.demo.design.enjoy
 * Type:    Test
 * Date:    2022-04-09 21:20
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.enjoy;

/**
 * 功能描述:
 *
 * @author Songxianyang
 * @date 2022-04-09 21:20
 */
public class Test {
    public static void main(String[] args) {
        WebsiteFactory factory = new WebsiteFactory();
        Website websiteWX = factory.getWebsite("微信公众号");
        websiteWX.use(new User("SteveCode"));
    
        Website websiteWX1 = factory.getWebsite("微信公众号");
        websiteWX1.use(new User("SteveCode"));
    
        Website websiteCSDN = factory.getWebsite("CSDN");
        websiteCSDN.use(new User("SteveCode."));
        
        System.out.println(factory.size());
    }
}
/*******************************************************************************
 * Package: com.example.demo.design.enjoy
 * Type:    User
 * Date:    2022-04-09 21:07
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.enjoy;

import lombok.Data;

/**
 * 功能描述: 外部状态 :用户类
 * 一直发生变化的对象 :外部状态
 *
 * @author Songxianyang
 * @date 2022-04-09 21:07
 */
@Data
public class User {
    private String name;
    
    public User(String name) {
        this.name = name;
    }
}
/*******************************************************************************
 * Package: com.example.demo.design.enjoy
 * Type:    Website
 * Date:    2022-04-09 21:08
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.enjoy;

/**
 * 功能描述:网站 具体实现
 *
 * @author Songxianyang
 * @date 2022-04-09 21:08
 */
public class Website extends WebsiteAbstract {
    
    private String type;
    
    public Website(String type) {
        this.type = type;
    }
    
    @Override
    public void use(User user) {
        System.out.println("当前网站正在使用中。。。。"+"该类型是:"+type+",用户:"+user.getName()+"-体验良好");
    }
}
/*******************************************************************************
 * Package: com.example.demo.design.enjoy
 * Type:    WebsiteAbstract
 * Date:    2022-04-09 21:06
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.enjoy;

/**
 * 功能描述: 享元模式 :共享 对象
 *  网站案例
 *  抽象类
 * @author Songxianyang
 * @date 2022-04-09 21:06
 */
public abstract class WebsiteAbstract {
    /**
     * 网站的使用
     */
    public abstract void use(User user);
    
}

/*******************************************************************************
 * Package: com.example.demo.design.enjoy
 * Type:    WebsiteFactory
 * Date:    2022-04-09 21:13
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.enjoy;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 功能描述: 网站工厂
 *
 * @author Songxianyang
 * @date 2022-04-09 21:13
 */
public class WebsiteFactory {
    private Map<String, Website> map = new HashMap<>();
    
    public Website getWebsite(String type) {
        //如果没有该类型就创建
        if (Objects.isNull(map.get(type))) {
            map.put(type,new Website(type));
           return map.get(type);
        }
        // 有就直接使用
        return map.get(type);
    }
    
    public int size() {
       return map.size();
    }
}

执行结果:

在这里插入图片描述

Integer 源码分析享元模式

/*******************************************************************************
 * Package: com.example.demo.jdk
 * Type:    Enjoy
 * Date:    2022-04-09 21:58
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.jdk;

/**
 * 功能描述:
 *
 * @author Songxianyang
 * @date 2022-04-09 21:58
 */
public class Enjoy {
    public static void main(String[] args) {
        Integer integer1 = Integer.valueOf(19);
        Integer integer12 = Integer.valueOf(19);
        Integer integer2 = Integer.valueOf(200);
        Integer integer22 = Integer.valueOf(200);
    
        System.out.println(integer1 == integer12);
        System.out.println(integer2 == integer22); 
    }
// 分析 先从cache中找对应的 池中的数据。数据范围-128到127 
// 否则就创建新的对象
public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
}

在这里插入图片描述