The flyweight of design pattern

Source: Internet
Author: User

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;
  }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.