
Java programmer expert
Today we will take a look at another Java design pattern from the structural patterns category – Facade. Design patterns in this category deal with class structure such as inheritance and composition.
Facade is a design pattern that provides a simplified interface for interacting with a complex system that may consist of multiple classes, interfaces, and components. It is often used in conjunction with the Singleton design pattern.
The Facade design pattern solves the complexity problem by providing a simple unified interface as an approach to the client that hides the details of the inside of the system, which can be difficult to understand and navigate due to the many classes and interdependencies. The advantage of the Facade is that the complex system can be changed, regardless of the client, who only communicates through the Facade.
Now we will write a program to test the properties of the given numbers. In doing so, we use the Facade design pattern to hide the complexity of the calculations from the user.
The SystemA class contains the jePrvocislo() method, which determines whether the specified number is a prime number. A prime number is a number greater than 1 that has no deviders other than 1 and itself.
SystemA.java
package designpatterns;
public class SystemA {
public boolean jePrvocislo(int cislo) {
if (cislo <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(cislo); i++) {
if (cislo % i == 0) {
return false;
}
}
return true;
}
}
The SystemB class contains the jeFibonacihoCislo() method, which determines whether the specified number is a member of a Fibonacci sequence. The Fibonacci sequence is such that each number in the sequence is the sum of the two previous numbers (e.g. 0, 1, 1, 2, 3, 5, …).
SystemB.java
package designpatterns;
public class SystemB {
public boolean jeFibonacihoCislo(int cislo) {
int a = 0, b = 1, temp;
while (a < cislo) {
temp = a;
a = b;
b = temp + b;
}
return a == cislo;
}
}
SystemC contains the jeOdmocnitelne() method, which determines whether the specified number can be square root without a remainder. The square root is the number that multiplied by itself gives the original number.
SystemC.java
package designpatterns;
public class SystemC {
public boolean jeOdmocnitelne(int cislo) {
if (cislo < 0) {
return false;
}
int odmocnina = (int) Math.sqrt(cislo);
return odmocnina * odmocnina == cislo;
}
}
The Facade class combines the previous systems and their methods through the komplexnaOperacia() method, which parses the specified number and returns the result in a text string.
Facade.java
package designpatterns;
public class Fasada {
private SystemA systemA;
private SystemB systemB;
private SystemC systemC;
public Fasada() {
systemA = new SystemA();
systemB = new SystemB();
systemC = new SystemC();
}
public String komplexnaOperacia(int cislo) {
StringBuilder vysledok = new StringBuilder("Fasáda spúšťa analýzu čísla " + cislo + ":\n");
vysledok.append("Systém A: Je prvočíslo? ").append(systemA.jePrvocislo(cislo)).append("\n");
vysledok.append("Systém B: Je Fibonačiho číslo? ").append(systemB.jeFibonacihoCislo(cislo)).append("\n");
vysledok.append("Systém C: Dá sa číslo odmocniť? ").append(systemC.jeOdmocnitelne(cislo));
return vysledok.toString();
}
}
Main class is the entry point of the program. Creates an instance of the facade and calls its komplexnaOperacia() method on any given number. In this example, we set the number to 1. The result of this operation is then printed on the console.
Main.java
import designpatterns.Fasada;
public class Main {
public static void main(String[] args) {
Fasada fasada = new Fasada();
// Môžeš zmeniť na akékoľvek iné číslo
int zadaneCislo = 1;
String vysledok = fasada.komplexnaOperacia(zadaneCislo);
System.out.println(vysledok);
}
}
The Facade design pattern is used when we need to hide complicated and hard to understand code of complex systems. By integrating the Facade between the client and the system, the client can easily interact with the Facade, which ensures that complex operations are called correctly and hides system details.
We have prepared the files with the above example in the form of code that you can run directly in Java. Download the Java Facade code here.
Do you know the Java programming language? If you’re looking for a job as a Java developer, check out our employee benefits and respond to our latest job offers!