设计模式8-外观模式

本文通过家庭影院系统的具体实现,深入解析了外观模式的设计原理及其应用优势。通过将复杂系统的多个子系统封装到一个统一的接口中,外观模式简化了系统的使用,实现了功能解耦,提升了系统的可维护性和易用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.场景问题解决

1.1 场景描述

1.2 OO设计

1.3 需求变动

1.4 带来问题

2.用设计模式改进

2.1 分析

2.2 重新设计

  • Popcorn,Screen,Projector,DVDPlayer
//爆米花
public class Popcorn {

	private static Popcorn instance = null;

	private Popcorn() {
	}

	public static Popcorn getInstance() {
		if (instance == null) {
			instance = new Popcorn();
		}
		return instance;
	}
	
	public void on() {
		System.out.println("Popcorn On");
	}

	public void off() {
		System.out.println("Popcorn Off");
	}

	public void pop() {
		System.out.println("Popcorn is popping");
	}


	public static class Stereo {

        private static Stereo instance = null;
        private int volume = 5;

        private Stereo() {
        }

        public static Stereo getInstance() {
            if (instance == null) {
                instance = new Stereo();
            }
            return instance;
        }

        public void on() {
            System.out.println("Stereo On");
        }

        public void off() {
            System.out.println("Stereo Off");
        }

        public void setVolume(int vol) {
            volume = vol;
            System.out.println("the volume of Stereo is set to  " + volume);
        }

        public void addVolume() {
            if (volume < 11) {
                volume++;
                setVolume(volume);
            }
        }

        public void subVolume() {
            if (volume > 0) {
                volume--;
                setVolume(volume);
            }
        }
    }

	public static class TheaterLights {

        private static TheaterLights instance = null;

        private TheaterLights() {
        }

        public static TheaterLights getInstance() {
            if (instance == null) {
                instance = new TheaterLights();
            }
            return instance;
        }

        public void on() {
            System.out.println("TheaterLights On");
        }

        public void off() {
            System.out.println("TheaterLights Off");
        }

        public void dim(int d) {
            System.out.println("TheaterLights dim to " + d + "%");
        }

        public void bright() {
            dim(100);
            System.out.println("TheaterLights bright");
        }
    }
}




//投影屏幕
public class Screen {

	private static Screen instance = null;

	private Screen() {
	}

	public static Screen getInstance() {
		if (instance == null) {
			instance = new Screen();
		}
		return instance;
	}
	
	public void up() {
		System.out.println("Screen  up");
	}

	public void down() {
		System.out.println("Screen  down");
	}
}




//投影仪
public class Projector {
	private int size=5;

	private static Projector instance = null;

	private Projector() {
	}

	public static Projector getInstance() {
		if (instance == null) {
			instance = new Projector();
		}
		return instance;
	}
	
	public void on() {
		System.out.println("Projector On");
	}

	public void off() {
		System.out.println("Projector Off");
	}

	public void focus() {
		System.out.println("Popcorn is focus");
	}
	
	public void zoom(int size) {
		this.size=size;
		System.out.println("Popcorn zoom to"+size);
	}
}



public class DVDPlayer {
	private static DVDPlayer instance = null;
	private DVDPlayer() {
	}

	public static DVDPlayer getInstance() {
		if (instance == null) {
			instance = new DVDPlayer();
		}
		return instance;
	}
	
	public void on() {
		System.out.println("DVDPlayer On");
	}

	public void off() {
		System.out.println("DVDPlayer Off");
	}

	public void play() {
		System.out.println("DVDPlayer is playing");
	}
	
	public void pause() {
		System.out.println("DVDPlayer  pause");
	}
	
	public void setdvd() {
		System.out.println("DVDPlayer  is setting dvd");
	}
}


  • HomeTheaterFacade
public class HomeTheaterFacade {
	private Popcorn.TheaterLights mTheaterLights;
	private Popcorn mPopcorn;
	private Popcorn.Stereo mStereo;
	private Projector mProjector;
	private Screen mScreen;
	private DVDPlayer mDVDPlayer;

	public HomeTheaterFacade() {
		mTheaterLights = Popcorn.TheaterLights.getInstance();
		mPopcorn = Popcorn.getInstance();
		mStereo = Popcorn.Stereo.getInstance();
		mProjector = Projector.getInstance();
		mScreen = Screen.getInstance();
		mDVDPlayer = DVDPlayer.getInstance();
	}

	public void ready() {
		mPopcorn.on();
		mPopcorn.pop();
		mScreen.down();
		mProjector.on();
		mStereo.on();
		mDVDPlayer.on();
		mDVDPlayer.setdvd();
		mTheaterLights.dim(10);
	}

	public void end() {
		mPopcorn.off();
		mTheaterLights.bright();
		mScreen.up();
		mProjector.off();
		mStereo.off();
		mDVDPlayer.setdvd();
		mDVDPlayer.off();
	}

	public void play() {
		mDVDPlayer.play();
	}

	public void pause() {
		mDVDPlayer.pause();
	}
}

  • FacadeTest 测试类
public class FacadeTest {
	public static void main(String[] args) {
		HomeTheaterFacade mHomeTheaterFacade=new HomeTheaterFacade();
		
		mHomeTheaterFacade.ready();
		mHomeTheaterFacade.play();
	}
}

3.设计模式总结

3.1 定义

外观模式:提供一个统一的接口,来访问子系统中一群功能相关接口
外观模式定义了一个高层接口,让子系统更容易使用

3.2 分析思路

最少知识原则 :尽量减少对象之间的交互,只留几个“密友”
项目设计中就是不要让太多的类耦合在一起

  • 如何遵循最少知识原则:
    • 对象的方法调用范围:
    • 该对象本身
    • 作为参数传进来的对象
    • 此方法创建和实例化的对象
    • 对象的组件

4. 设计模式使用场景及注意

优点:功能汇聚,功能解耦(系统对外的解耦).
可以用命令模式的宏命令来替代.
命令模式的侧重点是封装,外观模式主要为对外的解耦

适配器模式:

5.参考文章

内容总计于HeadFirst设计模式及相关视频

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值