设计模式(12)-外观模式
来源:互联网 发布:什么是数据流程分析 编辑:程序博客网 时间:2024/06/05 08:58
外观模式
外观模式属于结构型的设计模式,其特点是为外界调用提供一个统一的接口,把其他类中需要用到的方法提取出来,由外观类进行调用。
外观模式可以使客户更加容易调用相关代码,不需要了解相关的过程,方便客户使用,是常被使用的一种设计模式。
代码
此处以visual studio中的Build为例子进行外观模式的说明,Build的过程是包括了3个基本步骤,预处理->编译->链接,此处的Build即为外观,而预处理->编译->链接则为其他类,用户在使用visual studio时只需要点集Build即可,无需亲自按顺序运行预处理->编译->链接。
C++代码
文件结构:
—include
——Preprocesser.h
——Compiler.h
——Linker.h
——Facade.h
—src
——Preprocesser.cpp
——Compiler.cpp
——Linker.cpp
——Facade.cpp
——main.cpp
代码如下:
—include/Preprocesser.h
#ifndef _PREPROCESSER_H_#define _PREPROCESSER_H_class Preprocesser{public: static void Preprocess();};#endif
—include/Compiler.h
#ifndef _COMPILER_H_#define _COMPILER_H_class Compiler{public: static void Compile();};#endif
—include/Linker.h
#ifndef _LINKER_H_#define _LINKER_H_class Linker{public: static void Link();};#endif
—include/Facade.h
#include"Preprocesser.h"#include"Compiler.h"#include"Linker.h"class Facade{public: void Build();};
—src/Preprocesser.cpp
#include"Preprocesser.h"#include<iostream>using namespace std;void Preprocesser::Preprocess(){ cout<<"代码预处理中..."<<endl;}
—src/Compiler.cpp
#include"Compiler.h"#include<iostream>using namespace std;void Compiler::Compile(){ cout<<"代码编译中..."<<endl;}
—src/Linker.cpp
#include"Linker.h"#include<iostream>using namespace std;void Linker::Link(){ cout<<"代码链接中..."<<endl;}
—src/Facade.cpp
#include"Facade.h"void Facade::Build(){ Preprocesser::Preprocess(); Compiler::Compile(); Linker::Link();}
—src/main.cpp
#include"Facade.h"int main(){ Facade facader; facader.Build(); return 0;}
Python代码
文件结构:
—Preprocesser.py
—Compiler.py
—Linker.py
—Facade.py
代码如下:
—Preprocesser.py
# -*- coding:utf-8 -*-class Preprocesser: @staticmethod def Preprocess(): print "代码预处理中..."
—Compiler.py
# -*- coding:utf-8 -*-class Compiler: @staticmethod def Compile(): print "代码编译中..."
—Linker.py
# -*- coding:utf-8 -*-class Linker: @staticmethod def Link(): print "代码链接中..."
—Facade.py
# -*- coding:utf-8 -*-from Preprocesser import *from Compiler import *from Linker import *class Facade: def Build(self): Preprocesser.Preprocess() Compiler.Compile() Linker.Link()if "__main__" == __name__: facader = Facade() facader.Build()
- 设计模式:12 外观模式
- 设计模式(12)-外观模式
- 设计模式--外观模式
- 设计模式-外观模式
- 设计模式:外观模式
- 设计模式----外观模式
- 设计模式------外观模式
- 设计模式 外观模式
- 设计模式-【外观模式】
- 设计模式-----外观模式
- 设计模式 - 外观模式
- 设计模式 - 外观模式
- 设计模式- 外观模式
- 设计模式-外观模式
- 设计模式 - 外观模式
- 设计模式-外观模式
- 设计模式 - 外观模式
- 设计模式-外观模式
- 雪花,是冬天凝固的泪
- linux中bg、fg、ctrl+z、ctrl+d和ctrl+c的区别
- javascript笔记整理系列
- nodejs+短信服务+网易云信
- 大并发量需要注意的问题
- 设计模式(12)-外观模式
- Android音频系统
- 用Java DIY 函数式方法—— map
- java基础复习之多线程Thread 十三
- 没有谁的幸福值得你去羡慕
- markdown
- 时间格式转换
- JNI基础语法1
- 获取绝对路径