Read the description of other patterns.
Problem
Minimize the dependence of subsystems of a complex system and the exchange of information between them.
Description
When designing complex systems, so-called principle of decomposition, in which a complex system is divided into smaller and simpler subsystems. Moreover, the level of decomposition (its depth) is determined solely by the designer. Thanks to this approach, the individual components of the system can be developed in isolation, then integrated together. However, the obvious problem appears at first glance - the high connectivity of the system modules. This is manifested, first of all, in a large amount of information that modules exchange with each other. In addition, for such communication, some modules must have sufficient information about the nature of other modules.
')
Thus, minimizing the dependence of subsystems, as well as reducing the amount of information transmitted between them, is one of the main design tasks.
One of the ways to solve this problem is to use the “Facade” pattern.
The “Facade” pattern provides a unified interface instead of a set of interfaces of a certain subsystem. The facade defines a higher level interface that
ry simplifies the use of the subsystem.
Simply put, “Facade” is nothing more than a certain object that accumulates in itself a high-level set of operations for working with some complex subsystem. The facade aggregates the classes that implement the functionality of this subsystem, but does not hide them. It is important to understand that the client, at the same time, does not lose a lower level access to the classes of the subsystem, if it is, of course, necessary for him. The facade simplifies some operations with the subsystem, but does not impose its use on the client.
Practical task
Using the “Facade” pattern, we implement a unified interface to a certain user authorization subsystem. The authorization subsystem itself (in this example) certainly does not pretend to a “complex system”, but it clearly reflects the main advantages of the pattern.
Class diagram
Consider a chart. The authorization subsystem framework, for clarity, is highlighted in a rectangle. Facade Authorizator provides the client with a unified interface for working with the subsystem. In this case, this is just one method - authorizate (), but there could be more. In this case, the client can use the facade to work with the subsystem, and can directly use the classes that make it up. The authorization process itself is quite simple. Based on the username, the corresponding entry is searched for in the database using the DB interface. Then, the password of the found record is compared with the password by the specified user.

Implementation on C #
There is
no PgSQLDB class in the implementation code.
using System;
using System.Collections. Generic ;
using System.Linq;
using System.Text;
using System.Security;
namespace Facade
{
//
abstract class User
{
protected string username;
protected string passwd;
public abstract string getUserRole();
public string getPasswdHash()
{
// - .
// , -
return passwd.GetHashCode().ToString();
}
}
// , -
class DefaultUser : User
{
public DefaultUser( string username, string passwd)
{
this .username = username;
this .passwd = passwd;
}
public override string getUserRole()
{
return "DEFAULT_USER" ;
}
}
// ,
class Administrator : User
{
public Administrator( string username, string passwd)
{
this .username = username;
this .passwd = passwd;
}
public override string getUserRole()
{
return "ADMINISTRATOR" ;
}
}
//
interface DB
{
User search( string username);
}
// SQLite
class SQLiteDB : DB
{
public SQLiteDB( string filename)
{
//
}
public User search( string username)
{
//
throw new NotImplementedException();
}
}
// -
class Authorizator
{
public Authorizator()
{
//
}
//
public void authorizate( string username, string passwd)
{
DB db = new SQLiteDB( "db.sqlite" );
User user = db.search(username);
if (user.getPasswdHash() == passwd)
{
// ,
}
else
{
// -
throw new SecurityException( "Wrong password or username!" );
}
}
}
class Program
{
static void Main( string [] args)
{
//
string username = "Vasya" ;
string passwd = "qwerty" .GetHashCode().ToString();
Authorizator auth = new Authorizator();
try
{
auth.authorizate(username, passwd);
}
catch (SecurityException ex)
{
//
}
}
}
}
* This source code was highlighted with Source Code Highlighter .
PS : Does one habraeditor not work for me?