Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Facade and Factories (Don't Talk to Strangers)

DZone's Guide to

Java Facade and Factories (Don't Talk to Strangers)

Imagine yourself using API in all your projects for years. Everybody is using it. It is famous and mature. Suddenly, you have a huge production issue. What do you do?

· Performance Zone ·
Free Resource

Built by operators for operators, the Sensu monitoring event pipeline empowers businesses to automate their monitoring workflows and gain deep visibility into their multi-cloud environments. Get started for free today.

We all know the golden object-oriented rule: don't talk to strangers.

We all know that you will never find the API or framework's defects, magic, or limitations at the beginning of the development.

Imagine yourself using API in all your projects for years. Everybody else is using it, as well. It is famous and mature. Suddenly, you have a huge production issue. Changing that API is too hard and costly at this stage!

What now?

Or, maybe you decided to change any API or framework for new requirements. Let's take logging as an example. Now, we have log4j, logback, and JDK logging. At the beginning of the project. you decide to go with the logback since it's so famous and faster than log4j and it's bundled with Spring, etc.

After one year of the project, you got new requirements for the logging, which are:

  • All logs should be localized.
  • FATAL errors should be sent to a specific email.

Disaster, right?!

Examples of Famous Bugs in Very Famous Frameworks

Memory Leak

Performance

Deadlocks

How can we solve this issue? We simply need to follow three design patterns:

  1. Factory

  2. Wrapper

  3. Facade

...and use this flow:

Image title


Check out an example below:

Image title

The code can be found here. If you like it, please star and fork!

Download our guide to mitigating alert fatigue, with real-world tips on automating remediation and triage from an IT veteran.

Topics:
java ,performance ,factories ,facade

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}