?????????????÷??????????????
????Context context = new Context();
????Runnable runnable = ...
????context.put( "key"?? runnable?? Runnable.class );
????// several computation cycles later...
????Runnable value = context.get( "key"?? Runnable.class );
????????????δ???????????????????????????????6?б?????????????down cast??????????????????????????????????????????????10?и?????????ClassCastException ???????
????Context context = new Context();
????Runnable runnable = ...
????context.put( "key"?? runnable?? Runnable.class );
????// several computation cycles later...
????Executor executor = ...
????context.put( "key"?? executor?? Executor.class );
????// even more computation cycles later...
????Runnable value = context.get( "key"?? Runnable.class ); // runtime problem
??????????????????
?????????Context#get?е????????????Ч?????????????????t??????Object??????????????unbonded parameters??????????????????????????????????Context#put ????????????????????????????????????
??????????????????
???????????Context ????????????????????????????????????????????????????????key?? ??????????????????????????????Bloch????????????????????????typesafe heterogenous container pattern?????????ɡ?
???????????????key?????class ???????key?????Class ????????????????????????????Context??????????????????????????????δ?????????????T??????????????Class ????????????????type token????
????public class Context {
????private final Map<Class<?>?? Object> values = new HashMap<>();
????public <T> void put( Class<T> key?? T value ) {
????values.put( key?? value );
????}
????public <T> T get( Class<T> key ) {
????return key.cast( values.get( key ) );
????}
????[...]
????}
???????????Context#get ???????????????????Ч?????????滻???????????????????????????context??
????Context context = new Context();
????Runnable runnable ...
????context.put( Runnable.class?? runnable );
????// several computation cycles later...
????Executor executor = ...
????context.put( Executor.class?? executor );
????// even more computation cycles later...
????Runnable value = context.get( Runnable.class );
??????ο???????????????????????????????????????????????????????????????????????????????
?????й??????????????????????????????й?????????????????????????????????й?????????????????
????Bloch??????????????????????“???????????????????????????????raw form?????class????????????????????”?????????????????????????Context#put????????????dynamic cast????
????public <T> void put( Class<T> key?? T value ) {
????values.put( key?? key.cast( value ) );
????}
????????????????????????????????廯??non-reifiable ?????????У?????Effective Java????25???????仰???????????Runnable ??Runnable[]????????????List<Runnable>??
???????????List<Runnable>??????class???????е????????????????????List.class ????????Bloch????????????????????????????????
???????????????????洢??????????????????????????????????????????????????????????????????μ??????????????????????????????????Key??????????????
????????????????????
???????????洢????????????????????????????????key???Context ??????key??????????????????????????????????????????????value objects?????????????String ???????????????key???????????????
????public class Key<T> {
????final String identifier;
????final Class<T> type;
????public Key( String identifier?? Class<T> type ) {
????this.identifier = identifier;
????this.type = type;
????}
????}