←Older revision |
Revision as of 18:38, 10 January 2011 |
Line 25: |
Line 25: |
| ==== Rewrite Cookbook ==== | | ==== Rewrite Cookbook ==== |
| | | |
- | Options? At most to provide [[AlternativeBehavior]]: | + | Options? Not many. Most promissing is to provide [[AlternativeBehavior|alternatives]] by creating new type to represent the same functionality: |
- | # design new type to represent the new functionality
| + | <source lang="java"> |
- | # optionally change the [[enum]] definition to implement the type (but then the type has to be [[Java]] interface, it cannot be a class!)
| + | public enum OldCategory { |
- | # duplicate all the methods that deal with the [[enum]] to also accept or produce the new type (this has to be done transitively)
| + | public String getDisplayName(); |
| + | } |
| + | |
| + | public class NewCategory { |
| + | private NewCategory() {} |
| + | |
| + | public String getDisplayName(); |
| + | |
| + | public static NewCategory fromOld(OldCategory from); |
| + | public OldCategory toOld(); |
| + | |
| + | public static NewCategory create(String name); |
| + | } |
| + | </source> |
| + | Some people may prefer to define ''NewCategory'' as [[Java]] interface. Then it is possible to retrofit the ''OldCategory'' to implement this interface as well. Too bad this cannot be done with classes! [[Java]] classes can have just one superclass and in case of [[enum]]s it is restricted to ''java.lang.Enum''. |
| + | |
| + | In any case, the problem does not stop with the definition of ''NewCategory''. One also needs to duplicate all the methods that accept or return the ''OldCategory'' with their new versions that deals with ''NewCategory'': |
| + | <source lang="java"> |
| + | // old method |
| + | public OldCategory getCategory(); |
| + | // new method |
| + | public NewCategory getBetterCategory(); |
| + | </source> |
| | | |
| In my case, this was exactly what I had to do. Only with one problem. The [[enum]] was in signature of an interface, and I could not change that compatibly, so I would need to design new interface. Introducing the [[AlternativeBehavior]] is transitive change - just to point out the complexity. | | In my case, this was exactly what I had to do. Only with one problem. The [[enum]] was in signature of an interface, and I could not change that compatibly, so I would need to design new interface. Introducing the [[AlternativeBehavior]] is transitive change - just to point out the complexity. |