当前位置:网站首页>Software engineering - high cohesion and low coupling

Software engineering - high cohesion and low coupling

2022-06-26 08:04:00 Live Firestone

Interface design principles

Good interface should meet the six principles of design pattern , A lot of design patterns , The framework is based on the starting point of high cohesion and low coupling .

  • Principle of single responsibility : A class is only responsible for the corresponding responsibilities in a functional area .
  • Opening and closing principle : A software entity should be open to extension , Turn off for changes .
  • Richter substitution principle : All reference base classes ( Parent class ) Must be able to transparently use objects of its subclasses .
  • Dependence Inversion Principle : Abstractions should not depend on details , Details should depend on abstraction . In other words , To program for an interface , Instead of programming for implementation .
  • Interface isolation principle : Using multiple specialized interfaces , Instead of using a single master interface , That is, the client should not rely on interfaces it does not need
  • Dimitar's law : A software entity should interact with as few other entities as possible , For example, appearance mode , Unified interface for external exposure

High cohesion and low coupling

  • cohesion : Each module can complete its own functions as independently as possible , It doesn't depend on the code outside the module
  • coupling : The complexity of the interface between modules , The more complex the connection between modules, the higher the coupling , Pull one hair and move the whole body .
  • Purpose : Make the module " Reusability "," portability " Greatly increase .
  • High cohesion : A software module is composed of highly relevant code , Only one task , That is to say, the principle of single responsibility . The cohesion of the module reflects the tightness of the module . A module only needs to do one thing well , Don't be overly concerned about other tasks . The advantage of high cohesion is that it can improve the reliability of the program
  • Low coupling : Between modules , System to system interaction , Is inevitable , However, we should try to reduce the situation that a single module cannot be used independently or transplanted due to interaction , Provide as many separate interfaces as possible for external operation , This is what we call " Low coupling ".

Generally, the more cohesive the modules in the program structure are , The lower the coupling between modules .

Coupling and cohesion are two qualitative criteria of module independence , When dividing the software system into modules , Strive for high cohesion and low coupling , Improve the independence of modules , Lay the foundation for designing high quality software structure

The type of coupling ( Coupling strength from low to high )

  1. No direct coupling : There is no direct relationship between the two modules , The connection between them is completely realized through the control and call of the main module . The coupling is the weakest , Module independence is the strongest . Sub modules do not need to know the existence of each other , Connections between sub modules , All become the connection between the sub module and the main module

  2. Data coupling : When one module accesses another , Between each other is through simple data parameters ( It's not a control parameter 、 Common data structures or external variables ) To exchange input 、 transport Informative

  3. Tag coupling : It refers to the data structure passed between two modules , For example, array names in high-level languages 、 Record name 、 File names and so on are marked , In fact, it is the address of the data structure .

  4. Control coupling : When one module calls another , What is passed is the control variable ( Like a switch , Signs, etc ), The tuned module selectively performs a function within the block through the value of the control variable

  5. External coupling : A set of modules all access the same global simple variable , And the information of the global variable is not passed through parameters , It's called external coupling

  6. Public coupling : Refers to the coupling between modules that interact through a common data environment . Complex programs with common coupling increase with the number of coupling modules .

    a. Unable to control the access of each module to public data , It seriously affects the reliability and adaptability of software modules .

    b. Make the maintainability of software worse . If a module modifies public data , It will affect the relevant modules

    c. Reduces the intelligibility of the software . It's not easy to know which data is used by which Shared by modules , It's difficult to make mistakes

  7. Content coupling : This is the highest level of coupling , And the worst coupling .

    a. One module directly accesses the internal data of another module ;

    b. One module is not transferred to the inside of another module through the normal entrance ;

    c. Part of the program code of the two modules overlaps

    d. A module has multiple entrances .

    Content coupling can occur in assembly languages . Most high-level languages have been designed not to allow content coupling . This kind of coupling has the strongest coupling , Module independence is the weakest .

Cohesion classification ( low –> high )

  1. To gather by chance : It means that there is no relationship between the processing elements in a module .
  2. Logical cohesion : It refers to the execution of several logically similar functions in the module , Through the parameters to determine which function the module completes .
  3. Time converges : The module formed by combining the actions that need to be executed at the same time is called time cohesion module .
  4. Communication cohesion : All processing elements in the module operate on the same data structure ( It is also called information cohesion ), Or all elements in the module use the same input data or produce the same output data .
  5. Order cohesion : It refers to that each processing element in a module is closely related to the same function and must be executed in sequence , The output of the former function element is the input of the next function element
  6. Functional cohesion : This is the strongest cohesion , It means that all elements in the module complete a function together , Be short of one cannot . Coupling with other modules is the weakest .

It is generally believed , To gather by chance 、 Logical cohesion and temporal cohesion belong to low cohesion , Communication cohesion belongs to medium cohesion , Sequential cohesion and functional cohesion belong to high cohesion . Try to be as cohesive as possible when designing software , And can identify low cohesion modules , Thus, the cohesion of the module can be improved by modifying the design , Reduce coupling between modules , Improve the independence of modules , Lay the foundation for designing high quality software structure .

原网站

版权声明
本文为[Live Firestone]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170606258273.html