Sequential Cohesion — Types Of Cohesion
Ahmed Samir Ahmed
Senior Android Software Engineer @IDH | Ex E-Finance fintech | Android | IOS | Dotnet
Now we will talk about sequential cohesion which takes the second stage of cohesion types as shown in figure 1.???Note that if you want to know about the first type of coherence, you can find it in the link ???here ?(Functional Cohesion).
??Lets describe second type of cohesion:
Sequential cohesion happens when the output of one section (i.e. Function) in a module becomes the input for the next section (i.e. Function). This implies that all the components within the module are linked together, with the output (result) of one part feeding into another (as a parameter) and the chain continues. However, sequential cohesion does not prescribe how the elements should be combined. A module with sequential cohesion can have multiple functions or parts that serve different purposes. This type of Cohesion favors grouping those functions with each other (same class or same package).
Again, quoting Page-Jones: ``A?sequentially cohesive?module is one whose elements are involved in activities such that output data from one activity serves as input data to the next.’’
?Let’s take an example of the Data Processing module as shown in figure 1:
?Simple code example:
Let’s examine a module that calculates a student’s GPA score based on their marks.
领英推荐
The activities mentioned above can be combined into a single function since they all require the student’s grade record as input.
public float CalculateGPA(
{
AddStudentScore(record);
CalculateSemesterGPA(record);
CalculateCumulativeGPA(record);
}
public void AddStudentScore(StudentRecord record)
{
//Add Calculation Logic
}
public void CalculateSemesterGPA(StudentRecord record)
{
//Add Calculation Logic
}
public void CalculateCumulativeGPA(StudentRecord record)
{
//Add Calculation Logic
})
?More deep code example:
public void playWithPartyEffect(Audio audio)
SoundEffect soundEffect = new SoundEffect(400, 800, 0);
soundEffect = new LiveEffectFilter().apply(soundEffect);
soundEffect = new EchoFilter().apply(soundEffect);
soundEffect = new ExtraBassFilter().apply(soundEffect);
soundEffect = new DelayFilter().apply(soundEffect);
soundEffect.playSound(audio);
}
public class SoundEffect {
private int highFrequency;
private int lowFrequency;
private long repeatAfterMillis;
public SoundEffect(int highFrequency, int lowFrequency, long repeatAfterMillis) {
this.highFrequency = highFrequency;
this.lowFrequency = lowFrequency;
this.repeatAfterMillis = repeatAfterMillis;
}
public void updateHighFrequency(int hertz) {
highFrequency += hertz;
}
public void updateLowFrequency(int hertz) {
lowFrequency += hertz;
}
public void updateRepeatAfterMillis(long millis) {
repeatAfterMillis += millis;
}
public SoundEffect copy() {
return new Sound(highFrequency, lowFrequency, repeatAfterMillis);
}
public void play(Audio audio) {
// ...
}
}
// the following functions will be grouped in the same package :
public class DelayFilter implements Function<SoundEffect, SoundEffect> {
@Override
public SoundEffect apply(SoundEffect sound) {
SoundEffect newSound = sound.copy();
newSound.updateHighFrequency(100);
newSound.updateRepeatAfterMillis(20);
return newSound;
}
}
public class EchoFilter implements Function<SoundEffect, SoundEffect> {
@Override
public SoundEffect apply(SoundEffect sound) {
SoundEffect newSound = sound.copy();
newSound.updateLowFrequency(50);
newSound.updateRepeatAfterMillis(200);
return newSound;
}
}
public class ExtraBassFilter implements Function<SoundEffect, SoundEffect> {
@Override
public SoundEffect apply(SoundEffect sound) {
SoundEffect newSound = sound.copy();
newSound.updateLowFrequency(8000);
return newSound;
}
}
public class LiveEffectFilter implements Function<SoundEffect, SoundEffect> {
@Override
public SoundEffect apply(SoundEffect sound) {
SoundEffect newSound = sound.copy();
newSound.updateHighFrequency(800);
newSound.updateLowFrequency(400);
return newSound;
}
}{
?? If you find yourself intrigued by this pattern, it is commonly known as the “Pipes and filters” pattern.
??Please be informed, this type of cohesion is not as strong as Functional Cohesion or Layer Cohesion. It pertains to a set of?pure functions?that take input and produce output without making any changes to the environment, such as saving data to variables or a database. On the other hand, the following type of cohesion, known as “Procedural Cohesion,” like this type but comprises a collection of functions that are not pure, meaning they may have side effects or modify the environment.
??Points to keep in mind: ?
? If there are questions you can connect me on?LinkedIn .
?If you are interested in more articles on my Medium account, you can use this?Link . I hope that you’re having a great day.??????