外观模式
案例:投资基金案例
源代码:
package org.zangyu.Appearance;
public class Appearance {
public static void main(String[] args) {
// TODO Auto-generated method stub
Fund fund = new Fund();
fund.BuyFund();
fund.SellFund();
}
}
class Stock1
{
//买股票
public void sell()
{
System.out.println("股票1卖出");
}
//买股票
public void Buy()
{
System.out.println("股票1买入");
}
}
class Stock2 //股票2
{/*代码类似*/
//买股票
public void sell()
{
System.out.println("股票2卖出");
}
//买股票
public void Buy()
{
System.out.println("股票2买入");
}
}
class Stock3 //股票3
{/*代码类似*/
//买股票
public void sell()
{
System.out.println("股票3卖出");
}
//买股票
public void Buy()
{
System.out.println("股票3买入");
}
}
class NationalDebt1 //国债1
{/*代码类似*/
//买国债
public void sell()
{
System.out.println("国债1卖出");
}
//买国债
public void Buy()
{
System.out.println("国债1买入");
}
}
class NationalDebt2 //国债2
{/*代码类似*/
//买国债
public void sell()
{
System.out.println("国债2卖出");
}
//买国债
public void Buy()
{
System.out.println("国债2买入");
}
}
class Fund//基金类
{
Stock1 gu1;
Stock2 gu2;
Stock3 gu3;
NationalDebt1 nd1;
NationalDebt2 nd2;
public Fund()
{
gu1= new Stock1();
gu2= new Stock2();
gu3= new Stock3();
nd1= new NationalDebt1();
nd2= new NationalDebt2();
}
public void BuyFund()
{
gu1.Buy();
gu2.Buy();
gu3.Buy();
nd1.Buy();
nd2.Buy();
}
public void SellFund()
{
gu1.sell();
gu2.sell();
gu3.sell();
nd1.sell();
nd2.sell();
}
}
用户不需要了解股票,甚至可以对股票一无所知,买了基金过了一段时间后在数回来就可以数钱了。参与股票的具体买卖都有基金公司完成。客户端代码也非常简洁明了。
外观模式——为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
外观模式结构框架:
源代码:
package org.zangyu.Appearance;
public class Appearance1 {
public static void main(String[] args) {
Facade facade=new Facade();
facade.MethodA();
facade.MethodB();
}
}
class Facade
{//外观类
SubSystemOne one;
SubSystemTwo two;
SubSystemThree three;
SubSystemFour four;
public Facade()
{
one=new SubSystemOne();
two=new SubSystemTwo();
three=new SubSystemThree();
four=new SubSystemFour();
}
public void MethodA()
{
System.out.println("方法组A()");
one.MethodeOne();
two.MethodeTwo();
four.MethodeFour();
}
public void MethodB()
{
System.out.println("方法组B()");
two.MethodeTwo();
three.MethodeThree();
}
}
class SubSystemOne
{
public void MethodeOne()
{
System.out.println("子系统方法一");
}
}
class SubSystemTwo
{
public void MethodeTwo()
{
System.out.println("子系统方法二");
}
}
class SubSystemThree
{
public void MethodeThree()
{
System.out.println("子系统方法三");
}
}
class SubSystemFour
{
public void MethodeFour()
{
System.out.println("子系统方法四");
}
}
在外观模式中,通常只需要一个外观类,并且此外观类只有一个实例,换言之他只是一个单例类。这并不意味着整个系统里只有一个外观类,而仅仅是说对每一个子系统只有一个外观类。
五.总结
1.外观模式的优点
(1)对客户屏蔽子系统组件,减少了客户处理的对象数目并使得子系统使用起来更加容易。通过引入外观模式,客户代码将变得很简单,与之关联的对象也很少。
(2)实现了子系统与客户之间的松耦合关系,这使得子系统的组件变化不会影响到调用它的客户类,只需要调整外观类即可。
(3)降低了大型软件系统中的编译依赖性,并简化了系统在不同平台之间的移植过程,因为编译一个子系统一般不需要编译所有其他的子系统。一个子系统的修改对其他子系统没有任何影响,而且子系统内部变化也不会影响到外观对象。
(4)只是提供了一个访问子系统的统一入口,并不影响用户直接使用子系统类。
2.外观模式的缺点
(1)不能很好地限制客户使用子系统类,如果对客户访问子系统类做太多的限制则减少了可变性和灵活性。
(2)在不引入抽象外观类的情况下,增加新的子系统可能需要修改外观类或客户端的源代码,违背了“开闭原则”。
3.外观模式适用环境
(1)当要为一个复杂子系统提供一个简单接口时可以使用外观模式。该接口可以满足大多数用户的需求,而且用户也可以越过外观类直接访问子系统。
(2)客户程序与多个子系统之间存在很大的依赖性。引入外观类将子系统与客户以及其他子系统解耦,可以提高子系统的独立性和可移植性。
(3)在层次化结构中,可以使用外观模式定义系统中每一层的入口,层与层之间不直接产生联系,而通过外观类建立联系,降低层之间的耦合度。