java设计模式_享元模式

java设计模式_享元模式

/**
 * 
 */
package com.wangbiao.design.flyweight;

/**
 * @Title: User.java
 * @Package com.wangbiao.design.flyweight
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-28 下午2:31:14 
 * @version V1.0
 */
public class User {
    
    private String name;
    public User(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}

/**
 * 
 */
package com.wangbiao.design.flyweight;

/**
 * @Title: FlyWeight.java
 * @Package com.wangbiao.design.flyweight
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-28 下午2:29:07 
 * @version V1.0
 */
public interface FlyWeight {
    
    public void operation(User user);

}

class ConcreteFlyWeight_Share implements FlyWeight{
    private String name = null;
    
    public ConcreteFlyWeight_Share(String name) {
        this.name = name;
    }
    
    @Override
    public void operation(User user) {
        System.out.println("Name is " + name + "----User Name is "+user.getName());
    }
    
}

class ConcreteFlyWeight_Not_Share implements FlyWeight{
    private String name = null;
    
    public ConcreteFlyWeight_Not_Share(String name) {
        this.name = name;
    }
    
    @Override
    public void operation(User user) {
        System.out.println("Not Share this object ----Name is " + name + "  User Name is "+user.getName());
    }
    
}


/**
 * 
 */
package com.wangbiao.design.flyweight;

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

/**
 * @Title: FlyWeightFactory.java
 * @Package com.wangbiao.design.flyweight
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-28 下午2:37:07 
 * @version V1.0
 */
public class FlyWeightFactory {
    
    private Map<String,FlyWeight> map = new HashMap<String,FlyWeight>();
    
    public FlyWeight getFlyWeight(String name){
    if(name != null && map.containsKey(name)){
            return map.get(name);
        }else{
            map.put(name, new ConcreteFlyWeight_Share(name));
            return new ConcreteFlyWeight_Share(name);
        }
    }

    public int getObjectNum(){
        return  this.map.size();
    }
}

/**
 * 
 */
package com.wangbiao.design.flyweight;

/**
 * @Title: Client.java
 * @Package com.wangbiao.design.flyweight
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-26 下午3:54:13 
 * @version V1.0
 */
public class Client {

    /**
     * @param args
     */
    public static void main(String[] args) {
        FlyWeightFactory factory = new FlyWeightFactory();
        
        FlyWeight flyWeight_one =  factory.getFlyWeight("足球");
        flyWeight_one.operation(new User("大空翼"));
        
        FlyWeight flyWeight_two =  factory.getFlyWeight("篮球");
        flyWeight_two.operation(new User("樱木花道"));
        
        FlyWeight flyWeight_three =  factory.getFlyWeight("足球");
        flyWeight_three.operation(new User("大郎"));
        
        //在某些情况下,不需要用共享的对象,同样支持。
        FlyWeight flyWeight_no_share = new ConcreteFlyWeight_Not_Share("足球");
        flyWeight_no_share.operation(new User("锐林"));
        
        System.out.println("Object num is "+ factory.getObjectNum());
        
        /**
         * 
         *  Name is 足球----User Name is 大空翼
            Name is 篮球----User Name is 樱木花道
            Name is 足球----User Name is 大郎
            Not Share this object ----Name is 足球  User Name is 锐林
            Object num is 2
         * 
         */
    }

}


你可能感兴趣的:(java设计模式_享元模式)