asp.net

asp.net,c#
结构型模式 外观模式
Definition

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.
UML class diagram
facade[1].gif
   Participants


   The classes and/or objects participating in the Façade pattern are:

  • Facade   (MortgageApplication)
    • knows which subsystem classes are responsible for a request.
    • delegates client requests to appropriate subsystem objects.
  • Subsystem classes   (Bank, Credit, Loan)
    • implement subsystem functionality.
    • handle work assigned by the Facade object.
    • have no knowledge of the facade and keep no reference to it.

Sample code in C# 
   

This structural code demonstrates the Facade pattern which provides a simplified and uniform interface to a large subsystem of classes.

// Facade pattern -- Structural example

using System;

// "SubSystem"


class
SubSystemOne

{
  public void MethodOne()
  {
    Console.WriteLine("SubSystemOne Method");
  }
}

class
SubSystemTwo

{
  public void MethodTwo()
  {
    Console.WriteLine("SubSystemTwo Method");
  }
}

class
SubSystemThree

{
  public void MethodThree()
  {
    Console.WriteLine("SubSystemThree Method");
  }
}

class
SubSystemFour

{
  public void MethodFour()
  {
    Console.WriteLine("SubSystemFour Method");
  }
}

// "Facade"


class
Facade

{
  // Fields
  SubSystemOne one;
  SubSystemTwo two;
  SubSystemThree three;
  SubSystemFour four;

 
// Constructors
  public Facade()
  {
    one = new SubSystemOne();
    two = new SubSystemTwo();
    three = new SubSystemThree();
    four = new SubSystemFour();
  }

 
// Methods
  public void MethodA()
  {
    Console.WriteLine( "\nMethodA() ---- " );
    one.MethodOne();
    two.MethodTwo();
    four.MethodFour();
  }

  public
void MethodB()

  {
    Console.WriteLine( "\nMethodB() ---- " );
    two.MethodTwo();
    three.MethodThree();
  }
}

///
<summary>

/// Client test
/// </summary>
public class Client
{
  public static void Main(string[] args)
  {
   
// Create and call Facade
    Facade f = new Facade();
    f.MethodA();
    f.MethodB();

  }
}

Output
MethodA() ----
SubSystemOne Method
SubSystemTwo Method
SubSystemFour Method

MethodB() ----
SubSystemTwo Method
SubSystemThree Method



This real-world code demonstrates the Facade pattern as a MortgageApplication object which provides a simplified interface to a large subsystem of classes measuring the creditworthyness of an applicant.

// Facade pattern -- Real World example

using System;

// "SubSystem ClassA"


class
Bank

{
  // Methods
  public bool SufficientSavings( Customer c )
  {
    Console.WriteLine("Check bank for {0}", c.Name );
    return true;
  }
}

// "SubSystem ClassB"


class
Credit

{
  // Methods
  public bool GoodCredit( int amount, Customer c )
  {
    Console.WriteLine( "Check credit for {0}", c.Name );
    return true;
  }
}

// "SubSystem ClassC"


class
Loan

{
  // Methods
  public bool GoodLoan( Customer c )
  {
    Console.WriteLine( "Check loan for {0}", c.Name );
    return true;
  }
}

class
Customer

{
  // Fields
  private string name;

 
// Constructors
  public Customer( string name )
  {
    this.name = name;
  }

 
// Properties
  public string Name
  {
    get{ return name; }
  }
}

// "Facade"


class
MortgageApplication

{
  // Fields
  int amount;
  private Bank bank = new Bank();
  private Loan loan = new Loan();
  private Credit credit = new Credit();

 
// Constructors
  public MortgageApplication( int amount )
  {
    this.amount = amount;
  }

 
// Methods
  public bool IsEligible( Customer c )
  {
   
// Check creditworthyness of applicant
    if( !bank.SufficientSavings( c ) ) 
      return
false;
    if( !loan.GoodLoan( c ) ) 
      return
false;
    if( !credit.GoodCredit( amount, c )) 
      return
false;

    return true;
  }
}
/// <summary>
/// Facade Client
/// </summary>
public class FacadeApp
{
  public static void Main(string[] args)
  {
    // Create Facade
    MortgageApplication mortgage =
                new MortgageApplication( 125000 );
    // Call subsystem through Facade
    mortgage.IsEligible(
             new Customer( "Gabrielle McKinsey" ) );

 
}

}

Output
Check bank for Gabrielle McKinsey
Check loan for Gabrielle McKinsey
Check credit for Gabrielle McKinsey

posted on 2007-03-08 20:23  灵魂边缘  阅读(239)  评论(0编辑  收藏  举报