设计模式(二十五)------23种设计模式(17):享元模式


使用频率:★☆☆☆☆

一、什么是享元模式

大量细粒度对象共享复用

二、补充说明

可以节约内存空间,提高系统的性能;

一个对象有内部和外部两种状态,内部状态是不变的,外部状态是可变的,把一个对象分成内部状态和外部状态,然后通过共享内部状态,达到节约内存空间的目的;

应用场景举例:一个文档中多次出现相同的图片;一篇文章中出现了很多重复的字符串;围棋的棋子(黑棋和白旗);

三、角色

抽象享元:一个接口或抽象类;

具体享元:内部状态为其成员属性,其实例为享元对象,可以共享;

享元工厂:生产享元对象,将具体享元对象存储在一个享元池中,享元池一般设计为一个存储“键值对”的集合;

客户端:使用享元对象

四、例子,JAVA实现

一个共享字符串的例子

抽象享元

复制代码
package com.pichen.dp.structuralpattern.flyweight;


public interface IFlyweight {

    //id为外部状态,不共享
    public void setId(int id);
}
复制代码

具体享元

复制代码
package com.pichen.dp.structuralpattern.flyweight;

public class ShareStr implements IFlyweight{
    //内部状态str作为成员变量,共享的.
    private String str;

    public ShareStr(String str) {
        this.str = str;
    }

    //id为外部状态,不共享
    @Override
    public void setId(int id) {
        System.out.println("str: " + str + "id:" + id);
    }
    
    

}
复制代码

享元工厂

复制代码
package com.pichen.dp.structuralpattern.flyweight;

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

public class FlyweightFactory {

    private Map<String, Object> strMap = new HashMap<String, Object>();
    
    public IFlyweight getInstance(String str){
        
        IFlyweight fly = (IFlyweight) strMap.get(str);
        
        if(fly == null){
            fly = new ShareStr(str);
            strMap.put(str, fly);
        }
        
        return fly;
    }
}
复制代码

客户端

复制代码
package com.pichen.dp.structuralpattern.flyweight;

public class Main {

    public static void main(String[] args) {
        FlyweightFactory factory = new FlyweightFactory();
        
        ShareStr hello = (ShareStr) factory.getInstance("hello");
        hello.setId(1);
        
        ShareStr hello2 = (ShareStr) factory.getInstance("hello");
        hello.setId(2);
        
        ShareStr test = (ShareStr) factory.getInstance("test");
        hello.setId(3);

        System.out.println(hello);
        System.out.println(hello2);
        System.out.println(hello.equals(hello2));
        
        System.out.println(test);
        System.out.println(hello.equals(test));
        
        
        
    }
}
复制代码

打印结果:

复制代码
str: helloid:1
str: helloid:2
str: helloid:3
com.pichen.dp.structuralpattern.flyweight.ShareStr@feb48
com.pichen.dp.structuralpattern.flyweight.ShareStr@feb48
true
com.pichen.dp.structuralpattern.flyweight.ShareStr@11ff436
false
复制代码

 


@Author       风一样的码农
@HomePageUrl  http://www.cnblogs.com/chenpi/ 
@Copyright       转载请注明出处,谢谢~ 

使用频率:★☆☆☆☆

一、什么是享元模式

大量细粒度对象共享复用

二、补充说明

可以节约内存空间,提高系统的性能;

一个对象有内部和外部两种状态,内部状态是不变的,外部状态是可变的,把一个对象分成内部状态和外部状态,然后通过共享内部状态,达到节约内存空间的目的;

应用场景举例:一个文档中多次出现相同的图片;一篇文章中出现了很多重复的字符串;围棋的棋子(黑棋和白旗);

三、角色

抽象享元:一个接口或抽象类;

具体享元:内部状态为其成员属性,其实例为享元对象,可以共享;

享元工厂:生产享元对象,将具体享元对象存储在一个享元池中,享元池一般设计为一个存储“键值对”的集合;

客户端:使用享元对象

四、例子,JAVA实现

一个共享字符串的例子

抽象享元

复制代码
package com.pichen.dp.structuralpattern.flyweight;


public interface IFlyweight {

    //id为外部状态,不共享
    public void setId(int id);
}
复制代码

具体享元

复制代码
package com.pichen.dp.structuralpattern.flyweight;

public class ShareStr implements IFlyweight{
    //内部状态str作为成员变量,共享的.
    private String str;

    public ShareStr(String str) {
        this.str = str;
    }

    //id为外部状态,不共享
    @Override
    public void setId(int id) {
        System.out.println("str: " + str + "id:" + id);
    }
    
    

}
复制代码

享元工厂

复制代码
package com.pichen.dp.structuralpattern.flyweight;

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

public class FlyweightFactory {

    private Map<String, Object> strMap = new HashMap<String, Object>();
    
    public IFlyweight getInstance(String str){
        
        IFlyweight fly = (IFlyweight) strMap.get(str);
        
        if(fly == null){
            fly = new ShareStr(str);
            strMap.put(str, fly);
        }
        
        return fly;
    }
}
复制代码

客户端

复制代码
package com.pichen.dp.structuralpattern.flyweight;

public class Main {

    public static void main(String[] args) {
        FlyweightFactory factory = new FlyweightFactory();
        
        ShareStr hello = (ShareStr) factory.getInstance("hello");
        hello.setId(1);
        
        ShareStr hello2 = (ShareStr) factory.getInstance("hello");
        hello.setId(2);
        
        ShareStr test = (ShareStr) factory.getInstance("test");
        hello.setId(3);

        System.out.println(hello);
        System.out.println(hello2);
        System.out.println(hello.equals(hello2));
        
        System.out.println(test);
        System.out.println(hello.equals(test));
        
        
        
    }
}
复制代码

打印结果:

复制代码
str: helloid:1
str: helloid:2
str: helloid:3
com.pichen.dp.structuralpattern.flyweight.ShareStr@feb48
com.pichen.dp.structuralpattern.flyweight.ShareStr@feb48
true
com.pichen.dp.structuralpattern.flyweight.ShareStr@11ff436
false
复制代码

 


@Author       风一样的码农
@HomePageUrl  http://www.cnblogs.com/chenpi/ 
@Copyright       转载请注明出处,谢谢~ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值