Class library for design patterns in Java?

I find myself repeating the same programming patterns over and over again in many new projects. I was thinking about creating my own reusable library of typical implementations of such patterns - without trying to cover all possible design patterns, but only those that have tested showed that it makes sense to put such typical implementations in the library (for example, an adapter, factory, etc. ) - but before I would like to know if there is already an existing library for this purpose, already available for Java ?.

I know that it is very difficult to completely generalize programming patterns in such a way that they can be reused in different implementations with complex requirements (for example, the composition of the templates, classes involved in several templates, etc.). However, in most cases, the required template instances are quite simple and standardized, and in many situations the execution of work can be slightly accelerated using such a library.

Thanks for your feedback.!

+7
source share
9 answers

It is for this reason that I created PerfectJPattern . Just remember to check and understand the code examples. They are available for download, as well as on the site documentation pages for each of the template implementations. If you read the GoF book, you will easily understand examples.

For example, if you want to use the Composite Pattern in your code, and if you use PerfectJPattern , you only need to specify which interface (general parameter and class instance) that you would like to use as Composite, and the rest is provided to you, see PerfectJPattern Composite At the bottom of this page is a working example that shows how to do this.

Another aspect that you should also consider is that in PerfectJPattern you don't have to reuse common template implementations (e.g. perfectjpattern-core Maven submodule), you also have the choice of only reusing a pure abstract level (perfectypattern- api Maven subodule) and provide the implementation yourself. In PerfectJPattern , you have the flexibility of reusing at different levels of abstraction, since there is a fine-grained layered structure also reflected in the structure of the Maven project. Reusing perfectjpattern-api gives you an abstract template directive if you want, which will help you speed up the implementation of your own Design Pattern templates . However, ideally, you should use it as much as possible.

Update. Following the comment below, it is worth noting that not all templates can be fully compotentized; see From templates to components . Some templates can only be partially detailed, and some others are not at all similar to the Singleton case. Singleton is too context sensitive and therefore you can find the PerfectJPattern interface. However, in PerfectJPattern, the following templates are completely components, for example. Observer, Command, Adapter, Decorator, Composite, Proxy, Visitor, DAO, etc.

+3
source

A design template is just ... templates. They are not classes ready to be used for everyone, but common concepts are found in several projects. This is why you will not find the design pattern API.

+4
source

I disagree with the other answers that reusable implementations cannot be created for design patterns. However, this may not always be easy and requires a lot of abstract programming.

From C #, I lacked the simplicity of an observer pattern in Java. (events in C #) After finishing a reusable general observer for Java, I came across PerfectJPattern .

It might be worth checking out.

A component template is essentially a context-independent , reusable, and type-safe version of the source template that covers at least as many uses as the source template, and this does not require developers to re-implement the same template code in each in a different context . Design patterns can be reused in terms of design, component patterns can be reused in terms of design and code .

Kudos to the desire to reduce any form of duplication. "Do not repeat yourself" is one of the most important principles of programming.


Since some additional design pattern arguments can be centralized in the library, I give you some additional examples:

+1
source

What you are looking for is PerfectJPattern .

Checkout dp4j . It allows you to implement the Singleton pattern with @Singleton and lazily initialize it with @Singleton(lazy=true) . This is a "collection of reusable component design patterns implemented in Java."

For Singleton, we recommend dp4j . To implement Singleton, you comment on your class with @Singleton and lazy to initialize it, you comment with @Singleton(lazy=true) .

+1
source

If you find that you are repeating similar code in several projects, it might be a good idea to extract fragments that are repeated in the reusable code library, in the hope of avoiding repetition in the future.

This is unlikely to lead to fully public implementations of design patterns.

0
source
0
source

Many templates are built into Java SE - you may not notice.

Decorator is all over java.io

The java.sql package is AbstractFactory.

All wrapper classes are Flyweights.

I agree with those who say I'm not looking for a template library. They must be detected when writing code.

0
source

This is possible in a language that offers higher order functions and some other things. Here are some slides that link to an article that discusses the origami library. It displays the "library code" for the following templates: composite, iterator, visitor, builder. So if you want to try this on jvm, you can today, just use scala. http://www.slideshare.net/remeniuk/algebraic-data-types-and-origami-patterns

0
source

Do you see a forest or trees?

Design patterns should become apparent, and not be selected in front. By the time they become apparent, you do not need a template because the code exists. Then you marvel at how the decision worked out, and your brain subconsciously chose the right approach. It gives you that warm, fuzzy feeling of "I trust this code."

If you create a library of design templates, you created yourself a big hammer (or a hammer factory, or a hammer factory factory [1]), and everything becomes a convenient nail (including screws), which leads to a lot of sore previews and spaghetti bowls in your development team .

[1] http://discuss.joelonsoftware.com/default.asp?joel.3.219431.12

-one
source