Notes on High Cohesion and low coupling

Source: Internet
Author: User

In general, the design principles of high cohesion and low coupling should be realized in actual software development. C language is different from C ++. C language is process-oriented and C ++ object-oriented.

In a real project, the original business functions need to be retained to upgrade the business. To ensure that the old functions continue to be maintained, the old functions cannot be deleted directly.

The C language is process-oriented and usually uses the callback method. C ++ Is Object-Oriented. To achieve high cohesion and low coupling, the interface technology is required.

C language:

The software usually has the logging function of background logs, which is implemented by the log function. The main business is represented by the business function:

void log(){printf("Logging...\n");}
void business(){while(1){sleep(1);printf("Deal Business...\n");log();}}
int main(){business();return 0;}

How can I upgrade the background log function?

The general idea is: Write another function log2 and change the log in business to log2?

But how can you easily change the main business code? I need to change the main business code because of a small function. Isn't it a matter of IQ?

In another way, Use callback:

#include <stdio.h>#include <unistd.h>void log1(){printf("1 Logging...\n");}void log2(){printf("2 Logging...\n");}void business( void (*f)() ){while(1){sleep(1);printf("Deal Business...\n");f();}}int main(){business(log1);return 0;}

The business function accepts a function pointer. The Pointer Points to a function without any parameters. The return value is void, which conforms to the log function prototype. In business, you only need F () to call the corresponding function.

When you need to use log1, pass log1 to business, and use log2 after upgrade.


C ++ example:

C ++ emphasizes the idea of object-oriented.

#include <iostream>using namespace std;class Log{public:void log(){cout << "logging..." << endl;}};class Business{private:        Log *l;public:        Business(Log *l = NULL)        {}void business(){while(1){sleep(1);cout << "Deal Business..." << endl;l->log();}}};int main(){       Business b(new Log);       b.business();       return 0;}

Now, how do we upgrade the background log function? Some people think of the overload in C ++, and reload a function log2 in the log class;

Some people think of inheriting the log class and overwriting the log function, but these methods all need to change the code in the business class. How can this problem be solved? Therefore, the interface technology in C ++ is useful.

Remember, interfaces emphasize methods. Methods in interfaces are defined as pure virtual functions. Interfaces cannot be instantiated or instantiated, classes that require functions in the interface only need to inherit the interface! The following is an example:

# Include <iostream> using namespace STD; Class log {public: Virtual void log () = 0; // pure virtual function}; Class log1: public log // inherited interface {public: void log () {cout <"1 logging... "<Endl ;}}; class log2: Public log // inherited interface {public: void log () {cout <" 2 logging... "<Endl ;}}; class business {public: void business (log * f) // if the function parameter is set to a log pointer, it indicates the instance of log1 or log2, implemented by polymorphism {While (1) {sleep (1); cout <"deal business... "<Endl; F-> log ();}} }; Int main () {business B; B. Business (New log2); // calls the log function in log2! Return 0 ;}

At this time, the log service upgrade will not affect the Business Code. You only need to import different log instantiation into the business.

This is a great design idea.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.