Select Page

Flyweight

Flyweight

Use sharing to support large numbers of fine-grained objects efficiently.

Source Code

/**  * Declares an interface through which flyweights can receive and act on  * extrinsic state.  *   * @role __Flyweight  */ public interface Flyweight { 	void sampleOperation(FlyweightContext context); }  /**  * Represents extrinsic state of flyweight(s).  *   * @role __State  */  public interface FlyweightContext { }  /**  * Implements the Flyweight interface and adds storage for intrinsic state, if  * any. Objects of this class must be sharable. Any state it stores must be  * intrinsic (independent of this object's context)  */  public class ConcreteFlyweight implements Flyweight { 	public ConcreteFlyweight(Object key) { 		// initialize internal state 	}  	/** @see patterns.gof.flyweight.Flyweight#sampleOperation(FlyweightContext) */ 	public void sampleOperation(FlyweightContext context) { 		// provide implementation here 	} }  /**  * Creates and manages flyweight objects. Ensures that flyweights are shared  * properly. When a client requests a flyweight, this factory supplies an  * existing instance or creates one, if none exists.  *   * @role __Factory  */  public class FlyweightFactory { 	private HashMap flyweight;  	public FlyweightFactory() { 		flyweight = new HashMap(); 	}  	public Flyweight getFlyweight(Object key) { 		if (flyweight.containsKey(key)) { 			return (Flyweight) flyweight.get(key); 		} else { 			Flyweight newFlyweight = new ConcreteFlyweight(key); 			flyweight.put(key, newFlyweight); 			return newFlyweight; 		} 	}  	/** 	 * @return new instance ofunshared flyweight 	 */ 	public Flyweight getUnsharedConcreteFlyweight() { 		return new UnsharedConcreteFlyweight(); 	} }  /**  * Not all Flyweight subclasses need to be shared  */  public class UnsharedConcreteFlyweight implements Flyweight { 	/** @see patterns.gof.flyweight.Flyweight#sampleOperation(FlyweightContext) */ 	public void sampleOperation(FlyweightContext context) { 		// provide implementation here 	}  } 

About The Author

Cédric Walter

I worked with various Insurances companies across Switzerland on online applications handling billion premium volumes. I love to continuously spark my creativity in many different and challenging open-source projects fueled by my great passion for innovation and blockchain technology.In my technical role as a senior software engineer and Blockchain consultant, I help to define and implement innovative solutions in the scope of both blockchain and traditional products, solutions, and services. I can support the full spectrum of software development activities, starting from analyzing ideas and business cases and up to the production deployment of the solutions.I'm the Founder and CEO of Disruptr GmbH.

Categories