外观模式实例
问题描述
某软件公司为新开发的智能手机控制与管理软件提供一键备份功能,通过该功能可以将原本存储在手机中的通讯录、短信、照片、音乐等资料一次性拷贝到移动存储介质中(例如:SD卡)中。在实现过程中需要与多个已有的类进行交互,如通讯录管理类、短信管理类。
结构图
编程实现
需要交互的类
public class ContactsManager {
private String contacts;
public ContactsManager(String contacts) {
this.contacts = contacts;
}
public void copy(){
System.out.println(contacts);
}
}
public class MessageManager {
private String message;
public MessageManager(String message) {
this.message = message;
}
public void copy(){
System.out.println(message);
}
}
public class PhotoManager {
private String photo;
public PhotoManager(String photo) {
this.photo = photo;
}
public void copy(){
System.out.println(photo);
}
}
Facade类
public class Facade {
private ContactsManager contacts;
private MessageManager message;
private PhotoManager photo;
public Facade() {
this.contacts = new ContactsManager("通讯录");
this.message = new MessageManager("短信");
this.photo=new PhotoManager("照片");
}
public void backup(){
contacts.copy();
message.copy();
photo.copy();
}
}
客户端
public class Client {
public static void main(String[] args) {
Facade facade=new Facade();
facade.backup();
}
}