外观模式

本节主要内容:1.外观模式的意图;2.UML图;3.应用举例

一、外观模式意图:为子系统中的一组接口提供一个一致的接口。这个接口定义了一个高层接口,这个接口使得这一子系统更加容易使用

Provide a unified interface to a set of interface in a subsystem.Facade defines a higher-lever interface that make the subsystem easier to use.

二、UML图

 原型如左图,使用外观模式之后如右图。

三、代码示例:

应用举例:写代码中常遇见的情景:一个系统的一般的三步走:需求分析形成文档、代码编写、测试。版本发布。

AnalysisApp类

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace y.PatternFacade
7 {
8 class AnalysisApp
9 {
10 public void AnalysisDoc()
11 {
12 Console.WriteLine("Complete Document!");
13 }
14 }
15 }

CodeApp类:

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace y.PatternFacade
7 {
8 class CodeApp
9 {
10 public void DesignCode()
11 {
12 Console.WriteLine("Complete Code!");
13 }
14 }
15 }

TestApp类:

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace y.PatternFacade
7 {
8 class TestApp
9 {
10 public void TestExample()
11 {
12 Console.WriteLine("Complete Test!");
13 }
14 }
15 }

FacadeApp类

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace y.PatternFacade
7 {
8 class FacadeApp
9 {
10 private AnalysisApp analysisApp;
11 private CodeApp codeApp;
12 private TestApp testApp;
13 public FacadeApp()
14 {
15 analysisApp = new AnalysisApp();
16 codeApp = new CodeApp();
17 testApp = new TestApp();
18 }
19 public void ShowApp()
20 {
21 analysisApp.AnalysisDoc();
22 codeApp.DesignCode();
23 testApp.TestExample();
24 }
25 }
26 }

Boss类

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace y.PatternFacade
7 {
8 class Boss
9 {
10 public void GetAppResult()
11 {
12 FacadeApp facadeApp = new FacadeApp();
13 facadeApp.ShowApp();
14 }
15 }
16 }

控制台

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace y.PatternFacade
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 Console.Title = "外观模式";
13 Console.WriteLine("Press any key to exit......\r\n");
14 Boss boss = new Boss();
15 boss.GetAppResult();
16 Console.Read();
17 }
18 }
19 }

运行结果如下:

四、源码这里下载

参考文献:http://www.dofactory.com/Patterns/PatternFacade.aspx

 

 

posted @ 2011-11-24 17:18  走过留痕  阅读(971)  评论(0编辑  收藏  举报