See how Python implements the facade mode

introduction

  Let’s make a comparatively vivid analogy. Let’s compare the facade to the surface of a building on a construction site, which can be marked with banners, such as XXXX Railway Engineering Bureau. This more eye-catching side can attract more attention. When people pass by the building, they can see the external appearance, and they don’t understand the complexity of its own structure at this time.
  While hiding the internal complexity in the program, the facade also provides an easily accessible interface for external clients.

 

 

Structural design pattern

Facade mode is different from singleton mode and factory mode. It is a structural mode.

  • Structural patterns describe how to combine objects and classes into larger structures.

  • Structural mode is a mode that can simplify design work. It can find a simpler way to recognize or express the relationship between entities.

  • Structural mode is a synthesis of class and object mode. The class pattern describes abstraction through inheritance, thereby providing a more useful program interface, while the object pattern describes how to connect objects to form larger objects.

 

Understand facade design patterns

  • It provides a unified interface for a group of interfaces in the subsystem, and defines a high-level interface to help clients use the subsystem in a simpler way.

  • The problem solved by the facade is how to use a single interface object to represent a complex subsystem. In fact, it is not an encapsulation subsystem, but a combination of the underlying subsystems.

  • Promote the decoupling of subsystems and clients.

 

The following picture may help you better understand:

image

There are three participants: facade, client, system

 

Client

The facade
will be instantiated. A request will be made to the facade to let the subsystem complete the corresponding functions. The
client interacts with the facade, so that it can easily communicate with the subsystem and complete work items without worrying about the complexity of the system.

 

Facade

An interface that knows which subsystem a request should be processed
by, and delegates the client's request to the corresponding subsystem object in a combined manner

 

system

Realize the function of the subsystem, represented by a group of classes responsible for different tasks.
Deal with the assignment of facade objects, but don't know the facade and don't reference it

 

Simple understanding: The implementation of the entire model is to arrange the system to complete the work after the facade receives the client's needs.

Take a common example in life: one day, you went to KFC with a friend, and you ordered a XXX set meal from the waiter. In the set meal, there is a glass of ice cola, a burger, a piece of fries, and a spicy chicken wings. After the waiter heard your order request, he turned his head and told Houchu that he needed a XXX set meal, so there were 4 people in Houchu who started to act, one for coke, one for hamburger, one for french fries, and one for chicken wings.

In this example, you are the client, the waiter is the facade, and the 4 people in the back kitchen make the 4 subsystems, and they work together to complete the production of this package. In this way, the understanding of the facade model is very simple and clear.

 

Python implements facade mode

#门面
class ProjectProcess(object):
    def __init__(self):
        print("get the project of the process")

    def get_process(self):
        self.req=Requirement()
        self.req.getRequirementProcess()

        self.dev=Develop()
        self.dev.getDevelopProcess()

        self.test=Test()
        self.test.getTestProcess()

#子系统
class Requirement(object):
    def __init__(self):
        print("return the requirement of process")

    def getRequirementProcess(self):
        print("the requirement was complement 90%")


class Develop(object):
    def __init__(self):
        print("return the requirement of process")

    def getDevelopProcess(self):
        print("the develop was complement 30%")


class Test(object):
    def __init__(self):
        print("return the requirement of process")

    def getTestProcess(self):
        print("the test was complement 10%")

#客户端
if __name__ == '__main__':
    pp = ProjectProcess()
    pp.get_process()

 

The principle of least knowledge

   (1) The facade can decouple the client from the subsystem that implements specific functions, and the design principle behind it is the principle of minimum knowledge.
   (2) When designing the system, for each object created, the number of classes that interact with it should be examined, as well as the way of interaction to avoid creating many classes that are tightly coupled with each other. If there are a lot of dependencies between classes, the system becomes difficult to maintain.

Welcome to pay attention to [The Way of Infinite Testing] public account , reply [receive resources],
Python programming learning resources dry goods,
Python+Appium framework APP UI automation,
Python+Selenium framework Web UI automation,
Python+Unittest framework API automation,

Resources and codes are sent for free~
There is a QR code of the official account at the bottom of the article, you can just scan it on WeChat and follow it.

Remarks: My personal public account has been officially opened, dedicated to the sharing of test technology, including: big data testing, functional testing, test development, API interface automation, test operation and maintenance, UI automation testing, etc., WeChat search public account: "Infinite The Way of Testing", or scan the QR code below:

 Add attention and let us grow together!

Guess you like

Origin blog.csdn.net/weixin_41754309/article/details/113093627