High aggregation and low coupling

Source: Internet
Author: User

This is a concept in software engineering.

First, you must know that a software is composed of multiple subprograms,

A program consists of multiple modules (methods!

Cohesion refers to the closeness between various modules in the program,

Coupling is the closeness between various external programs (subprograms.

So it is easy to understand, why is it high cohesion? The closer the relationship between modules, the fewer errors! Low coupling? The more complex the relationship between subprograms, more unexpected errors will occur! It will cause a lot of trouble for future maintenance work!

 

 

C # A major feature of the language is
High aggregation and low coupling. The delegation also clearly reflects such a feature. The value of delegation is not what it can help you solve, but what it can help you optimize your program.
In many cases, we can do the work without entrusting, and may occasionally feel that it is more troublesome to use delegation, but as mentioned above, delegation can make the code lower coupling, more readable, and easier to meet the needs.
Changes, but also reduces the amount of code.
Since there are so many advantages, we certainly need to use them. If you still have doubts about the advantages of delegation, we will complete the following example together.
There is an athlete named Green who needs to train three days a week, Monday, Tuesday and Wednesday. He does different things after training every day, but these things are regular. He wants to divide the Training Points on Monday.
The number is handed over to the manager. He does not have to pay for it on Tuesday, but he wants to record the score himself. On Wednesday, he does not need to pay or record it, but he wants to show his score to his teammates.
For such a game rule, we try to use code to implement it. First, we do not consider delegation.
Obviously, this process requires athletes to pass some information (parameters) to some methods after each training (the method submitted to the manager, the recorded method, and the method passed to other teammates) let's take the three
Methods are written in a dosomething class. The result is a string we define to record the results.

Public class dosomething
{
Public void givecodetomanager (Object sender, customeevetnargs E)
{
Result + = sender. tostring () + "scores:" + E. Code. tostring () + "sent to Manager ";
}
Public void recodecode (Object sender, customeevetnargs E)
{
Result + = sender. tostring () + "scores:" + E. Code. tostring () + "record ";
}
Public void givecodetoothers (Object sender, customeevetnargs E)
{
Result + = sender. tostring () + "scores:" + E. Code. tostring () + "to others ";
}
}
// The passed parameter contains customeevetnargs,
// This is our custom transfer information class. This class will contain the passed scores, as shown below:
Public class customeevetnargs: eventargs
{
Private string _ code;
Public String code
{
Get {return _ code ;}
Set {_ code = value ;}
}
}
// We also need a player or athlete class,
// This class calls the method in the dosomething Class Based on Time and passes the Parameter
Public class player
{
Private string _ code;
Public String code
{
Get {return _ code ;}
Set {_ code = value ;}
}
Private string _ thedate;
Public String thedate
{
Get {return _ thedate ;}
Set {_ thedate = value ;}
}
Public void oncomplete ()
{
Customeevetnargs E = new customeevetnargs ();

E. Code = This. Code;
Dosomething things = new dosomething ();
If (_ thedate = "mon ")
{
Things. givecodetomanager (this, e );
}
If (_ thedate = "Tues ")
{
Things. recodecode (this, e );
}
If (_ thedate = "wed ")
{
Things. givecodetoothers (this, e );
}
}
}
// This completes the basic types, and we will call them now
Player Green = new player ();
Green. Code = "A + ";
Green. thedate = "Tues ";
Green. oncomplete ();
Response. Write (result );

Hi, we have implemented this game. If you are a careful person, you may go back and check your code. Are you sure you want to find something unsatisfactory? You will find athletes
There are too many things in it. For example, the date is not an athlete's attribute. What is the relationship between the athlete and the date? The only event of an athlete is to complete training. He does not need to worry about other tasks.
I am so tired. Do I have to send scores and record scores? Maybe he should have an assistant to do these things, so in the athlete class, there should not be those if, they make the Code Coupling too high, when an athlete wants
When changing his training process, he had to change the player class. As a result, the delegation came out.
Define a delegate, use the delegate keyword, followed by his parameters, this delegate is to delegate him to handle the method in dosomething, so the delegate and the signature of these methods need
. Public Delegate void mydelegate (Object sender,
Customeevetnargs E );
So with this assistant, green is much easier. At last, he doesn't have to worry about the day of the week. Let's change the player class, which removes the thedate attribute, and if,
Only one delegated oncomplete event is triggered when athletes oncomplete.

 

Public class player
{
Private string _ code;
Public String code
{
Get {return _ code ;}
Set {_ code = value ;}
}
Public event mydelegate oncomplete;
Public void oncomplete ()
{
Customeevetnargs E = new customeevetnargs ();
E. Code = This. Code;
If (oncomplete! = NULL)
{
Oncomplete (this, e );
}
}
}
// This assistant depends on the day of the week and then runs the corresponding method. date is the date of today.
Dosomething things = new dosomething ();
If (date = "mon ")
{
Green. oncomplete + = new mydelegate (things. givecodetomanager );
}
If (date = "Tues ")
{
Green. oncomplete + = new mydelegate (things. recodecode );
}
If (date = "wed ")
{
Green. oncomplete + = new mydelegate (things. givecodetoothers );
}
Green. oncomplete ();
Response. Write (result );

 

In this case, if the game process changes, it has nothing to do with green. He only needs to concentrate on training, and the other is changed by this assistant.
The delegate can be added and eliminated by the + =,-=, and other delegate methods in the day of the week.
High aggregation and low coupling are very important functions of delegation.
You can also click my blog (click my avatar) to View Details. If you are interested, you can discuss them together.

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.