• 设计模式:外观模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)


    上一篇《单例模式》                                                                         下一篇《模板模式》

    简介:

    外观模式,它是一种设计模式,它为子系统中的一组接口提供一个统一的、简单的接口。这种模式主张按照描述和判断资料来评价课程,关键活动是在课程实施的全过程中进行观察和搜集意见,以了解人们对课程的不同看法。

    外观模式的使用场景:
    1、当你需要为一个复杂的子系统提供一个简单的接口时。它允许你简化一些复杂的操作或过程,使得在使用这个子系统时无需了解其内部细节。
    2、当你的系统需要与其他系统进行交互时。通过为外部系统提供一个统一的接口,可以使得内部子系统与外部系统的交互更加简单和直接。
    3、当你需要对一个大型遗留系统进行维护和扩展时。这种情况下,为新系统创建一个外观类可能更为实用,它可以封装原有系统的复杂性,并为新系统提供简单的接口。
    这种模式在设计和开发过程中可以发挥很大的作用,帮助开发者更好地组织和管理代码,提高系统的可维护性和可扩展性。

    外观模式的创建步骤:
    1、定义一个外观类(Facade),这个类将为子系统中的每个类定义一个方法。
    2、在外观类中,定义一些方法,这些方法将被客户端调用。这些方法将被子系统中的类实现。
    3、在客户端代码中,使用外观类的方法来调用子系统中的方法。

    外观模式的优点,主要包括:
    1、简化子系统使用:为子系统提供了一个简单的接口,使得客户端可以更容易地使用子系统而无需了解其内部细节。
    2、增强了子系统的松耦合:通过外观类,将客户端与子系统之间的关系解耦,客户端只需要和外观类进行交互,无需直接和子系统中的类打交道。
    3、增加了安全性:通过将子系统的内部实现细节隐藏在外观类之后,可以防止客户端直接访问和修改子系统的内部状态。
    4、隐藏子系统实现:外观模式隐藏了子系统的实现细节,只向客户端暴露必要的接口,从而保护了子系统的完整性。
    5、提高了代码的可读性和可维护性:通过使用外观模式,代码变得更加结构化,易于阅读和维护。
    6、减少了客户端和子系统之间的代码复杂度:通过将客户端和子系统之间的复杂交互抽象为一个或几个简单的方法调用,减少了客户端和子系统之间的代码复杂度。
    7、符合迪米特法则:外观模式符合迪米特法则(最少知道原则),使得客户端需要处理的类数量减少,降低了系统的耦合性。

    外观模式的缺点,主要包括:
    1、子系统扩展风险:当子系统需要扩展时,可能会对原有系统的功能造成影响。
    2、不符合开闭原则:在需要修改子系统时,可能也需要修改外观类,这不符合开闭原则。
    3、降低了子系统的可维护性:外观模式使得子系统的内部结构和实现细节更加难以被了解和维护。
    4、增加了代码的复杂性:外观模式的实现需要增加额外的类和方法,这会增加代码的复杂性和维护难度。
    5、降低了代码的可读性:如果外观类的设计和实现不够清晰,可能会使得代码的可读性更加困难。
    6、增加了额外的开销:外观模式的实现和运行需要额外的开销,例如需要额外的方法调用和处理等。

    示例:

    一、C#外观模式

    以下是一个示例,展示了如何在C#中实现外观模式:

    1. public interface ISubsystem1  
    2. {  
    3.     void Operation1();  
    4. }  
    5.   
    6. public interface ISubsystem2  
    7. {  
    8.     void Operation2();  
    9. }  
    10.   
    11. public interface ISubsystem3  
    12. {  
    13.     void Operation3();  
    14. }  
    15.   
    16. public class Subsystem1 : ISubsystem1  
    17. {  
    18.     public void Operation1()  
    19.     {  
    20.         Console.WriteLine("Subsystem1.Operation1");  
    21.     }  
    22. }  
    23.   
    24. public class Subsystem2 : ISubsystem2  
    25. {  
    26.     public void Operation2()  
    27.     {  
    28.         Console.WriteLine("Subsystem2.Operation2");  
    29.     }  
    30. }  
    31.   
    32. public class Subsystem3 : ISubsystem3  
    33. {  
    34.     public void Operation3()  
    35.     {  
    36.         Console.WriteLine("Subsystem3.Operation3");  
    37.     }  
    38. }  
    39.   
    40. public class Facade  
    41. {  
    42.     private ISubsystem1 _subsystem1;  
    43.     private ISubsystem2 _subsystem2;  
    44.     private ISubsystem3 _subsystem3;  
    45.   
    46.     public Facade()  
    47.     {  
    48.         _subsystem1 = new Subsystem1();  
    49.         _subsystem2 = new Subsystem2();  
    50.         _subsystem3 = new Subsystem3();  
    51.     }  
    52.   
    53.     public void SimpleOperation()  
    54.     {  
    55.         _subsystem1.Operation1();  
    56.         _subsystem2.Operation2();  
    57.         _subsystem3.Operation3();  
    58.     }  
    59. }
    60.  
    61. public class Client {  
    62.     public void test() {  
    63.         Facade facade = new Facade();  
    64.         facade.SimpleOperation();  
    65.     }  
    66. }

    二、java外观模式

    外观模式通常通过以下方式实现:

    1. // 子系统中的组件类  
    2. class SubsystemComponent1 {  
    3.     public void operation1() {  
    4.         System.out.println("SubsystemComponent1.operation1");  
    5.     }  
    6. }  
    7.   
    8. class SubsystemComponent2 {  
    9.     public void operation2() {  
    10.         System.out.println("SubsystemComponent2.operation2");  
    11.     }  
    12. }  
    13.   
    14. class SubsystemComponent3 {  
    15.     public void operation3() {  
    16.         System.out.println("SubsystemComponent3.operation3");  
    17.     }  
    18. }  
    19.   
    20. // 外观类  
    21. class Facade {  
    22.     private SubsystemComponent1 component1;  
    23.     private SubsystemComponent2 component2;  
    24.     private SubsystemComponent3 component3;  
    25.   
    26.     public Facade() {  
    27.         component1 = new SubsystemComponent1();  
    28.         component2 = new SubsystemComponent2();  
    29.         component3 = new SubsystemComponent3();  
    30.     }  
    31.   
    32.     public void simplifiedOperation() {  
    33.         component1.operation1();  
    34.         component2.operation2();  
    35.         component3.operation3();  
    36.     }  
    37. }  
    38.   
    39. // 客户端代码  
    40. public class Client {  
    41.     public static void main(String[] args) {  
    42.         Facade facade = new Facade();  
    43.         facade.simplifiedOperation();  
    44.     }  
    45. }

    三、javascript外观模式

    在JavaScript实现外观模式的示例:

    1. // 子系统中的组件  
    2. const Component1 = {  
    3.   operation1: function() {  
    4.     console.log('Component1.operation1');  
    5.   }  
    6. };  
    7.   
    8. const Component2 = {  
    9.   operation2: function() {  
    10.     console.log('Component2.operation2');  
    11.   }  
    12. };  
    13.   
    14. const Component3 = {  
    15.   operation3: function() {  
    16.     console.log('Component3.operation3');  
    17.   }  
    18. };  
    19.   
    20. // 外观类  
    21. const Facade = {  
    22.   constructor() {  
    23.     this.component1 = new Component1;  
    24.     this.component2 = new Component2;  
    25.     this.component3 = new Component3;  
    26.   },  
    27.   
    28.   simplifiedOperation: function() {  
    29.     this.component1.operation1();  
    30.     this.component2.operation2();  
    31.     this.component3.operation3();  
    32.   }  
    33. };  
    34.   
    35. // 客户端代码  
    36. const facade = new Facade;  
    37. facade.simplifiedOperation(); // 输出:Component1.operation1 Component2.operation2 Component3.operation3

    四、C++外观模式

    以下是在C++中实现外观模式:

    1. #include  
    2.   
    3. // 子系统中的组件类  
    4. class Component1 {  
    5. public:  
    6.     void operation1() {  
    7.         std::cout << "Component1.operation1" << std::endl;  
    8.     }  
    9. };  
    10.   
    11. class Component2 {  
    12. public:  
    13.     void operation2() {  
    14.         std::cout << "Component2.operation2" << std::endl;  
    15.     }  
    16. };  
    17.   
    18. class Component3 {  
    19. public:  
    20.     void operation3() {  
    21.         std::cout << "Component3.operation3" << std::endl;  
    22.     }  
    23. };  
    24.   
    25. // 外观类  
    26. class Facade {  
    27. public:  
    28.     Facade() {  
    29.         component1 = new Component1;  
    30.         component2 = new Component2;  
    31.         component3 = new Component3;  
    32.     }  
    33.   
    34.     ~Facade() {  
    35.         delete component1;  
    36.         delete component2;  
    37.         delete component3;  
    38.     }  
    39.   
    40.     void simplifiedOperation() {  
    41.         component1->operation1();  
    42.         component2->operation2();  
    43.         component3->operation3();  
    44.     }  
    45.   
    46. private:  
    47.     Component1* component1;  
    48.     Component2* component2;  
    49.     Component3* component3;  
    50. };  
    51.   
    52. // 客户端代码  
    53. int main() {  
    54.     Facade facade;  
    55.     facade.simplifiedOperation(); // 输出:Component1.operation1 Component2.operation2 Component3.operation3  
    56.     return 0;  
    57. }

    五、python外观模式

    以下是在python中实现外观模式:

    1. # 子系统中的组件  
    2. class Component1:  
    3.     def operation1(self):  
    4.         print("Component1.operation1")  
    5.   
    6. class Component2:  
    7.     def operation2(self):  
    8.         print("Component2.operation2")  
    9.   
    10. class Component3:  
    11.     def operation3(self):  
    12.         print("Component3.operation3")  
    13.   
    14. # 外观类  
    15. class Facade:  
    16.     def __init__(self):  
    17.         self.component1 = Component1()  
    18.         self.component2 = Component2()  
    19.         self.component3 = Component3()  
    20.   
    21.     def simplified_operation(self):  
    22.         self.component1.operation1()  
    23.         self.component2.operation2()  
    24.         self.component3.operation3()  
    25.   
    26. # 客户端代码  
    27. if __name__ == '__main__':  
    28.     facade = Facade()  
    29.     facade.simplified_operation() # 输出:Component1.operation1 Component2.operation2 Component3.operation3

    六、go外观模式

    以下是一个示例,展示了如何在go中实现外观模式:

    1. // 子系统中的组件  
    2. type Component1 struct{}  
    3.   
    4. func (c *Component1) Operation1() {  
    5.     fmt.Println("Component1.Operation1")  
    6. }  
    7.   
    8. type Component2 struct{}  
    9.   
    10. func (c *Component2) Operation2() {  
    11.     fmt.Println("Component2.Operation2")  
    12. }  
    13.   
    14. type Component3 struct{}  
    15.   
    16. func (c *Component3) Operation3() {  
    17.     fmt.Println("Component3.Operation3")  
    18. }  
    19.   
    20. // 外观类  
    21. type Facade struct {  
    22.     component1 *Component1  
    23.     component2 *Component2  
    24.     component3 *Component3  
    25. }  
    26.   
    27. func (f *Facade) SimpleOperation() {  
    28.     f.component1.Operation1()  
    29.     f.component2.Operation2()  
    30.     f.component3.Operation3()  
    31. }  
    32.   
    33. // 客户端代码  
    34. func main() {  
    35.     facade := &Facade{  
    36.         component1: &Component1{},  
    37.         component2: &Component2{},  
    38.         component3: &Component3{},  
    39.     }  
    40.     facade.SimpleOperation() // 输出:Component1.Operation1 Component2.Operation2 Component3.Operation3  
    41. }

    七、PHP外观模式

    以下是一个示例,展示了如何在PHP中实现外观模式:

    1.  
    2.   
    3. // 子系统中的类  
    4. class Subsystem1 {  
    5.     public function operation1() {  
    6.         echo "Subsystem1.operation1\n";  
    7.     }  
    8. }  
    9.   
    10. class Subsystem2 {  
    11.     public function operation2() {  
    12.         echo "Subsystem2.operation2\n";  
    13.     }  
    14. }  
    15.   
    16. class Subsystem3 {  
    17.     public function operation3() {  
    18.         echo "Subsystem3.operation3\n";  
    19.     }  
    20. }  
    21.   
    22. // 外观类  
    23. class Facade {  
    24.     private $subsystem1;  
    25.     private $subsystem2;  
    26.     private $subsystem3;  
    27.   
    28.     public function __construct() {  
    29.         $this->subsystem1 = new Subsystem1();  
    30.         $this->subsystem2 = new Subsystem2();  
    31.         $this->subsystem3 = new Subsystem3();  
    32.     }  
    33.   
    34.     public function simplifiedOperation() {  
    35.         $this->subsystem1->operation1();  
    36.         $this->subsystem2->operation2();  
    37.         $this->subsystem3->operation3();  
    38.     }  
    39. }  
    40.   
    41. // 客户端代码  
    42. $facade = new Facade();  
    43. $facade->simplifiedOperation(); // 输出:Subsystem1.operation1 Subsystem2.operation2 Subsystem3.operation3  
    44.   
    45. ?>

    《完结》

    上一篇《单例模式》                                                                         下一篇《模板模式》

  • 相关阅读:
    Vuex中Mutation 的基本使用
    css如何讲一个框分为三份写内容?
    【单源最短路 图论】3112. 访问消失节点的最少时间
    界面组件Kendo UI for React R3 2022新版,让Web应用更酷炫
    【OAuth2】十五、客户端认证流程-自定义授权页面和客户端认证
    Java之线程的详细解析一
    镜频抑制滤波器对射频接收前端输出噪声的影响
    【Java刷题】初始化List应该选择ArrayList还是LinkedList
    WPF控件4
    8.2模拟赛总结
  • 原文地址:https://blog.csdn.net/yetyrain/article/details/133936421