当前位置:网站首页>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 .
边栏推荐
- Go Foundation (I)
- Airflow2.2.3 + efficiency + MySQL 8 build a robust distributed scheduling cluster
- From zero to one, I will teach you to build a "search by text and map" search service (I)
- 1019 数字黑洞
- Does cdc2.2.1 not support postgresql14.1? Based on the pgbouncer connection mode, with 5433
- [WC2021] 斐波那契——数论、斐波那契数列
- [C language] start a thread
- String不同创建方式的区别
- C language -- branch structure
- Webapck system foundation
猜你喜欢

Mécanisme d'attention du canal de fixation

Implementation of b+ tree index based on xlsx

Daily practice - February 15, 2022

New listing of operation light 3.0 -- a sincere work of self subversion across the times

Blue Bridge Cup ruler method

JVM_ 16_ Garbage collector

【HackTheBox】dancing(SMB)

Implementation of thread pool based on variable parameter template

SQL two columns become multi row filter display

Inftnews | metauniverse technology will bring a new shopping experience
随机推荐
IDEA修改jvm内存
The virtual machine MySQL cannot be connected to the local computer
【Laravel系列8】走出 Laravel 的世界
ROS URDF model is parsed into KDL tree
Webassembly learning - dynamic linking
【新功能】Ambire 钱包集成了 Metis 网络
Path and LD_ LIBRARY_ Example of path usage
Is the increased life insurance off the shelf? What additional life insurance products are available now?
剑指 Offer II 040. 矩阵中最大的矩形
【HackTheBox】dancing(SMB)
Seattention channel attention mechanism
赚钱的5个层次,你在哪一层?
The great gods take connections from the MySQL connection pool in the open of the rich function. The initialization of the connection pool is 20. If the parallelism of the rich function is 1
Build a simple website by yourself
不使用union实现Mysql 列转行
Lua protobuff Emmy Lua wheel
[C language] explain the thread exit function pthread_ exit
1018 锤子剪刀布
How to create robots Txt file?
The 30th day of force deduction (DP topic)