Most Java programmers that are using proprietary libraries or other immutable frameworks are faced with a problem: Every time you need some functionality to work with a framework class you build procedural or even worse redundant code. This leads to high coupling between the framework and the rest of the application.
Here are some thoughts to solve this problem:
Inheritance
The additional functionality is added to an inherited class. This often leads to many casts and instanceof in the source code.
Simple Wrapper
A class that holds the class of the framework. All additonal functionality is added by the wrapper class. Whenever the framework class is needed it can be retrieved by a getter of the wrapper class.
Decorator
Using the decorator pattern often is not possible. The reason is that the framework class has to implement a shared interface with the decorater class but there is no way to change the framework. If the framework class does not implement such an interface the decorator pattern can not be used.
Monkey Patching
For Java-Programmers this is unusual, but often used in ruby and other dynamic languages. With the rise of JRuby and Groovy you can now patch your Java framework classes easily and follow TDA to get DRY code. In some static languages like Scala this is possible, too (see Pimp my Library).
In my opinion monkey patching is the best way to work with unchangable frameworks.
Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts
Tuesday, September 1, 2009
Friday, August 28, 2009
clojure parallel map
Map is a function that takes two parameters. A list and another function that will be executed for every element in the list:
(defn multiply-by-2[x] (* x 2)) (map multiply-by-2 [0 1 2 3 4 5 6 7 8 9]) ; => (0 2 4 6 8 10 12 14 16 18)
The map funktion is a wonderful thing, because every computation for a list element can be parallelized.
Even though Clojure has the function pmap that does exactly this parallelization, I built the same functionality on my own to understand multithreading with Clojure and java.util.concurrent.
Here it is:(import '(java.util.concurrent Executors)) (defn my-pmap [map-fn, l] (let [len (count l) pool (Executors/newFixedThreadPool len) tasks (map (fn [i](fn [] (map-fn (nth l i)))) (range len))] (let [result (map (fn [future](.get future)) (.invokeAll pool tasks))] (.shutdown pool) result))) (my-pmap multiply-by-2 [0 1 2 3 4 5 6 7 8 9]) ; => (0 2 4 6 8 10 12 14 16 18) ; it yields the same result as (pmap multiply-by-2 [0 1 2 3 4 5 6 7 8 9])
Friday, April 24, 2009
Spring Security AuthenticationSuccessListener
Ever asked how to do things on a successful authentication when using Spring Security?
It's very easy. You just need a ApplicationListener that listens for an InteractiveAuthenticationSuccessEvent.
First create a AuthenticationSuccessListener
package example.web.security; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.security.event. authentication. InteractiveAuthenticationSuccessEvent; public class AuthenticationSuccessListener implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) { if (event instanceof InteractiveAuthenticationSuccessEvent) { // do things on authentication ... } } }
Then declare a bean somewhere in Spring Application Context
... <bean class="example.web.security.AuthenticationSuccessListener" /> ...Youre done!
Subscribe to:
Posts (Atom)