Sequential Cohesion — Types Of Cohesion

Sequential Cohesion — Types Of Cohesion

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).

No alt text provided for this image
Figure 1: The spectrum of Cohesion types from the least desirable/undesirable to the most desirable.


??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:

  • The File Reader component is responsible for reading the file and transferring its content to the Format Translator component.
  • The Format Translator component converts the content’s format and passes it on to the File Compressor component.
  • Following that, the File Compressor compresses the files and transfers the compressed file to the File Uploader component.
  • Lastly, the File Uploader component uploads the compressed file to the specified Cloud Object Storage location.

No alt text provided for this image
Figure 1: An example of Data Processing module exhibiting sequential cohesion

?Simple code example:

Let’s examine a module that calculates a student’s GPA score based on their marks.

  • Add individual mark of each subject
  • Calculate GPA score
  • Calculate cumulative GPA score

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: ?

  • The elements are interconnected in a way that the output data from one activity becomes the input data for the next activity.
  • This type of arrangement typically exhibits strong coupling and is relatively easy to maintain.
  • Due to the specific nature of the activities involved, this arrangement is not inherently designed for easy reusability since the activities are not generally useful when combined with other activities.

? 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.??????

要查看或添加评论,请登录

社区洞察

其他会员也浏览了