Categories: Design Pattern

Structural Patterns – Facade

Facade – A single class that represents an entire subsystem that hides the system complexity.

From Design Patterns – Structural Patterns

Structure

Facade pattern – Class diagram UML

Applicability

  • Suppose the developers need to have a limited straightforward interface object to a complex sub-system. In that case, the developers should use this pattern.
  • If the developers want to structure sub-system layers, the developers should use this pattern.

Pros and Cons

PROS

  • It can isolate the source code from the complexity sub-systems.

CONS

  • A facade is going to become a good object coupled to all classes of an app.

How to Implement

#1 Basic Sample

class Program
{
 static void Main(string[] args)
 {
  SubSystem1 subSystem1 = new SubSystem1();
  SubSystem2 subSystem2 = new SubSystem2();

  Facade facade = new Facade(subSystem1, subSystem2);

  Console.WriteLine(facade.Operation());

  Console.ReadKey();

 }
}

public class Facade
{
 protected SubSystem1 SubSystem1;
 protected SubSystem2 SubSystem2;

 public Facade(SubSystem1 _subSystem1, SubSystem2 _subSystem2)
 {
  this.SubSystem1 = _subSystem1;
  this.SubSystem2 = _subSystem2;
 }

 public string Operation()
 {
  string Result = "Facade initializes subsystem:\n";
  Result += this.SubSystem1.OperationSub1();
  Result += this.SubSystem2.OperationSub2();
  Result += "Facade orders subsystem to perform the action:\n";
  Result += this.SubSystem1.OperationNSub1();
  Result += this.SubSystem2.OperationNSub2();

  return Result;
 }
}

public class SubSystem1
{
 public string OperationSub1() => $"Operation 1 from Subsystem1 \n";
 public string OperationNSub1() => $"Operation N from Subsystem1 \n";
}

public class SubSystem2
{
 public string OperationSub2() => $"Operation 1 from Subsystem2 \n";
 public string OperationNSub2() => $"Operation N from Subsystem2 \n";
}

#2 Web Application Project

Step 1 Create two services and the generic type repositories. These services will call the generic type repository’s interface object.

Fig 1 Facade – Single repository solution explorer

Step 2 Create a Facade class file, then implement two interfaces and create a new function for these interface operation processes.

Fig 2 Facade class file source code

Step 3 Create the Facade services in the Core project, then implement the Facade class file and create a new function for layout.

Fig 3 FacadeServices class file source code

Step 3 Register the related-facade pattern class file in the Startup.cs file.

services.AddScoped(typeof(IFacade<>), typeof(FacadeRepo<>));
services.AddScoped(typeof(FacadeServices));

Step 4 Create the Controller files for the Facade pattern in the web project.

Fig 4 Facade pattern result

Reference

davidsky69

View Comments

Recent Posts

API Gateway in .NET 5 with Ocelot

What is the API gateway? An API gateway is an API management tool that sits…

4 years ago

.NET 5 application with Onion architecture

The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…

4 years ago

SOLID Principles – Dependency inversion principle

In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…

4 years ago

SOLID Principles – Interface segregation principle

In the field of software engineering, the interface segregation principle (ISP) states that no code…

4 years ago

SOLID Principles – Liskov substitution principle

Subtype Requirement: Let  be a property provable about objects  of type T. Then  should be true for objects  of type S where S is…

4 years ago

SOLID Principles – Open-closed principle

In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…

4 years ago