Please enable JavaScript.
Coggle requires JavaScript to display documents.
Flyweight (Applicability - use when all of the following are true (Most…
Flyweight
-
Key Objects
-
Flyweight Factory creates and manages flyweight objects. When client asks for an object, provides an existing one if it is present
-
Client - maintains reference to external flyweight objects. Computes or stores the states of extrinsic state
Flyweight Defines an interface through which flyweight objects can receive and acton on extrinsic state
Consequences
-
Greatest savings come if there is a fair bit of intrinsic and extrinsic state and extrinsic state can be computed instead of stored
Incurs runtime costs due to transferring, maintaining or computing extrinsic state. Offset by storage savings
-
Motivation
-
Key here is context. Attributes which are intrinsic and independent of context are stored within flyweight. Attributes that are extrinsic and depend on context are stored in the client
Implementation
Removing Extrinsic State If there are as many kinds of extrinsic state as there are objects prior to sharing, then it won't help much. Should be easy to remove extrinsic state
Managing Shared Objects Client should not create flyweights, should be created by Factory
Applications
-
Integers, Strings etc in Java when stored in pools
-
-