gpt4 book ai didi

java - 有什么方法可以标记一个对象以表明它已经完成了一个过程?

转载 作者:行者123 更新时间:2023-11-30 09:23:05 24 4
gpt4 key购买 nike

以不变性为例。我如何修改一个对象以表明它已经不可变并且不需要再次包装?

假设我们不想使用反射来扫描 setter,因为那样效率低且不充分。

例子:

// Deliberately chosing lowercase because it is a system attribute.
interface immutable {
// Nothing in here I can think of.
}

// immute - have I invented a new word?
// What can I do with the return type to indicate immutability?
public static <T> List<T> immute(List<T> list) {
// If it's not an immutable
if (!(list instanceof immutable)) {
// Make it so - how can I stamp it so?
return Collections.<T>unmodifiableList(list);
}
// It is immutable already.
return list;
}

最佳答案

进一步研究这个想法产生了这个错误的解决方案 - 这太可怕了,几乎任何其他技巧都会更好,但我觉得我应该发布。请找到更好的解决方案:

public class Test {
// Deliberately chosing lowercase because it is a system attribute.
interface immutable {
// Nothing in here I can think of.
}

// immute - have I invented a new word?
// What can I do with the return type to indicate immutability?
public static <T> List<T> immute(List<T> list) {
// If it's not an immutable
if (!(list instanceof immutable)) {
// Make it so - how can I stamp it so?
return Hacker.hack(Collections.<T>unmodifiableList(list),
List.class,
immutable.class);
}
// It is immutable already - code DOES get here.
return list;
}

public void test() {
System.out.println("Hello");
List<String> test = new ArrayList<>();
test.add("Test");
test("Test", test);
List<String> immutableTest = immute(test);
test("Immutable Test", immutableTest);
List<String> immutableImmutableTest = immute(immutableTest);
test("Immutable Immutable Test", immutableImmutableTest);
}

private void test(String name, Object o) {
System.out.println(name + ":" + o.getClass().getSimpleName() + "=" + o);

}

public static void main(String args[]) {
new Test().test();
}
}

class Hacker {
// Hack an object to seem to implement a new interface.
// New interface should be instanceof testable.
// Suggest the additional type is an empty interface.
public static <T> T hack(final Object hack,
final Class<T> baseType,
final Class additionalType) {

return (T) Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
new Class[]{baseType, additionalType},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Always invoke the method in the hacked object.
return method.invoke(hack, args);
}
});
}
}

关于java - 有什么方法可以标记一个对象以表明它已经完成了一个过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16225895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com