The Composite design pattern has the intention to represent part-whole as your friends on social media or a tower, as in the picture below.

Tokyo skytree tower

Why this pattern?

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly [1].

Using a composite structure, we can apply the same operations over both composites and individual objects. In other words, in most cases, we can ignore the differences between compositions of objects and individual objects.[2]

Examples

GOF

The diagrams below show how to identify this pattern and the participants.


When do you need to connect things, one of the options is to build a bridge, and this pattern is an example of it in code time upfront of integrations.

Forth Railway Bridge, Scotland
  • For more about this bridge, click here.

Motivation

Excerpt from GOF book[1],

When an abstraction can have one of several possible implementations, the usual way to accommodate them is to use inheritance. An abstract class defines the interface to the abstraction, and concrete subclasses implement it in different ways. But this approach isn’t always flexible enough. …


Working with a dev environment is kind of different from prod, and managing the changes should be too. To achieve the changes Liquibase is an option rather than Hibernate create-drop strategy.

Database tables

To maintain all DB schema changes into one place, Liquibase has these reasons.

  1. Version-controlled database schema changes
  2. Automatically orders scripts for deployment
  3. Branching and merging for teams
  4. Embeds into your product or build tools, like Jenkins
  5. Easily rollback changes

For this tutorial, the focus will be on item 1.


Write code is great, but sometimes is better generate code than type them. For this article I’ll present some options, such as Lombok and MapStruct.

Time is money

Why?

As I said before, sometimes writing a repeated piece of code is not a good idea, since this is a DRY (Don’t Repeat Yourself).

Back in time, to 2000’s 👴 working with EJB 2.x the tool to generated code was XDoclet with Apache Ant if I’m not wrong and I have to say, I don’t miss that time.

After the arrival of the Java Annotations changed the game.

Lombok uses the Java Annotations feature to…


Working with Lists is a common task in Java, but this task should be done carefully whether the unmodifiable or immutable is required.

Change
Change
Change

This article covers the change inside the elements of the List in order to avoid problems, for instance: Where is the element nth? Why did the element have one value and now has another value?

Retain the data as is, and don’t allow any element change, if required or a man in the middle.

The term unmodifiable means that the structure of the List itself cannot be altered[1].

If the elements in the List are mutable (which…


It was a kind 😆 of weird, handle all the Kubernetes (k8s) commands on my local environment and then I decided to install the k8s dashboard.

Expected result

Solve the problem to see a GUI instead of CLI. Although help when you’re starting on k8s and/or won’t have in mind all k8s commands, there are so many, check it out.

  • Has Helm installed
  • You’re using kind
  • If you’re using other local k8s, such as Minikube check the section Minikube.

Install the dashboard using the following steps:

  1. Helm install
helm install dashboard kubernetes-dashboard/kubernetes-dashboard -n kubernetes-dashboard --create-namespace

After for the umpteenth time type the command port-forward I noticed that I had to use Ingress 😜 and avoid the famous DRY (Don’t Repeat Yourself).

Disclaimer → Is not wrong use port-forward command, but if you can don’t remember it, looks great.

Assumptions → You’re familiar with docker and has installed the softwares below.

For this tutorial I’m using the following softwares

In terms of deploy, I’m using an application built with Quarkus, with 2 endpoints, /resteasy-jackson/quarks and /hello-resteasy. The image is available on my Docker hub.

This is an overview of those piece together…


Let’s play on my brand new video game? I can’t because my old television doesn’t support the video game interface. May you already face this situation someday.

Adapting…

Motivation

Excerpt from GOF book

How can existing and unrelated classes work in an application that expects classes with a different and incompatible interface? [1]

Applicability

Excerpt from GOF book,

you want to use an existing class, and its interface does not match the one you need.

you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don’t necessarily have compatible interfaces.

(object adapter only) you need…


Disclaimer ⚠️. Even though this pattern belongs to the code smell section in Java, worth knows it and recognize and know what is your purpose.

Below we can see the prototypes of Stormtrooper since for me they’re always equals.

Stormtrooper clones

Use the Prototype Pattern when a client needs to create a set of objects that are alike or differ from each other only in terms of their state and creating an instance of a such object (e.g., using the “new” keyword) is either expensive or complicated.[7]

Excerpt from GOF book,

Use the Prototype pattern when a system should be independent of…


This pattern works together with the factory method, my last article before it, and creates a family of objects. Based on that, let’s see your structure and how to implement it.

Abstract factory

To write this article I used the Gang Of Four, Head First Design Patterns, and Wikipedia. All the links are available in the References section.

Consider a user interface toolkit that supports multiple look-and-feel standards, such as Motif and Presentation Manager. Different look-and-feels define different appearances and behaviors for user interface “widgets” like scroll bars, windows, and buttons. To be portable across look-and-feel standards, an application should not hard-code…

Luiz Gustavo De O. Costa

Hi there 👋 , I’m Luiz Gustavo I’m a Brazilian and don’t play soccer very well, then I became a software developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store