Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Software Engineering Design Each of the following heuristics helps make a good m

ID: 3822993 • Letter: S

Question

Software Engineering Design

Each of the following heuristics helps make a good module according to some modularity principle. Identify the principle.

(a) Make all attributes of a class protected or private.

(b) A class should capture exactly one key abstraction.

(c) Make sure an operation needs all its parameters.

(d) Spin off unrelated data into another class.

(e) Minimize the number of classes with which a class collaborates.

(f) Most of the operations in a class should use most of the attributes most of the time.

(g) Model the real world whenever possible.

(h) Do not change the state of an object without going through its public interface.

Explanation / Answer

In this context a responsibility is considered to be one reason to change. This principle states that if we have 2 reasons to change for a class, we have to split the functionality in two classes. Each class will handle only one responsibility and on future if we need to make one change we are going to make it in the class which handle it. When we need to make a change in a class having more responsibilities the change might affect the other functionality of the classes.

The Single Responsibility Principle is a simple and intuitive principle, but in practice it is sometimes hard to get it right.

Intent

A class should have only one reason to change.

Example

Let's assume we need an object to keep an email message. We are going to use the IEmail interface from the below sample. At the first sight everything looks just fine. At a closer look we can see that our IEmail interface and Email class have 2 responsibilities (reasons to change). One would be the use of the class in some email protocols such as pop3 or imap. If other protocols must be supported the objects should be serialized in another manner and code should be added to support new protocols. Another one would be for the Content field. Even if content is a string maybe we want in the future to support HTML or other formats.

If we keep only one class, each change for a responsibility might affect the other one:

We can create a new interface and class called IContent and Content to split the responsibilities. Having only one responsibility for each class give us a more flexible design:

Conclusion

The Single Responsibility Principle represents a good way of identifying classes during the design phase of an application and it reminds you to think of all the ways a class can evolve. A good separation of responsibilities is done only when the full picture of how the application should work is well understand.

  // single responsibility principle - bad example    interface IEmail {          public void setSender(String sender);          public void setReceiver(String receiver);          public void setContent(String content);  }    class Email implements IEmail {          public void setSender(String sender) {// set sender; }          public void setReceiver(String receiver) {// set receiver; }          public void setContent(String content) {// set content; }  }  
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote