This project aims to provide implementations of different Design Patterns using Lambda expressions and functional programming in Java 8. A Maven project with the current implemented patterns is available in the src directory. Each pattern implementation is provided into a specific package containing an usage example.
This page provides a simplified and general purpose implementation of such patterns with an usage example, considering that you understand how each pattern works. It is not the intent of this project to explain the patterns. To learn about Design Patterns, you can see some references below:
The examples provided in this page just have essential comments and ommits parts of the code that aren't essential to the implemented understanding, such as constructors, getters and setters. To see a complete, comprehensively documented and ready-to-compile code, follow the link to the source code provided for each current implemented pattern.
This project was inpired and adapted from Design Patterns In Kotlin by Dariusz Baciński. It is work in progress and contributions are welcome! 😄
- Behavioral Patterns
- Observer / Listener
- Strategy
- Command
- State
- Chain of Responsibility
- Visitor
- Creational Patterns
- Builder / Assembler
- Factory Method
- Singleton
- Abstract Factory
- Structural Patterns
- Adapter
- Decorator
- Facade
- Protection Proxy
/**
* The interface to be used by Observable objects to notify Observers
* when specific events happen.
*/
@FunctionalInterface
public interface Listener<T extends Object, U extends Object> {
void notifyObserver(T sender, U data);
}
/**
* Your Observable class, that you usually define with a meaningful
* name such as "Car", "Student" or anything else.
*/
public class MyObservable {
/*
Defines two different Listeners, one for an event called "Foo" and other "Bar"
that receives the sender Observable object and a Long value that in this case,
represents the time the event was fired (but it could be any data and type you want).
*/
private Listener<MyObservable, Long> onEventFooIsFired;
private Listener<MyObservable, Long> onEventBarIsFired;
public MyObservable(){
/*
Initialize the event listeners with empty lambdas to avoid NullPointerException's.
*/
this.onEventFooIsFired = (observable, time) -> {};
this.onEventBarIsFired = (observable, time) -> {};
}
/**
* A foo method that fires a foo event.
*/
public void foo(){
//Include your code here!
onEventFooIsFired.notifyObserver(this, System.currentTimeMillis());
}
/**
* A bar method that fires a bar event.
*/
public void bar(){
//Include your code here!
onEventBarIsFired.notifyObserver(this, System.currentTimeMillis());
}
}
/**
* Implemention of a Observer class that will be notified when
* some events in Observable objects happen.
*/
public class Observer {
public static void main(String[] args) {
MyObservable observable1 = new MyObservable();
observable1.setOnEventFooIsFired((o, t) -> System.out.println(o + " foo method fired at time " + t));
observable1.setOnEventBarIsFired((o, t) -> System.out.println(o + " bar method fired at time " + t));
//Call the methods that will make the Listener to notify the Observer
observable1.foo();
observable1.bar();
}
}