2

これらの 2 つのコードの違いは何ですか? どちらも同じ結果になります。

キャッシングあり:

DefaultPicoContainer cachingContainer = new DefaultPicoContainer(new OptInCaching().wrap(new ConstructorInjection()));

cachingContainer.as(Characteristics.CACHE).addComponent(Cookie.class);

Cookie cookie1 = (Cookie)cachingContainer.getComponent(Cookie.class);
Cookie cookie2 = (Cookie)cachingContainer.getComponent(Cookie.class);

assert(cookie1 == cookie2); // Same instance: OK

インスタンスの追加:

DefaultPicoContainer instanceContainer = new DefaultPicoContainer(new ConstructorInjection());

instanceContainer.addComponent(Cookie.class, new Cookie());

Cookie cookie3 = (Cookie)instanceContainer.getComponent(Cookie.class);
Cookie cookie4 = (Cookie)instanceContainer.getComponent(Cookie.class);

assert(cookie3 == cookie4); // Same instance: OK
4

1 に答える 1