Facade (design pattern)

format_list_bulleted Contenido keyboard_arrow_down
ImprimirCitar
Facade UML class diagram.svg

Facade (Facade) is a type of structural design pattern. It is motivated by the need to structure a programming environment and reduce its complexity by dividing it into subsystems, minimizing communications and dependencies between them.

Application Considerations

The facade pattern will be applied when you need to provide a simple interface for a complex subsystem, or when you want to structure several subsystems in layers, since the facades would be the entry point to each level. Another prone scenario for its application arises from the need to decouple a system from its clients and from other subsystems, making it more independent, portable and reusable (that is, reducing dependencies between subsystems and clients).

Structure

It can be seen in the following figure:

Facade UML class diagram.svg

Here is an example:

FacadeDesignPattern.png

Participants

Facade (Facade): Knows which subsystem classes are responsible for a given request, and delegates those client requests to the appropriate subsystem objects.

Subclasses (ModuleA, ModuleB, ModuleC...): implement the functionality of the subsystem. They carry out the work requested by the facade. They do not know the existence of the facade.

Collaborations

Clients communicate with the subsystem by sending requests to the Facade object, which forwards them to the appropriate subsystem objects.

The subsystem objects do the final work, and the facade does some of the work to get from its interface to the subsystem's.

Clients using the facade do not have to access subsystem objects directly.

Advantages and disadvantages

The main advantage of the facade pattern is that to modify the classes of the subsystems, you only have to make changes to the interface/facade, and clients can remain oblivious to it. Furthermore, and as mentioned above, clients do not need to know the classes behind that interface.

On the downside, if you consider the case that multiple clients need to access different subsets of the functionality provided by the system, they might end up using only a small part of the façade, so it would be convenient to use several more specific façades in instead of a single global.

Related Patterns

One of the more directly related patterns is the singleton, since on certain occasions the facades can be unique instances.

Other patterns that are somewhat related to the facade pattern are the GRASP (General Responsibility Assignment Software Patterns), which are not design patterns, but good practices that guide the developer to find the design patterns, which are more concrete. One of the GRASP patterns is a controller that acts as an entry point into the logic layer, which can be nicely compared to using the facade pattern.

Known Uses (Issues/Solutions)

Problem: A client needs to access some of the functionality of a more complex system.

  • Define an interface that allows access only to that functionality.

Problem: There are very frequent groups of tasks for which you can create simpler and more readable code.

  • Define functionality that group these tasks into simple and clear functions or methods.

Problem: A library is hardly readable.

  • Create a more readable intermediary.

Problem: Dependency between client code and internal part of a library.

  • Create an intermediary and make calls to the library only or, above all, through it.

Problem: Need to access a set of APIs that may also have a not very good design.

  • Create a well-designed, intermediate API that allows access to the functionality of others.

Problem: Many client classes want to use several server classes, and need to know exactly which one provides each service. The system would become very complex, because all the client classes would have to be related to each and every one of the server classes.

  • Create one or more Facade classes, which implement all services, so that or all customers use that single class, or that each group of customers use the facade that best fits your needs.

Examples of use

In Java the classes java.awt.Graphics and java.awt.Font.

In the following example implemented in Java, you can see how the problem of making code more readable and not repeating code for the most frequent tasks is solved.

Implementation (Java)

 package com.genbetadev; public class Printer {private String TypeDocument;private String leaf;private Boolean color;private String text;public String getTipoDocumento() {return TypeDocument; ! public void setTypeDocument(String TypeDocument) {this.TypeDocument = TypeDocument;!public void setHome(String leaf) {this.leaf = leaf;!public String getHoja() {return leaf;!public void setColor(Boolean color) {this.color = color;!public Boolean getColor() {return color;!public void setText(String text) {this.text = text;!public String getText() {return text;!public void Print() {printer.PrintDocument();! !

This is a simple class that prints documents in one format or another. The client class code will help us better understand how it works.

 package com.genbetadev; public class Principal {public static void main(String[] args) {Printer i = new Printer();i.setHome("a4");i.setColor(bar);i.setTypeDocument("pdf");i.setText("text 1");i.PrintDocument();Printer i2 = new Printer();i2.setHome("a4");i2.setColor(bar);i2.setTypeDocument("pdf");i2.setText("text 2");i2.PrintDocument();Printer i3 = new Printer();i3.setHome("a3");i3.setColor(false);i3.setTypeDocument("excel");i3.setText("text 3");i3.PrintDocument();! !

As we can see, the client class is in charge of invoking the printer, and configuring it to later print various documents. Now, practically all the documents we write have the same structure (A4 format, Color PDF). We are continually iterating code. We are going to build a new class NormalFacadePrinter that simplifies the printing of the most common documents.

 package com.genbetadev; public class FacadeImpressorNormal {Printer printer;public FacadeImpressorNormal(String text) {super();printer= new Printer();printer.setColor(bar);printer.setHome("A4");printer.setTypeDocument("PDF");printer.setText(text); !public void Print() {printer.PrintDocument(); ! !

This way the client will be much easier:

 package com.genbetadev; public class Principal {public static void main(String[] args) {FacadeImpressorNormal facade1= new FacadeImpressorNormal("text1");facade1.Print();FacadeImpressorNormal facade2= new FacadeImpressorNormal("text2");facade2.Print();Printer i3 = new Printer();i3.setHome("a4");i3.setColor(bar);i3.setTypeDocument("excel");i3.setText("text 3");i3.PrintDocument();! !

Contenido relacionado

Web server

A web server or HTTP server is a computer program that processes an application on the server side, making bidirectional or unidirectional synchronous or...

Template:Google

Small Computer System Interface

A small computer system interface, better known by the English acronym SCSI is a standard interface for transferring data between different devices on the...
Más resultados...
Tamaño del texto:
undoredo
format_boldformat_italicformat_underlinedstrikethrough_ssuperscriptsubscriptlink
save