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

来源:互联网 发布:建筑工程记账软件 编辑:程序博客网 时间:2024/05/18 01:27
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();
    }

  }

}
   
原创粉丝点击