Structural Design Pattern: Facade Pattern - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

demo-image

Structural Design Pattern: Facade Pattern

Share This
The Facade Pattern is a design principle that advocates for the provision of a single, streamlined interface to a collection of interfaces within a subsystem. This approach effectively conceals the intricacies of the subsystem from the client.

The Facade Pattern can be defined as an abstraction layer that provides a simplified interface for a complex subsystem.

Essentially, every Abstract Factory can be considered a subtype of Facade.
 
It protects the clients from the intricacies of the sub-system components. It facilitates a low level of dependency between subsystems and the entities that use them.
 
Application of the Facade Pattern:
  • When you desire to offer a straightforward interface for an intricate sub-system. 
  • When there are several dependencies between clients and the implementation classes of an abstraction.

Source code of ShapeFacade.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.t4b.test.java.dp.sp.fp;
class ShapeFacade {
interface Shape {
void draw();
}
class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Drawing Rectangle!");
}
}
class Square implements Shape {
@Override
public void draw() {
System.out.println("Drawing Square!");
}
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing Circle!");
}
}
private Shape circle = new Circle();
private Shape rectangle = new Rectangle();
private Shape square = new Square();
public ShapeFacade() {
}
public void drawCircle() {
circle.draw();
}
public void drawRectangle() {
rectangle.draw();
}
public void drawSquare() {
square.draw();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Source code of TestMain.java
1
2
3
4
5
6
7
8
9
10
11
12
package com.t4b.test.java.dp.sp.fp;
public class TestMain {
public static void main(String[] args) {
ShapeFacade shapeFacade = new ShapeFacade();
shapeFacade.drawCircle();
shapeFacade.drawRectangle();
shapeFacade.drawSquare();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.