创建支持实习的对象的良好模式? [英] Good pattern for creating an object that supports interning?

查看:90
本文介绍了创建支持实习的对象的良好模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个支持实习的类,例如Java的 String 类。当创建Class的实例时,如果具有相同参数的引用已经存在,它将返回一个预先存在的引用。否则应该返回一个新的引用。这样:

I need a Class that supports interning, like Java's String class. When creating an instance of the Class it should return a pre-existing reference if a reference with the same parameters already exists. Otherwise it should return a new reference. Such that:

Foo a = new Foo(5);
Foo b = new Foo(5);
Foo c = new Foo(6);
a == b
a != c

1)一个工厂维护预先存在的对象的表,还是表一般只是一个静态变量在类本身内?

1) Is it the job of a factory to maintain the table of pre-existing objects, or is the table generally just a static variable within the class itself?

2)现有的对象?是每一次请求一个实例时搜索的一组对象,还是一个地图,其中键是对象,值是参数?

2) What is the table of pre-existing objects? Is it a Set of objects you search through every time an instance is requested, or is it a Map where keys are objects and values are parameters?

推荐答案

有两件事情出现在我的脑海里

Two things come to my mind


  1. Flyweight Pattern

Guava's ComputingMap

这篇关于创建支持实习的对象的良好模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