No nonsense C # design pattern eight: Façade

Source: Internet
Author: User

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";





        }





    }





}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.