[一天一个设计模式]-[外观模式]

外观模式,顾名思义就是要把对外的接口整的好看点。是一种通过为多个复杂的子系统提供一个一致的接口,而使这些子系统更加容易被访问的模式。该模式对外有一个统一接口,外部应用程序不用关心内部子系统的具体的细节,这样会大大降低应用程序的复杂度,提高了程序的可维护性。

与适配器模式区别,适配器模式要把对象转成特定对象

与装饰器模式区别,装饰器模式装饰完还是原来的类型。

举个列子,去买奶茶,奶茶有各种各样的,你只需要跟店员说就行。

class BubbleTea{
    
    void name(){
        sout("I.m BulleTea.");
    }
} 
class RedTea{
    
    void name(){
        sout("I.m RedTea.");
    }
} 
class BlackTea{
    
    void name(){
        sout("I.m BlackTea.");
    }
}
class Assistant {
    BubbleTea bubbleTea;
    RedTea redTea;
    BlackTea blackTea;
    
    public Assistant(){
        bubbleTea = new BubbleTea();
        redTea = new RedTea();
        blackTea = new BlackTea();
    }
    
    void bubbleTea(){
        bubbleTea.name();
    }
    
    void redTea(){
        redTea.name();
    }
    void blackTea(){
        blackTea.name();
    }
}

main(){
    Assistant a=new Assistant();
    a.bubbleTea();
    a.redTea();
    a.blackTea();
    
}
posted @ 2019-12-12 08:56  A_yes  阅读(85)  评论(0编辑  收藏  举报