我一直在阅读文档,boost::flyweight但我没有看到任何提及解除分配或引用计数政策.基本上,flyweight对象应该像不同值的存储库一样,但不清楚当不再使用不同的值时会发生什么.
它已经被支持了吗?是否可以通过一些定制工厂启用?
我对Flyweight模式的这些状态的差异感到困惑.
我知道intrinsic状态是共享的状态,而Extrinsic不是.
但是,我没有看到extrinsic状态在模式中或以下示例中的重要性:
public static void main(String[] args) {
// Create the flyweight factory...
EngineFlyweightFactory factory = new EngineFlyweightFactory();
// Create the diagnostic tool
DiagnosticTool tool = new EngineDiagnosticTool();
// Get the flyweights and run diagnostics on them
Engine standard1 = factory.getStandardEngine(1300); //intrinsic
standard1.diagnose(tool); //extrinsic
Engine standard2 = factory.getStandardEngine(1300); //intrinsic
standard2.diagnose(tool); //extrinsic
Engine standard3 = factory.getStandardEngine(1300); //intrinsic
standard3.diagnose(tool); //extrinsic
Engine standard4 = factory.getStandardEngine(1600); //intrinsic
standard4.diagnose(tool); //extrinsic
Engine standard5 = factory.getStandardEngine(1600); //intrinsic
standard5.diagnose(tool); …Run Code Online (Sandbox Code Playgroud) 我看不出Multiton和Flyweight模式之间有任何单一的区别.请解释一下有什么区别?
我有一个类,其中包含一个字符串作为私有成员。该类具有公共成员函数,它们都将const ref返回给std :: wstring。
返回的所有const-ref字符串都是私有成员的子字符串。
我可以将每个子字符串存储为成员,然后将const ref返回给那些成员,但这并不是最佳选择,因为它会复制内存。
假设我确实存储了足够的内存来知道每个子字符串的开始和结束索引。
如何在不复制任何成员字符串的情况下实现上述类?
要应用享元模式,我们需要将 Object 属性分为内在和外在属性。内在属性使对象独一无二,而外在属性由客户端代码设置并用于执行不同的操作。
但我的问题是为什么我们不能同时拥有内在和外在属性作为实例变量(请参阅下面的电子邮件类),而只能在循环外创建一个对象并在循环中设置参数并使用不同参数发送多封电子邮件。
public class Test {
public static void main(String[] args) {
Email ob = new Email();
for (int i = 0; i < 10; i++) {
ob.sender = String.valueOf(i);
ob.sendEmail();
}
}
}
public class Email {
public String sender;
public void sendEmail()
{
System.out.println("Email sent to sender:"+sender);
}
}
Run Code Online (Sandbox Code Playgroud)