Design Patterns — Facade Pattern

Faruk Karadeniz
2 min readOct 9, 2021

In this article we are implementing Facade design patter at our project.

You can find design patterns overview by clicking.

Facede Pattern

Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system. This type of design pattern comes under structural pattern as this pattern adds an interface to existing system to hide its complexities.This pattern involves a single class which provides simplified methods required by client and delegates calls to methods of existing system classes.

FacePattern — (https://www.tutorialspoint.com/design_pattern/facade_pattern.htm)

Think about you are developing a library facede pattern provides of abstracting the client from unnecessary detail.

Implementation

We used spring boot project for implementing facade design pattern. You can use simple java project for implementing facade design pattern.Let’s create a new simple project at https://start.spring.io/. We need common dependencies as below

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

First of all we are gonna create an interface with name IFileParser.

interface IFileParser {
String parse(String content);
}

We are gonna create interface and classes package private in facade pattern.Now let’s create parser classes.

JsonParser class;

class JsonParser implements IFileParser {

@Override
public String parse(String content) {
System.out.println("Contet : ".concat(content));
return "Im JSON parser";
}
}

HtmlParser class;

class HtmlParser implements IFileParser {

@Override
public String parse(String content) {
System.out.println("Contet : ".concat(content));
return "Im HTML parser";
}
}

XmlParser class;

class XmlParser implements IFileParser {

@Override
public String parse(String content) {
System.out.println("Contet : ".concat(content));
return "Im XML parser";
}
}

As you notice we create all classes and interface by package-private. But we need to reach this parser class’es parse method. How we are gonno do that ? By parser service of course :) Now let’s create a parser service for clients.

@Service
public class FileParserService {
private IFileParser htmlParser;
private IFileParser jsonParser;
private IFileParser xmlParser;

public FileParserService() {
htmlParser = new HtmlParser();
jsonParser = new JsonParser();
xmlParser = new XmlParser();
}

public String htmlParser(String content){
return htmlParser.parse(content);
}
public String jsonParser(String content){
return jsonParser.parse(content);
}
public String xmlParser(String content){
return xmlParser.parse(content);
}
}

All done. We are created faced pattern now we can create test cases.

@SpringBootTest
class FacedPatternTests {

@Autowired
private FileParserService fileParserService;

@Test
void chooseXmlParser() {
assertEquals("Im XML parser",fileParserService.xmlParser("XML"));
}

@Test
void chooseHtmlParser() {
assertEquals("Im HTML parser",fileParserService.htmlParser("HTML"));
}

@Test
void chooseJsonParser() {
assertEquals("Im JSON parser",fileParserService.jsonParser("JSON"));
}

@Test
void incorrenctFileTypeXmlParser() {
assertNotEquals("Im XML parser",fileParserService.htmlParser("XML"));
}

@Test
void incorrenctFileTypeHtmlParser() {
assertNotEquals("Im HTML parser",fileParserService.jsonParser("HTML"));
}

@Test
void incorrenctFileTypeJsonParser() {
assertNotEquals("Im JSON parser",fileParserService.xmlParser("JSON"));
}

}

You can find all project details at github.

Contibutors

Bahattin Yılmaz => Github, Linkedin, Medium

Faruk Karadeniz => Github , Linkedin

References

[1] https://www.tutorialspoint.com/design_pattern/index.htm

--

--