Design Patterns (19) - Flyweight Pattern

flyweight pattern

1. Definition

      Use sharing technology to effectively support a large number of fine-grained objects. Flyweight is a shared object.

2. Sample code

       For the management of fine-grained permission objects, the Flyweight pattern is described.

/* Flyweight interface describing authorization data */
public interface Flyweight{
   /* Determine whether the incoming security entity and permissions match the internal state of the flyweight object*/
   public boolean match(String securityEntity,String permit);
}

/*Encapsulate the repeated flyweight object in the authorization data*/
public class AuthorizationFlyweight implements Flyweight{
    private String securityEntity;
    private String permit;
    //Construction method, pass in security entity and permission data, separated by commas
    public AuthorizationFlyweight(String state){
        String ss[] = state.split(",");
        securityEntity = ss[0];
        permit = ss[1];
    }
    public String getSecurityEntity(){
        return securityEntity;
    }
    public String getPermit(){
        return permit;
    }
    public boolean match(String securityEntity,String permit){
         if(this.securityEntity.equals(securityEntity) && this.permit.equals(permit)){
              return true;
         }
         return false;
    }
}

/* Flyweight factory definition, usually implemented as simple interest mode */
public class FlyweightFactory{
    private static FlyweightFactory factory = new FlyweightFactory();
    private FlyweightFactory(){
    }
    public static FlyweightFactory getInstance(){
       return factory;
    }
    //Cache multiple flyweight objects
    private Map<String,Flyweight> fsMap = new HashMap<String,Flyweight>();
    //Get the flyweight object corresponding to the key
    public Flyweight getFlyweight(String key){
       Flyweight f = fsMap.get(key);
       if(f == null){
          f = new AuthorizationFlyweight(key);
          fsMap.put(key,f);
       }
    }
}

   

/*Define the security management class and realize it as simple interest*/
public class SecurityMgr{
    private static SecurityMgr securityMgr = new SecurityMgr();
    private SecurityMgr(){
    }
    public static getInstance(){
        return securityMgr;
    }
    //Used to store the permissions of the login personnel during operation
    private Map<String,Collection<Flyweight>> map = new HashMap<String,Collection<Flyweight>>();
    //Simulate user login
    public void login(){
       Colllection<Flyweight> col = queryByUser(user);
       map.put(user,col);
    }
    //Determine whether a user has a certain permission to a security entity
    public boolean hasPermit(String user,String securityEntity,String permit){
        Colllection<Flyweight> col = map.get(user);
        if(col == null || col.size() == 0){
            return false;
        }
        for(Flyweight fm:col){
            if(fm.match(securityEntity,permit)){
               return true;
            }
        }
        return false;
    }
    //Get all the permissions of someone from the database
    private Colllection<Flyweight> queryByUser(){
         Colllection<Flyweight> col= new ArrayList<Flyweight>();
         for(String s : TestDB.colDB){
             String ss[] = s.split(",");
             if(ss[0].equals(user)){
                  Flyweight fm = FlyweightFactory.getInstance().getFlyweight(ss[1],ss[2]);
                  col.add(fm);
             }
         }
         return col;
    }
}

/* Simulate database operation */
public class TestDB{
    public static Collection<String> colDB = new ArrayList<String>();
    static{
       colDB.add("Zhang San, personnel list, view");
       colDB.add("Li Si, personnel list, view");
       colDB.add("Li Si, salary data, view");
       colDB.add("Li Si, salary data, modify");
    }
}

   

/*Client test*/
public class Client{
   public static void main(String args[]){
      SecurityMgr mgr = new SecurityMgr();
      mgr.login("Zhang San");
      mgr.login("Li Si");
      boolean f1 = mrg.hasPermit("Zhang San","salary data","view");
      boolean f2 = mrg.hasPermit("Li Si","salary data","view");
   }
}

   

3. Practical application

    The design focus of Flyweight mode is to separate change and invariance. In Flyweight mode, in order to create and manage shared Flyweight parts, Flyweight Factory is introduced, which generally contains instance pools of Flyweight objects. .

 

The essence of flyweight pattern: separation and sharing

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326331846&siteId=291194637