当前位置:网站首页>Facade pattern
Facade pattern
2022-06-29 04:23:00 【Change with affection】
Appearance mode
Appearance mode (Facade Pattern) Hide the complexity of the system , And provides an interface for the client to access the system . This type of design pattern belongs to structural pattern , It adds an interface to an existing system , To hide the complexity of the system .
Cinema Management Project
Set up a home theater :
DVD player 、 Projector 、 Auto screen 、 Surround sound 、 Popcorn machine , It is required to complete the function of using home theater , The process is :
Directly with the remote control : Coordinate the switch of each equipment
Open the popcorn machine
Put down the screen
Turn on the projector
Turn on the stereo
open DVD, choose dvd
Go get the popcorn
Dim the lights
Play
After watching the movie , Shut down the devices
Case a : Traditional way to solve theater management

summary
- stay Test Of main In the method , Create objects for each subsystem , And call the subsystem directly ( object ) Related methods , It will cause confusion in the calling process , There is no clear process
- It's not good for ClientTest in , To maintain the operation of the subsystem
Case 2 : Appearance mode
- Appearance mode (Facade) , Also called “ Process mode : Appearance mode is one of the subsystems - The group interface provides - One - To the interface , This pattern defines a high-level interface , This interface makes this subsystem easier to use
- The facade pattern defines a consistent interface , To shield the details of the internal subsystem , Make the caller only need to call with this interface , You don't need to care about the internal details of this subsystem
public class DVDPlayer {
// Use singleton mode , Use the hungry man style
private static DVDPlayer instance = new DVDPlayer();
public static DVDPlayer getInstanc() {
return instance; }
public void on() {
System.out.println(" dvd on "); }
public void off() {
System.out.println(" dvd off "); }
public void play() {
System.out.println(" dvd is playing "); }
public void pause() {
System.out.println(" dvd pause .."); }
}
public class Popcorn {
private static Popcorn instance = new Popcorn();
public static Popcorn getInstance() {
return instance; }
public void on() {
System.out.println(" popcorn on "); }
public void off() {
System.out.println(" popcorn ff "); }
public void pop() {
System.out.println(" popcorn is poping "); }
}
public class Projector {
private static Projector instance = new Projector();
public static Projector getInstance() {
return instance; }
public void on() {
System.out.println(" Projector on "); }
public void off() {
System.out.println(" Projector ff "); }
public void focus() {
System.out.println(" Projector is Projector "); }
}
public class Screen {
private static Screen instance = new Screen();
public static Screen getInstance() {
return instance; }
public void up() {
System.out.println(" Screen up "); }
public void down() {
System.out.println(" Screen down "); }
}
public class Stereo {
private static Stereo instance = new Stereo();
public static Stereo getInstance() {
return instance; }
public void on() {
System.out.println(" Stereo on "); }
public void off() {
System.out.println(" Screen off "); }
public void up() {
System.out.println(" Screen up.. "); }
}
public class TheaterLight {
private static TheaterLight instance = new TheaterLight();
public static TheaterLight getInstance() {
return instance; }
public void on() {
System.out.println(" TheaterLight on "); }
public void off() {
System.out.println(" TheaterLight off "); }
public void dim() {
System.out.println(" TheaterLight dim.. "); }
public void bright() {
System.out.println(" TheaterLight bright.. "); }
}
public class HomeTheaterFacade {
// Define subsystem objects
private TheaterLight theaterLight;
private Popcorn popcorn;
private Stereo stereo;
private Projector projector;
private Screen screen;
private DVDPlayer dVDPlayer;
// Constructors
public HomeTheaterFacade() {
super();
this.theaterLight = TheaterLight.getInstance();
this.popcorn = Popcorn.getInstance();
this.stereo = Stereo.getInstance();
this.projector = Projector.getInstance();
this.screen = Screen.getInstance();
this.dVDPlayer = DVDPlayer.getInstanc();
}
// Operation Division 4 Step
public void ready() {
popcorn.on();
popcorn.pop();
screen.down();
projector.on();
stereo.on();
dVDPlayer.on();
theaterLight.dim();
}
public void play() {
dVDPlayer.play(); }
public void pause() {
dVDPlayer.pause(); }
public void end() {
popcorn.off();
theaterLight.bright();
screen.up();
projector.off();
stereo.off();
dVDPlayer.off();
}
}
test
public class Client {
public static void main(String[] args) {
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
homeTheaterFacade.ready();
homeTheaterFacade.play();
homeTheaterFacade.pause();
homeTheaterFacade.end();
}
}

