本文介绍了Flyweight Pattern中描述的内在和外在状态有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FlyWeight 中的模式当大多数物体状态可以作为外在的时,FlyWeight模式适用

From the chapter on FlyWeight Pattern inside Gang of Four the FlyWeight pattern is applicable when most object state can be made extrinsic.

外在状态是什么意思?我得到这种模式用于共享对象的感觉。如果要共享对象,那么该对象甚至可以有什么状态呢?

What does extrinsic state mean ? I get the feeling that this pattern is used for sharing of objects . If objects are to be shared , then how can that object even have any state at all ?

推荐答案

无论该项目符号列表中的具体措辞如何,了解消息很重要:适用于数据的重要部分可以在许多对象之间共享的情况,因为它是不可变的。字体面的例子使得这一点非常清楚;来自日常Java的一个例子是 java.util.regex.Pattern ,不可变的外在状态的持有者与 Matcher ,这种轻量级的重复使用它,并保持了局部内在的状态。许多 Matcher 可以并行存在,所有这些都重用内部编译的正则表达式。

Whatever the specific wording in that bulleted list, it is important to understand the message: Flyweight applies to the case where an important part of data can be shared among many objects because it is immutable. The example with font faces makes this quite clear; an example from everyday Java is java.util.regex.Pattern, the holder of immutable extrinsic state, vs. Matcher, the flyweight that reuses it and holds local intrinsic state. Many Matchers can exist in parallel, all reusing the compiled regex on the inside.

该引用使事情更清晰比你的问题的一个:

This quote makes things clearer than the one from your question:

这篇关于Flyweight Pattern中描述的内在和外在状态有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 18:23