[Design mode sorting Note 9] appearance mode (facade)

Source: Internet
Author: User
Tags define abstract

[Guide]

[Design mode sorting Note 1] basic knowledge

[Design mode sorting Note 2] simple factory Mode)

[Design mode sorting Note 3] factory Mode)

[Design pattern sorting Note 4] abstract factory pattern (Abstract Factory)

[Design pattern sorting Note 5] creator pattern (builder)

[Design pattern arrangement Note 6] Summary of factory pattern and creator Pattern

[Design mode sorting Note 7] prototype)

[Design mode sorting Note 8] Singleton)

[Design mode sorting Note 9] appearance mode (facade)

... Later, including some examples

[/Guide]

 

The design of the appearance mode is very simple. It is to package an object, and the external interfaces are provided by the appearance class. It consists of three parts: Abstract appearance class, specific appearance class, and other users. For example, in the above wage calculation module, not only does one system need the real salary of an individual, but the internal website of the Group also needs the personal salary information for the employee to query? At this time, an interface is required for the Group intranet to use. The returned data is the final data, which does not need to be calculated or assembled in any system outside the appearance class for future maintenance. Next let's take a look.CodeExample:

Design the total salary calculation class for Shenzhen subsidiary:

Code

Namespace Consoleapp
{
Public   Interface Isalary
{
Decimal Commandsalary ();
}
}

namespace consoleapp
{< br> Public class shenzhensalary: isalary
{< br> Public decimal commandsalary ()
{< br> /// here is the computing process .. saved

//Assume that the total salary is 10000 yuan.
Return 10000;
}
}
}

Designed fund computing for Shenzhen subsidiaries:

Code

Namespace Consoleapp
{
Public   Interface Ifund
{
Decimal Commandfund ();
}
}

namespace consoleapp
{< br> Public class shenzhenfund: ifund
{< br> Public decimal commandfund ()
{< br> /// here is the computing process .. saved

//Assume that 180 is returned.
Return 180;
}
}
}

 

Design the tax calculation class for Shenzhen subsidiary:

Code

Namespace Consoleapp
{
Public   Interface Itax
{
Decimal Commandtax ();
}
}

namespace consoleapp
{< br> Public class shenzhentax: itax
{< br> Public decimal commandtax ()
{< br> /// here is the computing process .. saved

//Assume that 580 is returned.
Return 580;
}
}
}

 

Define abstract appearance. When an employee queries his or her real salary on the Intranet, the following information is displayed: total salary, the Fund to be deducted, the tax to be deducted, and the individual's real salary. The class is defined as follows:

Code

Namespace Consoleapp
{
///   <Summary>
/// Abstract appearance
///   </Summary>
Public   Interface Iemployeepay
{
///   <Summary>
/// Total salary
///   </Summary>
///   <Returns> </returns>
Decimal Salary ();
///   <Summary>
/// Funds to be deducted
///   </Summary>
///   <Returns> </returns>
Decimal Fund ();
///   <Summary>
/// Tax to be deducted
///   </Summary>
///   <Returns> </returns>
Decimal Tax ();
///   <Summary>
/// Individual real salary
///   </Summary>
///   <Returns> </returns>
Decimal Pay ();
}
}

 

The specific appearance class is defined as follows:

Code

Namespace Consoleapp
{
///   <Summary>
/// The real salary of the employee. This class is the specific appearance class.
///   </Summary>
Public   Class Employeepay: iemployeepay
{
///   <Summary>
/// Total salary acquisition
///   </Summary>
Public   Virtual   Decimal Salary ()
{
Shenzhensalary salarysz =   New Shenzhensalary ();
Return Salarysz. commandsalary ();
}

///   <Summary>
/// Obtain the Fund to be deducted
///   </Summary>
Public   Virtual   Decimal Fund ()
{
Shenzhenfund fundsz =   New Shenzhenfund ();
Return Fundsz. commandfund ();
}

///   <Summary>
/// Tax to be deducted
///   </Summary>
Public   Virtual   Decimal Tax ()
{
Shenzhentax taxsz =   New Shenzhentax ();
Return Taxsz. commandtax ();
}

///   <Summary>
/// Earn real salary
///   </Summary>
///   <Returns> </returns>
Public   Virtual   Decimal Pay ()
{
Employeepay =   New Employeepay ();
Decimal Salary = Employeepay. Salary ();
Decimal Fund = Employeepay. Fund ();
Decimal Tax = Employeepay. Tax ();

ReturnSalary-Fund-Tax;
}
}
}

 

The group company intranet is called as follows:

Code

Namespace Consoleapp
{
Class Program
{
///   <Summary>
/// External calls do not require any calculation. When you need to change the calculation method,
/// You can modify the abstract appearance class and the specific appearance class, so that it can be used by N users,
///   </Summary>
///   <Param name = "ARGs"> </param>
Public   Static   Void Main ( String [] ARGs)
{
Iemployeepay employeepay =   New Employeepay ();

Console. writeline ( " Total salary: "   + Employeepay. Salary ());
Console. writeline ( " The funds to be deducted are: "   + Employeepay. Fund ());
Console. writeline ( " The tax to be deducted is: "   + Employeepay. Fund ());
Console. writeline ( " My real salary is: "   + Employeepay. Pay ());

Console. Readline ();
}
}
}

 

Other systems, such as the group information platform, the group financial system, the Group Tax Return System, and the group fund system, are called in this way. When logical changes occur, the work is completed only when the appearance class is modified, no modification is required to the external subsystem. It seems similar to the principle of Web Services. In fact, everyone should have used this design idea. If you do not know its name, remember it as the appearance pattern.

 

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.