The appearance pattern is in MyBatis Source code analysis of framework application
public class Configuration {
protected ReflectorFactory reflectorFactory;
protected ObjectFactory objectFactory;
protected ObjectWrapperFactory objectWrapperFactory;
this.reflectorFactory =new DefaultReflectorFactory();
this.objectFactory =new DefaultObjectFactory();
this.objectWrapperFactory =new DefaultObjectWrapperFactory();
public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler = (ParameterHandler) this.interceptorChain.pluginAll(parameterHandler);
return parameterHandler;
}
}
public static MetaObject forObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
return object == null ? SystemMetaObject.NULL_META_OBJECT : new MetaObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
}
private MetaObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
this.originalObject = object;
this.objectFactory = objectFactory;
this.objectWrapperFactory = objectWrapperFactory;
this.reflectorFactory = reflectorFactory;
if (object instanceof ObjectWrapper) {
this.objectWrapper = (ObjectWrapper)object;
} else if (objectWrapperFactory.hasWrapperFor(object)) {
this.objectWrapper = objectWrapperFactory.getWrapperFor(this, object);
} else if (object instanceof Map) {
this.objectWrapper = new MapWrapper(this, (Map)object);
} else if (object instanceof Collection) {
this.objectWrapper = new CollectionWrapper(this, (Collection)object);
} else {
this.objectWrapper = new BeanWrapper(this, object);
}
}

summary
- The appearance mode shields the details of the subsystem , Therefore, the appearance mode reduces the complexity of client's use of subsystem
- The appearance pattern decouples the coupling relationship between the client and the subsystem , Make the modules inside the subsystem easier to maintain and expand
- Through reasonable use of appearance mode , Can help us better divide the level of visit
- When the system needs to be layered , Consider using Facade Pattern
- When maintaining a large legacy system , Maybe the system has become very difficult to maintain and expand , At this point, consider developing a... For the new system Facade class , To provide a clear and simple interface for legacy systems , Let the new system and Facade Class interaction , Improve reusability
- Don't use the appearance mode too much or unreasonably , It's good to use the appearance mode , It's better to call the module directly . To make the system hierarchical , For maintenance purposes .
边栏推荐
- Airflow 2.2.3 containerized installation
- Is the interviewer too difficult to serve? A try catch asks so many tricks
- 剑指 Offer II 040. 矩阵中最大的矩形
- Nuxt - set SEO related tags, page titles, icons, etc. separately for each page (page configuration head)
- 1018 hammer scissors cloth
- ROS TF coordinate transformation Library & rewrite to create high frequency coordinate transformation broadcaster
- 选对学校,专科也能进华为~早知道就好了
- How to keep database and cache consistent
- Nuxt - 每个页面单独设置 SEO 相关标签及网页标题、图标等(页面配置 head)
- I came from a major, so I didn't want to outsource
猜你喜欢

Anaconda自带的Spyder编辑器启动报错问题

Why is the test post a giant pit? The 8-year-old tester told you not to be fooled

【新功能】Ambire 钱包集成了 Metis 网络

Build a simple website by yourself

女程序员晒出5月的工资条:工资是高,但是真累,网友评论炸锅了

Inftnews | metauniverse technology will bring a new shopping experience

为什么说测试岗位是巨坑?8年测试人告诉你千万别上当

Here comes Wi Fi 7. How strong is it?

yolox出现 RuntimeError: DataLoader worker (pid(s) 17724, 1364, 18928) exited unexpectedly

ROS URDF model is parsed into KDL tree
随机推荐
Remediation for Unsafe Cryptographic Encryption
Nuxt - set SEO related tags, page titles, icons, etc. separately for each page (page configuration head)
Canoe- how to parse messages and display information in the trace window (use of program node and structure type system variables)
iNFTnews | 元宇宙技术将带来全新的购物体验
Inftnews | metauniverse technology will bring a new shopping experience
Webassembly learning - dynamic linking
[C language] explain the thread recycling function pthread_ join
Project development culture
Live broadcast appointment AWS data everywhere series activities
JDBC learning
Ling Jing thinks about her own way
1018 hammer scissors cloth
Hcie security day41: theoretical learning: information collection and network detection
直播预约|AWS Data Everywhere 系列活动
Has my future been considered in the cloud native development route?
Implementation of thread pool based on variable parameter template
源代码防泄露技术种类浅析
Mécanisme d'attention du canal de fixation
Is the sink usually the JDBC insert update delete?
Developer scheme · environmental monitoring equipment (Xiaoxiong school IOT development board) connected to graffiti IOT development platform