Flyweight definition:
Avoid the overhead of a large number of small classes with the same content (such as memory consumption), so that you can share a class (meta Class).
Why use?
The principle of object-oriented language is that everything is an object, but if the real use, sometimes the number of objects may appear very large, such as word processing software, if each text as an object, thousands of words, the number of objects is thousands of, no doubt the memory consumption, then we still have to "seek common ground", Find common ground among these object groups, design a meta class, encapsulate the classes that can be shared, and, in addition, some features that depend on the application (context), are not shareable, and flyweight the internal state intrinsic and the external state extrinsic of the two important concepts.
The white point is to pinch one of the original model, and then with different occasions and circumstances, and then produce a specific model of the characteristics, it is obvious that here need to produce different new objects, Therefore, the factory pattern is often present in the Flyweight mode. The internal state of the Flyweight is shared, and Flyweight factory is responsible for maintaining a Flyweight pool (pattern pool) to hold objects in the internal state.
Flyweight mode is a model that improves program efficiency and performance, and can greatly speed up the program. Many applications: For example, if you are reading a series of strings from a database, many of which are duplicates, then we can store these strings in the flyweight pool ( Pool).
How to use it?
Let's start with the flyweight abstract interface:
public interface Flyweight
{
public void operation( ExtrinsicState state );
}
//用于本模式的抽象数据类型(自行设计)
public interface ExtrinsicState { }
The following is the concrete implementation of the interface (Concreteflyweight) and increases the memory space for the internal state, concreteflyweight must be shareable, and any state it holds must be internal (intrinsic), that is to say, Concreteflyweight must have nothing to do with its application environment.
public class ConcreteFlyweight implements Flyweight {
private IntrinsicState state;
public void operation( ExtrinsicState state )
{
//具体操作
}
}
Of course, not all flyweight need to be shared specifically to implement subclasses, so there is another concreteflyweight that is not shared:
public class UnsharedConcreteFlyweight implements Flyweight {
public void operation( ExtrinsicState state ) { }
}
Flyweight Factory is responsible for maintaining a Flyweight pool (storing internal state), and when the client requests a shared Flyweight, the factory first searches the pool for any applicable, if any, factory simply returns the object to be sent , otherwise, create a new object, add it to the pool, and return to send the object.
public class FlyweightFactory {
//Flyweight pool
private Hashtable flyweights = new Hashtable();
public Flyweight getFlyweight( Object key ) {
Flyweight flyweight = (Flyweight) flyweights.get(key);
if( flyweight == null ) {
//产生新的ConcreteFlyweight
flyweight = new ConcreteFlyweight();
flyweights.put( key, flyweight );
}
return flyweight;
}
}