{"id":15297578,"url":"https://github.com/schlegel11/lambdadecor","last_synced_at":"2025-10-24T05:22:16.858Z","repository":{"id":57730878,"uuid":"115570773","full_name":"schlegel11/LambdaDecor","owner":"schlegel11","description":"Decorator and strategy lambda mixture","archived":false,"fork":false,"pushed_at":"2018-10-29T02:14:43.000Z","size":146,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-22T17:14:26.785Z","etag":null,"topics":["behaviour","decorator","java8","lambda","pattern","strategy"],"latest_commit_sha":null,"homepage":"http://lambdadecor.schlegel11.de/","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/schlegel11.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-28T01:12:13.000Z","updated_at":"2018-10-29T02:14:45.000Z","dependencies_parsed_at":"2022-09-26T22:01:39.982Z","dependency_job_id":null,"html_url":"https://github.com/schlegel11/LambdaDecor","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/schlegel11/LambdaDecor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schlegel11%2FLambdaDecor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schlegel11%2FLambdaDecor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schlegel11%2FLambdaDecor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schlegel11%2FLambdaDecor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/schlegel11","download_url":"https://codeload.github.com/schlegel11/LambdaDecor/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schlegel11%2FLambdaDecor/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262056512,"owners_count":23251689,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["behaviour","decorator","java8","lambda","pattern","strategy"],"created_at":"2024-09-30T19:18:21.949Z","updated_at":"2025-10-24T05:22:16.791Z","avatar_url":"https://github.com/schlegel11.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LambdaDecor\n\n[![Build Status](https://travis-ci.org/schlegel11/LambdaDecor.svg?branch=master)](https://travis-ci.org/schlegel11/LambdaDecor)\n[![Maven Central](https://img.shields.io/maven-central/v/de.schlegel11/lambda-decor.svg)](https://search.maven.org/#search%7Cga%7C1%7Ca%3A%22lambda-decor%22)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/dfa7dfd1c9ce492cb447b7f8e9bcaa42)](https://www.codacy.com/app/marcel_4/LambdaDecor?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=schlegel11/LambdaDecor\u0026utm_campaign=badger)\n[![Known Vulnerabilities](https://snyk.io/test/github/schlegel11/lambdadecor/badge.svg)](https://snyk.io/test/github/schlegel11/lambdadecor)\n\nDecorator and strategy lambda mixture \u003cbr\u003e\nUse lambda's as a way to decorate your classes etc. with new functionality.\nIt is similar to the decorator or strategy pattern.\n\n## Requirements\n\n- JDK 8 and up\n\n## Documentation\n\n##### Version 1.2 (Latest)\n\nFor details see: [JavaDoc](https://schlegel11.github.io/LambdaDecor/releases/1.2/api/docs/)\n \n## Usage\n\n##### The Behaviour\n\nA Behaviour is a class in which you can add different functionality. \u003cbr\u003e\nIt wraps more or less a Function\u003cT, T\u003e which accepts and returns a specific type.\n\nWe create an Behaviour and add functionality like this:\n```java\n        Behaviour\u003cString\u003e stringBehaviour = DefaultBehaviour.newBehaviour();\n        stringBehaviour = stringBehaviour.with(s -\u003e s + \" concat something\"); // Add a Function\u003cString, String\u003e \n```\n\nIt is possible to chain more functionality by using the method \"with(Function\u003cT,T\u003e)\".\nFurther more the Behaviour class accespts also a Function\u003cT, Unappliable\u003e. The Unappliable is a functional interface\nwhich can be performed after all Behaviour specific functionality is performed.\n\nLets add more functionality to the Behaviour, an Unappliable and execute the whole thing ;)\n\n```java\n        Behaviour\u003cString\u003e stringBehaviour = DefaultBehaviour.newBehaviour();\n        stringBehaviour = stringBehaviour.with(s -\u003e s + \" concat something\"); // Add a Function\u003cString, String\u003e\n\n        stringBehaviour = stringBehaviour.with(s -\u003e s.replace('h', '#'))\n                                         .with(String::toLowerCase); /* Add more functionality -\u003e\n                                                                    the return value (in this case the string)\n                                                                    is the argument for the next \"with(Function\u003cT,T\u003e)\" in the chain. */\n\n        stringBehaviour = stringBehaviour.withUnapply(\n                s -\u003e () -\u003e System.out.println(\"Something after\")); // Add functionality with an Unappliable\n\n        DecorPair\u003cString\u003e decorPair = stringBehaviour.apply(\"anything\"); // Perform our Behaviour and returns an DecorPair\n\n        System.out.println(decorPair._Behaviour); //Output behaviour value: \"anyt#ing concat somet#ing\"\n\n        decorPair._Unapply.unapply(); //Perform the Unappliable of our Behaviour -\u003e Outputs: \"Something after\"\n```\n\nYou can chain as many \"with(Function\u003cT,T\u003e)\" or \"withUnapply(Function\u003cT, Unappliable\u003e)\" functionality as you want.\n\n##### The LambdaDecor\n\nThe LambdaDecor class wraps in general an Behaviour and allows an easier handling.\nFor example the above Behaviour can be recreated with this LambdaDecor:\n\n```java\n        LambdaDecor\u003cString\u003e lambdaDecor = DefaultLambdaDecor.create(b -\u003e b.with(s -\u003e s + \" concat something\")\n                                          .with(s -\u003e s.replace('h', '#'))\n                                          .with(String::toLowerCase)\n                                          .withUnapply(\n                                                  s -\u003e () -\u003e System.out.println(\"Something after\")));\n\n        System.out.println(lambdaDecor.apply(\"anything\"));\n        \n        lambdaDecor.unapply();\n```\n\n### UI example\n\n##### Construct the behaviour\n\nA sample use case could be a decoration of a JavaFX button.\nIn this case we create a final class with static methods for a specific behaviour of a Button object.\n```java\npublic final class MyButtonBehaviours {\n\n    public static Button redButton(Button button) {\n        button.setStyle(\"-fx-background-color: #ff3950\"); // Change button color to red\n        return button; // return our button\n    }\n\n    public static Unappliable helloOnClick(Button button) {\n        button.setOnMouseClicked(\n                event -\u003e System.out.println(\"Hello\")); // set a click listener and output \"Hello\" on click\n        return () -\u003e button.setOnMouseClicked(null); // On unapply set the listener to null\n    }\n\n    public static Unappliable onUnapplyGreenButton(Button button) {\n        return () -\u003e button.setStyle(\"-fx-background-color: #7fee59\"); // Change button color to green\n    }\n}\n```\n\n##### Construct the lambda decor\n\nNext we create our LambdaDecor with functionality of the ButtonBehaviour class.\n```java\npublic class Main extends Application {\n\n    @Override\n    public void start(Stage primaryStage) throws Exception {\n        LambdaDecor\u003cButton\u003e buttonDecor = DefaultLambdaDecor.create(b -\u003e b.with(MyButtonBehaviours::redButton)\n                                                                          .withUnapply(\n                                                                                  MyButtonBehaviours::helloOnClick)\n                                                                          .withUnapply(\n                                                                                  MyButtonBehaviours::onUnapplyGreenButton)); // Construct the decor object with its behaviour\n\n        FlowPane root = new FlowPane(5, 5);\n        root.getChildren()\n            .add(buttonDecor.apply(new Button(\"MyButton\"))); // Apply the behaviour to a button object\n        root.getChildren()\n            .add(buttonDecor.apply(new Button(\"MyButton\"))); // Apply the behaviour to a button object\n\n\n        primaryStage.setTitle(\"Hello World\");\n        primaryStage.setScene(new Scene(root, 300, 275));\n        primaryStage.show();\n        primaryStage.iconifiedProperty()\n                    .addListener((c, o, n) -\u003e buttonDecor.unapply()); // If the main window is iconified unapply the behaviour -\u003e button color change to green and listener is set to null\n    }\n\n\n    public static void main(String[] args) {\n        launch(args);\n    }\n}\n```\n\n### Summary\n\nWith the LambdaDecor its possible to add functionality to a specific type in a more functional oriented way.\nFurther more there is a loose coupling between our specific object and the functionality of it so we can exchange this easily.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschlegel11%2Flambdadecor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fschlegel11%2Flambdadecor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschlegel11%2Flambdadecor/lists"}