Design Patterns and America - quickly improve code quality

unit test

Write unit tests, sometimes a method depends on many other services can be achieved through mock framework or manually achieve such services depend on micro UserSerevice we can achieve a local implementation according to unit test

Unit testing to ensure that future demand or modify the increase in demand would affect other logic to determine when we only need to increase the original logic unit tests run again

Decoupling

How decoupling

Abstract package

Encapsulation and abstraction as two very versatile design, the design can be applied in many scenarios, such as systems, modules, lib, components, interfaces, classes, etc. design. Encapsulation and abstraction can effectively hide the complexity of the implementation, isolation variability achieved, dependent module to provide a stable and easy to use abstract interface. For example, open () function Unix operating system provides a file, we use very simple, but the underlying implementation is very complex, involving access control, concurrency control, physical storage, and so on. We encapsulates into an abstract open () function, can effectively control the spread code complexity, the complexity of the package in the local code. In addition, because the open () function defines an abstract rather than based on the specific implementation, we change the open () function when the underlying implementation, it does not rely on the need to change the code in the upper layer, also in line with our earlier mentioned "high cohesion, loose coupling" criteria code.

middle layer

 

 Introducing the intermediate layer can be simplified dependencies between modules or classes. Below this is a comparison chart of FIG dependency is introduced before and after the intermediate layer. Prior to introduction into the intermediate data storage layer, A, B, C three modules must rely on a cache memory, Redis secondary cache, DB three persistent storage module. After the introduction of the intermediate layer, three modules only require data from one module to store. As can be seen from the figure, the introduction of the intermediate layer is substantially simplified dependency, so that more clear code structure

Another advantage is that we have such a cache to be replaced with a new interface that places all dependent we all need to change, if there is an intermediate layer we just need to change the old middle layer interface replaced with the new interface

Modular

 

 Micro Modular Services: Goods Inventory Service payment service orders and service

lib library modular framework such as the division of our packages such as dubbo spring mvc

Guess you like

Origin www.cnblogs.com/LQBlog/p/12303804.html