←Older revision |
Revision as of 19:49, 20 March 2011 |
Line 14: |
Line 14: |
| | | |
| If you try to fix an ''open space'' into a ''fixed point'', what will you get? You'll get a ''fixed point'' - e.g. no changes allowed. [[Evolution]] prevented (only [[ExtendingInterfaces]] approach is possible, but tons of ''instanceof'' spread around all usages are not [[beautiful]] nor sustainable). | | If you try to fix an ''open space'' into a ''fixed point'', what will you get? You'll get a ''fixed point'' - e.g. no changes allowed. [[Evolution]] prevented (only [[ExtendingInterfaces]] approach is possible, but tons of ''instanceof'' spread around all usages are not [[beautiful]] nor sustainable). |
- |
| |
- | [[Image:OpenFixed.png|thumb|right]]
| |
| | | |
| Rather than mixing [[ClientAPI]] and [[ProviderAPI]], separate them and compose them together! Everytime you design a ''fixed point'', make sure that it is extensible. One example of that is [[RequestResponse]] pattern, but in general it is enough if you pass to the ''fixed point'' an ''open space'' argument. That will guarantee extensibility. | | Rather than mixing [[ClientAPI]] and [[ProviderAPI]], separate them and compose them together! Everytime you design a ''fixed point'', make sure that it is extensible. One example of that is [[RequestResponse]] pattern, but in general it is enough if you pass to the ''fixed point'' an ''open space'' argument. That will guarantee extensibility. |
| | | |
- | Let's imagine we want to design a listener (which is a [[ProviderAPI]]): | + | === Calculator === |
| + | |
| + | Let's go through this example while trying to design a bean (that is [[ClientAPI]]) and its listener (which is a [[ProviderAPI]]). |
| + | |
| + | <source lang="java" snippet="openfixed.bean"/> |
| + | |
| + | The calculator has an internal sum which can change by adding numbers to it. Whenever a change happens, listeners are notified: |
| | | |
| <source lang="java" snippet="openfixed.listener"/> | | <source lang="java" snippet="openfixed.listener"/> |
Line 27: |
Line 31: |
| <source lang="java" snippet="openfixed.event"/> | | <source lang="java" snippet="openfixed.event"/> |
| | | |
- | The event is typical [[ClientAPI]]. It is called by the providers of the listener, but its implementation is provided by the [[API]] itself. It is made '''final''' and as such it may safely grow in the future. For example we may enhance it with a new getter counting the sequence number of the event: | + | The event is typical [[ClientAPI]]. It is called by the providers of the listener, but its implementation is provided by the [[API]] itself. It is made '''final''' and as such it may safely grow in the future. Moreover it has package private constructor (another good defensive [[API]] design habbit), so only the ''Calculator'' class may instantiate the instances. |
| + | |
| + | There are few different implementation of the listeners. The ''trivial'' one delivers events synchronously: |
| + | |
| + | <source lang="java" snippet="openfixed.trivial'/> |
| + | |
| + | However as calling foreing code while holding a lock often leads to [[deadlock]]s, there is also trivial asynchronous implementation: |
| + | |
| + | <source lang="java" snippet="openfixed.asynch"/> |
| + | |
| + | All such implementations can be used and tested by a common code that looks like this: |
| + | |
| + | <source lang="java" snippet="openfixed.commontest"/> |
| + | |
| + | === Extending ''Open Space'' === |
| + | |
| + | Sometimes, when there is storm of events, it is important to distinquish the last one. For example [[X Window]] system supports such kind of ''reminding'' counter. Applications can them mark their state as dirty and only react to the last event in such batch event storm. |
| + | |
| + | Such storms are unlikely to be real problem in our ''Calculator'' example, but still, let's try to enhance the system with a way to find out amount of remaining events: |
| | | |
| <source lang="java" snippet="openfixed.addgetter"/> | | <source lang="java" snippet="openfixed.addgetter"/> |
| | | |
- | This is a classical example that has been used many times in the [[JDK]] itself (for example ''PropertyChangeEvent'' got additional ''getPropagationId''). However this is ''just'' an [[evolution]] of the [[ClientAPI]] part. However, it is possible to use the same trick to evolve the listener - e.g. [[ProviderAPI]] too!
| + | The field is initialized by the listener support class before the event is dispatched to registered listeners: |
| | | |
- | Imagine you'd like to add a method ''lastEventProcessed'' into the listener interface. Of course, this is not allowed, as [[ProviderAPI]] shall remain immutable for the sake of [[BackwardCompatibility]]. However you can ''mount'' a new fixed point on the ''open space'' type:
| + | <source lang="java" snippet="openfixed.pendingCount"/> |
| + | |
| + | We have evolved the [[ClientAPI]]. The event class. This is a classical approach used many times in the [[JDK]] itself (for example ''PropertyChangeEvent'' got additional ''getPropagationId''). For many purposes this is sufficient. However there can be more complicated use-cases that may require more complex evolution. |
| + | |
| + | === Mounting new ''Open Space'' via a ''Fixed Point'' === |
| + | |
| + | [[Image:OpenFixed.png|thumb|right]] |
| + | |
| + | There is a FileSystem [[API]] in [[NetBeans]] and it is also capable to deliver events in batches. There were some listeners wanting to be notified about the last modification only. However, due to complex releations between objects and listeners in the system, it was impossible to tell in advance, whether an event comming from the batch is the last one to be delivered to the listener or not (thus we cannot reliably count getRemaining()) value and had to try different solution. |
| + | |
| + | We could add a method ''lastEventProcessed'' into the listener interface. However this is not allowed, as the listener (aka [[ProviderAPI]]) shall remain immutable for the sake of [[BackwardCompatibility]]. We could follow the advice described in [[ExtendingInterfaces]] page and create new marker interface ''LastProccessed'' with the above metioned method. But rather than that, we resolve to a trick that ''mounts'' new open space on top of existing one. First of all let's define new listener (or one could reuse ''Runnable'' in this case): |
| | | |
- | <source lang="java" snippet="openfixed.mount"/>
| |
| <source lang="java" snippet="openfixed.postprocessor"/> | | <source lang="java" snippet="openfixed.postprocessor"/> |
- | <source lang="java" snippet="openfixed.postevent"/>
| |
| | | |
- | The above changes enhance the existing ''open space'' (which is allowed), create new ''fixed point'' and make sure it again contains new ''open space'' (so it is ready for future [[evolution]]). The extensibility is unlimited. | + | Then add new method to the event class. As the class is [[ClientAPI]] it can absorb new methods without any problems: |
| + | |
| + | <source lang="java" snippet="openfixed.mount"/> |
| + | |
| + | The idea is that those willing to find out when batch processing is over, can implement the new listener and register themselves when they receive the first event: |
| + | |
| + | <source lang="java" snippet="openfixed.usemount"/> |
| + | |
| + | The implementation just makes sure to collect all registered and to notify them when all regular listeners are processed: |
| + | |
| + | <source lang="java" snippet="openfixed.postimpl"/> |
| + | |
| + | The solution enhances the existing ''open space'' (which is allowed), creates new ''fixed point'' and make sure it again contains new ''open space'' (so it is ready for future [[evolution]]). The whole set up is displayed on the picture with two ''open space'' arcs. However one does not have stop at two, the extensibility is unlimited... |
| + | |
| + | [[Category:APIDesignPatterns]] [[Category:APIDesignPatterns:Evolution]] [[Category:APIDesignPatterns:Clarity]] |