Intention
Provides a consistent interface for a set of interfaces in a subsystem, and the façade pattern defines a high-level interface that makes the subsystem easier to use.
Scene
In a site that is recharging the game, creating orders requires dealing with three external interfaces:
L User system: Get the User ID according to the username, see if the user has activated the game
L Card System: see if some type of recharge card is still in stock
L Recharge System: Create an order and return the order number
If a Web site is coupled directly to three external interfaces, then the site because of the external System interface modification of the probability is very large, and these small interfaces are not very friendly, they provide most of the tool method, specific how to use or to see the recharge site to create orders logic.
The façade's idea is to encapsulate a high-level interface on a small interface, masking calls to the sub-interface, and providing an externally simpler and more accessible interface.
Sample code
using System;
using System.Collections.Generic;
using System.Text;
namespace Facadeexample
{
Class Program
{
static void Main (string[] args)
{
PAYFACACDE pf = new Payfacacde ();
Console.WriteLine ("Order:" + PF.) Createorder ("Yzhu", 0, 1,) + "created");
}
}
class PAYFACACDE
{
Private Accountsystem account = new Accountsystem ();
Private Cardsystem card = new Cardsystem ();
private Paysystem pay = new Paysystem ();
public string Createorder (string userName, int cardid, int cardcount, int areaid)
{
int UserID = account. Getuseridbyusername (UserName);
if (UserID = 0)
return string. Empty;
if (!account. Userisactived (UserID, areaid))
return string. Empty;
if (!card. Cardhasstock (Cardid, cardcount))
return string. Empty;
return pay. Createorder (UserID, Cardid, Cardcount);
}
}
class Accountsystem
{
public bool Userisactived (int userID, int areaid)
{
return true;
}
public int Getuseridbyusername (string userName)
{
return 123;
}
}
class Cardsystem
{
public bool Cardhasstock (int cardid, int cardcount)
{
return true;
}
}
class Paysystem
{
public string Createorder (int userID, int cardid, int cardcount)
{
return "0000000001";
}
}
}