Giter VIP home page Giter VIP logo

design-patterns-with-lambdas-in-java8's Introduction

Design Patterns with Lambdas in Java 8 Build Status Codacy Badge License: MIT

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! 😄

Table of Contents

  • Behavioral Patterns
  • Creational Patterns
    • Builder / Assembler
    • Factory Method
    • Singleton
    • Abstract Factory
  • Structural Patterns
    • Adapter
    • Decorator
    • Facade
    • Protection Proxy

Behavioral Patterns

Observer / Listener

Full source code

/**
 * 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());
    }    
}

Listener Usage

/**
 * 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();
    }
}

design-patterns-with-lambdas-in-java8's People

Contributors

1091643978 avatar manoelcampos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.