Designpatterns Study Notes: C + + language implementation---2.5 Factory
2016-07-18
(WWW.CNBLOGS.COM/ICMZN)
Pattern understanding
1. Flyweight mode, which is the shared unit mode
Flyweeight is a special noun in boxing, called "Extra-lightweight", then in the design mode the middle finger is the class to light weight, the granularity of the class is fine, can achieve fine-grained class reuse, but there is no
Lack of a shared mechanism, that is, multithreading under the class is not reusable.
The enjoy meta-mode is an important implementation of "pool technology", but they are not equivalent. Use the enjoy meta mode to support the sharing of a large number of fine-grained objects.
Because creating too many objects to the program's performance, you can use the sharing technology of the enjoy meta-mode to treat objects as "fine-grained objects" and then implement "shared objects."
Fine-grained objects refer to the number and nature of objects, which can be divided into "internal state intrinsic" "external state extriinsic".
Internal state intrinsic: is the information that the object can share and is stored inside the specific shared meta object.
External state extrinsic: it is used as a marker, because the external state is classified according to the external conditions, so the external state changes with the environment, so this part of the information
Generally speaking, flyweight can be divided into the following 4 characters:
(1) absflyweight, abstract enjoy meta role (shared unit)
is a shared unit of abstract behavior properties, generally abstract class, in the project may be a concrete class, the general can be the external state and internal state is defined (implemented), to avoid
A discretionary extension in a derived class.
(2) Concreteflyweight, specific enjoy meta role (internal state initialization)
A specific product class that implements Absflyweight abstract methods or business logic. Complete various personalization actions for internal properties.
(3) Unshareflyweight, non-shareable privileges (not reflected in design mode)
Objects that are not able to use a shared technology are not typically present in a meta factory.
(4) Flyweightfactory, enjoy the Yuan factory
Constructs a pool container, and searches for buffer-to-original objects in the pool container, based on the most key value of the external attribute in the sharing element.
2. Flyweight Advantages
3. Flyweight Application Scenario
(1) so that some fine-grained objects can be shared, using the enjoy meta-mode.
4. Flyweight Discussion
Program Implementation (c + +)
(1) Template application
(2) Output display
Designpatterns Learning Note: C + + language implementation---2.5 Factory