最后就是使用:
public class TestUse { public static void main(String args[]){ WeatherFactory factory = new WeatherFactory(); IWeather weather1,weather2,weather3,weather4,weather5,weather6,weather7,weather8; weather1 = factory.getFlyWeight("多云",15); weather2 = factory.getFlyWeight("晴",23); weather3 = factory.getFlyWeight("多云",16); weather4 = factory.getFlyWeight("阴",10); weather5 = factory.getFlyWeight("多云",15); weather6 = factory.getFlyWeight("多云",15); weather7 = factory.getFlyWeight("多云",15); weather8 = factory.getFlyWeight("多云",15); weather1.printWeather(); weather2.printWeather(); weather3.printWeather(); weather4.printWeather(); weather5.printWeather(); weather6.printWeather(); weather7.printWeather(); weather8.printWeather(); System.out.println("实际对象个数" factory.getFlyweightSize()); }}
输出:
天气:多云 温度:15天气:晴 温度:23天气:多云 温度:16天气:阴 温度:10天气:多云 温度:15天气:多云 温度:15天气:多云 温度:15天气:多云 温度:15实际对象个数4
可以看到,确实是我们预期的那样,相同的对象并没有重复创建。
享元模式的优点在于它大幅度地降低内存中对象的数量。但是,它做到这一点所付出的代价也是很高的:享元模式使得系统更加复杂。为了使对象可以共享,需要将一些状态外部化,这使得程序的逻辑复杂化。享元模式将享元对象的状态外部化,而读取外部状态使得运行时间变长。
21、外观模式
为系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
何时使用
需要的时候。需要的时候太多了
优点
看了下面你的介绍,就知道外观模式可以让一个系统更加容易使用
外观模式是一种很简单的模式,我们有意无意都在使用这种模式。
我们提供给用户的某个功能,可能是包含很多个步骤的,但我们可以把这些步骤封装到一个统一的接口中,让用户感觉仅仅就是一个单一的操作,使用起来也就更加简单。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-33893-42.html
烊烊加油