Home » Tag Archives: Design Patterns

Tag Archives: Design Patterns

Strategy Pattern in Java 8

java-logo

These are two examples on how to implement a Strategy pattern design using Java 8 functional style together with Cyclops pattern matching and Hamcrest libraries. PrintDependingOnInput method is a strategy that will System.println some message based on the log passed. AddPrefix is another strategy that will add a prefix to a message based on the message content. package com.marco.patternmatching; import ...

Read More »

Design Patterns in the 21st Century: The Chain of Responsibility Pattern

software-development-2-logo

Here’s a thing you might not see a lot. @Test public void hungryHungryPatrons() { KitchenStaff alice = new PieChef(); KitchenStaff bob = new DollopDistributor(); KitchenStaff carol = new CutleryAdder(); KitchenStaff dan = new Server(); alice.setNext(bob); bob.setNext(carol); carol.setNext(dan); Patron patron = new Patron(); alice.prepare(new Pie()).forPatron(patron); assertThat(patron, hasPie()); } It might look odd, but the idea is fairly common. For example, the ...

Read More »

Design Patterns in the 21st Century: The Adapter Pattern

java-logo

This is part three of my talk, Design Patterns in the 21st Century. The Adapter pattern bridges worlds. In one world, we have an interface for a concept; in another world, we have a different interface. These two interfaces serve different purposes, but sometimes we need to transfer things across. In a well-written universe, we can use adapters to make ...

Read More »

Design Patterns in the 21st Century: The Abstract Factory Pattern

java-logo

This is part two of my talk, Design Patterns in the 21st Century. This pattern is used everywhere in Java code, especially in more “enterprisey” code bases. It involves an interface and an implementation. The interface looks something like this: public interface Bakery { Pastry bakePastry(Topping topping); Cake bakeCake(); } And the implementation: public class DanishBakery implements Bakery { @Override ...

Read More »

Functional Factory Pattern

java-design-patterns-logo_scaled

Do you want a REALLY quick way to make a Factory object? Then lambdas or other function-passing is what you need! Not only is it quick, it’s really simple. I bet, if you’re a pretty good with Lambdas, you have a pretty good idea on how to do this simply by having read the title. If you’re one of those, ...

Read More »

How The Decorator Pattern Saved My Day

java-design-patterns-logo_scaled

At work I am dealing with a huge Java code base, which was developed over the course of more than 15 years by many different developers. Not all things were done by the books but at the same time I usually don’t have the possibility to refactor every oddity I come across. Still, steps towards higher code quality can be ...

Read More »

The Serialization Proxy Pattern

java-design-patterns-logo_scaled

In my last post, I talked about serialization in general. This one is much more focused and presents a single detail: the Serialization Proxy Pattern. It is a good, often the best, way to deal with many of the issues with serialization. If there was only one thing a developer would want to know about the topic, I’d tell him ...

Read More »

The Decorator Pattern With Java 8

java-design-patterns-logo_scaled

In a recent post I described how the decorator pattern saved my day. I gave a small code snippet which contained the simplest way to create decorators but promised that there would be a nicer way with Java 8. Here it is:               Decorating With Java 8 HyperlinkListener listener = this::changeHtmlViewBackgroundColor; listener = DecoratingHyperlinkListener.from(listener) ...

Read More »

Transforming Collections with Decorators

java-design-patterns-logo_scaled

The Decorator Pattern Ever since first learning the programming design patterns, the decorator pattern has been my favorite. It seemed such a novel idea to me, and so much more interesting than the others. Don’t get me wrong, most of the others blew my mind too, but none so much as the decorator pattern. To this day, it’s still one ...

Read More »

REST Messages And Data Transfer Objects

software-development-2-logo

In Patterns of Enterprise Application Architecture, Martin Fowler defines a Data Transfer Object (DTO) as: An object that carries data between processes in order to reduce the number of method calls. Note that a Data Transfer Object is not the same as a Data Access Object (DAO), although they have some similarities. A Data Access Object is used to hide ...

Read More »
Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.