外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。
这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。
抽象接口
/**
* @Author: chuan.bai
* @Description 脸的抽象类 包含 耳朵 鼻子 眼睛等
* @Date: Created on 13:35 2021/3/26
* @Modified By:
*/
public interface Face {
//使用的抽象方法
void use();
}
具体每个抽象类具体的时间
/**
* @Author: chuan.bai
* @Description
* @Date: Created on 13:37 2021/3/26
* @Modified By:
*/
public class Eye implements Face{
@Override
public void use() {
System.out.println("Eye.use");
}
}
/**
* @Author: chuan.bai
* @Description
* @Date: Created on 13:37 2021/3/26
* @Modified By:
*/
public class Ear implements Face{
@Override
public void use() {
System.out.println("Ear.use");
}
}
/**
* @Author: chuan.bai
* @Description
* @Date: Created on 13:37 2021/3/26
* @Modified By:
*/
public class Nose implements Face{
@Override
public void use() {
System.out.println("Nose.use");
}
}
外观类
/**
* @Author: chuan.bai
* @Description 外观类
* @Date: Created on 13:40 2021/3/26
* @Modified By:
*/
public class FaceMaker {
private Face ear;
private Face eye;
private Face nose;
public FaceMaker() {
ear = new Ear();
eye = new Eye();
nose = new Nose();
}
public void useEar() {
ear = new Ear();
ear.use();
}
public void useEye() {
eye = new Ear();
eye.use();
}
public void useNose() {
nose = new Nose();
nose.use();
}
}
客户端
/**
* @Author: chuan.bai
* @Description
* @Date: Created on 13:43 2021/3/26
* @Modified By:
*/
public class Client {
public static void main(String[] args) {
FaceMaker faceMaker = new FaceMaker();
faceMaker.useEar();
faceMaker.useNose();
faceMaker.useEye();
}
}
输出
Ear.use
Nose.use
Ear.use
Process finished with exit code 0