Flyweight: Problem
Each particle, such as a bullet, a missile or a piece of shrapnel was represented by a separate object containing plenty of data. At some point, when the carnage on a player’s screen reached its climax, newly created particles no longer fit into the remaining RAM, so the program crashed
每个粒子,例如子弹、导弹或弹片,都由包含大量数据的单独对象表示。在某个时刻,当玩家屏幕上的屠杀达到高潮时,新创建的粒子不再适合剩余的 RAM,因此程序崩溃了。
Flyweight: Solution
Problem analysis
– Some fields consume more memory than others, and store almost identical data
– 某些字段比其他字段消耗更多内存,并且存储几乎相同的数据
– Other parts are unique to each instance, and the values change over time
– 其他部分对于每个实例都是唯一的,并且值随时间变化
– Intrinsic state and extrinsic state
– 内在状态和外在状态
- Stop storing the extrinsic state inside the object
停止在对象内部存储外部状态 - Only intrinsic state stays within the object, for supporting reuse
只有内在状态保留在对象内,以支持重用
Proxy: Problem and Solution
A massive object consuming a vast amount of resources, needed from time to time, but not always
消耗大量资源的大型对象,有时需要,但并不总是需要
The Proxy pattern suggests that you create a new proxy class with the same interface as an original service object. Then you update your app so that it passes the proxy object to all of the original object’s clients. Upon receiving a request from a client, the proxy creates a real service object and delegates all the work to it.
代理模式建议您创建一个新的代理类,其接口与原始服务对象相同。然后更新您的应用程序,以便将代理对象传递给所有原始对象的客户端。在收到来自客户端的请求后,代理创建一个真正的服务对象并将所有工作委托给它。