自学内容网 自学内容网

设计模式笔记

1、享元模式

interface Flyweight
{
    void operation(String extrinsicState);
}
enum ConcreteFlyweight1 implements Flyweight
{
    INSTANCE("INTRINSIC STATE 1");
    private String intrinsicState;
    private ConcreteFlyweight1(String intrinsicState)
    {
        this.intrinsicState = intrinsicState;
    }

    @Override
    public void operation(String extrinsicState)
    {
        System.out.println("具体享元操作");
        System.out.println("内部状态:"+intrinsicState);
        System.out.println("外部状态:"+extrinsicState);
    }
}

enum ConcreteFlyweight2 implements Flyweight
{
    INSTANCE("INTRINSIC STATE 2");
    private String intrinsicState;
    private ConcreteFlyweight2(String intrinsicState)
    {
        this.intrinsicState = intrinsicState;
    }

    @Override
    public void operation(String extrinsicState)
    {
        System.out.println("具体享元操作");
        System.out.println("内部状态:"+intrinsicState);
        System.out.println("外部状态:"+extrinsicState);
    }
}
class UnsharedConcreteFlyweight1 implements Flyweight
{
    @Override
    public void operation(String extrinsicState)
    {
        System.out.println("非共享具体享元操作");
        System.out.println("外部状态:"+extrinsicState);
    }
}

class UnsharedConcreteFlyweight2 implements Flyweight
{
    @Override
    public void operation(String extrinsicState)
    {
        System.out.println("非共享具体享元操作");
        System.out.println("外部状态:"+extrinsicState);
    }
}
enum Key { KEY1,KEY2 }
enum UnsharedKey { KEY1,KEY2 }
enum Factory
{
    INSTANCE;
    private Map<Key,Flyweight> map = new HashMap<>();
    public Flyweight get(Key key)
    {
        if(map.containsKey(key))
            return map.get(key);
        switch(key)
        {
            case KEY1:    
                map.put(key, ConcreteFlyweight1.INSTANCE);
                return ConcreteFlyweight1.INSTANCE;
            case KEY2:
                map.put(key, ConcreteFlyweight2.INSTANCE);
                return ConcreteFlyweight2.INSTANCE;
            default:
                return null;
        }
    }

    public Flyweight get(UnsharedKey key)
    {
        switch(key)
        {
            case KEY1:
                return new UnsharedConcreteFlyweight1();
            case KEY2:
                return new UnsharedConcreteFlyweight2();
            default:
                return null;
        }
    }
}
public static void main(String[] args) 
{
    Factory factory = Factory.INSTANCE;
    Flyweight flyweight1 = factory.get(Key.KEY1);
    Flyweight flyweight2 = factory.get(Key.KEY1);
    System.out.println(flyweight1 == flyweight2);

    flyweight1 = factory.get(UnsharedKey.KEY1);
    flyweight2 = factory.get(UnsharedKey.KEY1);
    System.out.println(flyweight1 == flyweight2);
}
enum ConcreteFlyweight3 implements Flyweight {...}
enum ConcreteFlyweight4 implements Flyweight {...}
switch(key)
{
    case KEY1:    
        map.put(key, ConcreteFlyweight1.INSTANCE);
        return ConcreteFlyweight1.INSTANCE;
    case KEY2:
        map.put(key, ConcreteFlyweight2.INSTANCE);
        return ConcreteFlyweight2.INSTANCE;
    case KEY3:
        map.put(key, ConcreteFlyweight3.INSTANCE);
        return ConcreteFlyweight3.INSTANCE;
    case KEY4:
        map.put(key, ConcreteFlyweight4.INSTANCE);
        return ConcreteFlyweight4.INSTANCE;
    default:
        return null;
}
public Flyweight get(Key key)
{
    if(map.containsKey(key))
        return map.get(key);
    try
    {
        Class<?> cls = Class.forName("ConcreteFlyweight"+key.code());
        Flyweight flyweight = (Flyweight)(cls.getField("INSTANCE").get(null));
        map.put(key,flyweight);
        return flyweight;
    }
    catch(Exception e)
    {
        e.printStackTrace();
        return null;
    }
}

enum Key
{
    KEY1(1),KEY2(2),KEY3(3),KEY4(4);
    private int code;
    private Key(int code)
    {
        this.code = code;
    }
    public int code()
    {
        return code;
    }
}
enum UnsharedKey
{
    KEY1(1),KEY2(2),KEY3(3),KEY4(4);
    private int code;
    private UnsharedKey(int code)
    {
        this.code = code;
    }
    public int code()
    {
        return code;
    }
}
public Flyweight get(UnsharedKey key)
{
    try
    {
        Class<?> cls = Class.forName("UnsharedConcreteFlyweight"+key.code());
        return (Flyweight)(cls.newInstance());
    }
    catch(Exception e)
    {
        e.printStackTrace();
        return null;
    }
}


原文地址:https://blog.csdn.net/m0_74161592/article/details/143671962

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!