The definition of the share schema

Definition: Using shared objects effectively supports a large number of fine-grained objects

Generally speaking, it is to extract the general attributes of the class and establish the object pool to achieve the effect of limiting the number of objects

The above definition requires fine-grained objects, which inevitably leads to a large number of objects with similar properties. We divide the information about these objects into two parts: internal state and external state

  1. Internal state is information that can be shared by objects, stored inside the share object and not changed by the environment. Such as personal information in an examination system.
  2. An external state is a token that an object can depend on, a state that changes with the environment and cannot be shared. If enter oneself for an examination system in enter oneself for an examination subject. The share pattern usually limits the number of objects produced by reference to the external state

To put it bluntly, an internal state is a property that is different for each object, and an external state is a finite number of properties, such as gender only male and female

The class diagram for the Meta pattern is as follows:

 

The roles:

  1. Flyweight abstract User role: Simply put, it is an abstract class of a product that defines both the external state of the object and the interface or implementation of the internal state
  2. ConcreteFlyweight Concrete Role: A concrete product class that implements an abstract role defined service. The role needs to be aware that the internal state processing should be independent of the environment, and that an operation should not change the internal state while changing the external state, which is not allowed by the role
  3. FlyweightFactory: The responsibility is very simple to construct a pool container and provide methods to get objects from the pool

The purpose of the share pattern is to use the sharing technology to make some fine-grained objects can be shared

Abstract meta-role code:

 

Abstract actors are generally abstract classes, and in real projects are generally an implementation class, which is a method that describes a class of things. In abstract roles, external state and internal state should be defined to avoid arbitrary extension of subclasses. We add the final keyword to the external state to prevent unexpected events. Get the external state, accidentally modify it, and the pool gets messed up.

In program development, properties that need to be assigned only once are set to final to avoid logic confusion caused by unintentional modification.

Specific meta-role code:

 

Xiangyuan Factory code:

 

Enjoy the application of the model

Advantages and disadvantages of the share mode:

The flyweight pattern is a very simple model, it can greatly reduce the number of applications to create objects, reduce the program memory footprint, enhance the performance of the program, but it also improves the system complexity, the need to separate the external and internal state, curing characteristics and external state, does not change, change with internal state otherwise system logic confusion caused

Usage scenarios of Enjoy mode:

  1. A large number of similar objects exist in the system
  2. Fine-grained objects have a close external state, and the internal state is context-independent, that is, the object has no specific identity
  3. Scenarios where buffer pools are required

Extension of the share mode

1. Thread safety issues

When using the share mode, there is a fixed number of roles in the object pool, and it is possible for different threads to hold the same object at the same time. This is where thread insecurity comes in

We need to pay attention to this when using the share mode. When we use the share pattern, there are as many share objects in the object pool as possible, as many as are sufficient for the business

2. Performance balance

Since it’s object-oriented programming, why don’t we separate out the external state and define it as an object?

In tests, using objects for external state is less efficient than using primitive types. Therefore, it is best to mark external status with Java basic types, such as stirng and int, to greatly improve efficiency


The meta schema is also ubiquitous in Java apis. Java’s String, for example, implements object pooling

It should be noted that although you can use the share pattern to implement object pooling, there is a big difference between the two. Object pooling emphasize on the reuse of objects, each object in the pool is replaceable, obtained from the same pool of object A and object B is identical to the client, it mainly solve the reuse, and the flyweight pattern basically solve the problems of Shared object, how to set up multiple fine-grained objects can be Shared is its focus.