设计模式[7] Facade Pattern 外观模式

a pattern that provides an unified interface to access a set of interfaces in a subsystem. a higher-level interface that makes the subsystem easier to use. Typical example: In web apps, we have business objects that wraps data access calls.

用到很多的一种模式。就是把下层的调用包装一下,
例如.net web项目中的.net object,最简单的例子:User,User.SignIn(string username, string password)
Web页面中只要调用这个方法就可以了,其他关于数据库连接、查询的事情都包在了这个方法里面。

看看示例代码:
  //  Facade pattern -- Structural example

using  System;

namespace  DoFactory.GangOfFour.Facade.Structural
{

  
// Mainapp test application

  
class MainApp
  
{
    
public static void Main()
    
{
      Facade facade 
= new Facade();

      facade.MethodA();
      facade.MethodB();

      
// Wait for user
      Console.Read();
    }

  }


  
// "Subsystem ClassA"

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

  }


  
// Subsystem ClassB"

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

  }


  
// Subsystem ClassC"

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

  }


  
// Subsystem ClassD"

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

  }


  
// "Facade"

  
class Facade
  
{
    SubSystemOne one;
    SubSystemTwo two;
    SubSystemThree three;
    SubSystemFour four;

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


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


    
public void MethodB()
    
{
      Console.WriteLine(
" MethodB() ---- ");
      two.MethodTwo();
      three.MethodThree();
    }

  }

}
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值