본문 바로가기

BackEnd

[SW공학] 절차적 응집도와 해결방안

반응형

모듈의 정의(by chatGPT)

소프트웨어에서 모듈은 프로그램을 구성하는 각각의 요소를 모듈로 구축하는 것을 말합니다. 

모듈은 재사용 가능한 프로그램을 말하며, 내부 변수, 함수, 클래스 등의 코드 블록을 정의하고 다른 파일에서 import 문으로 가져올 수 있습니다

 

[ 모듈화에 대해서 ]

모듈화(Modularization) 시키는 방법 모듈이란 “분리되고, 독립적이며, 교체&n...

blog.naver.com

모듈을 사용하면 프로그램을 더욱 모듈화하여 유지보수성을 높일 수 있습니다.


결합도와 응집도

응집도는 각 소프트웨어 모듈의 완성도를 측정하는 기준이다.

응집도가 높으면 유지보수성과 완성도가 높은 모듈이다.

 

유사한 개념으로 결합도가 있는데, 이는 모듈 간의 의존성을 나타낸다.

모듈 간 의존성이 높을 수록 하나의 모듈이나 마찬가지기 때문에

각 소프트웨어 모듈은 독립적으로 발전할 수 없다.

응집도와 결합도


절차적 응집도

모듈이란 개념은 클래스로 구현할 수 있다.

그리고 절차적 응집도의 정의는 다음과 같다.

절차적 응집도 정의(chat gpt)

절차적 응집도란 모듈 내부의 구성 요소들이 순차적으로 수행되는 경우를 말합니다.

이를 없애기 위해서는 모듈 내부의 구성 요소들이 하나의 목적을 위해 연결되어 있어야 합니다.

이를 위해 높은 응집도와 낮은 결합도를 가지도록 구성하게 하면 됩니다.

 

먼저 짚고 넘어가야 할 것은 절차적 응집도는 모듈 내의 관계 개념이다.

따라서, 다른 모듈에서 호출할 때는 상관없다.

단지, 자기 자신의 메서드를 호출할 때 문제가 된다.

 

보통 아래와 같이 커넥션을 만들고 쿼리를 실행하는 메서드를 같은 클래스 내에 정의하는 경우이다.

public void ExecuteQuery(
    {
        if (OpenConnection())
        {
            if (ExecuteQuery(command, "select * from product"))
            {
                CreateResult(dataset);
            }
            CloseConnection(connection);
        }
    }

// the procedures that handles files are put in the same class or package :

    public bool OpenConnection()
    {
        //Logic for SQL Connection 
        return true;
    }
    public DataSet ExecuteQuery(SqlCommand command, string query)
    {
        //Logic for Command execution 
        return new DataSet();
    }
    public DataTable CreateResult(DataSet dataAdapter)
    { 
        //Logic for result creation
        return new DataTable();
    }
    public bool CloseConnection(SqlConnection connection)
    {
        //logic to close connection
        return true;
    })

절차적 응집도 제거 방법

그럼 해당 응집도를 어떻게 없앨까?

 

쓰는 곳에서 비즈니스 로직에 맞게 순차적으로 쓰게 하면 된다.

그리고 모듈 내부에서 응집도를 낮추면서 직접 관리하는 부분을 없앤다.

public void updateFiles()

    StorageManager manager = new StorageManager();
    manager.readOldFileFromDisk();
    manager.scanOldFileForNewLines();
    manager.scanOldFileForWhiteSpaces();
    manager.fillInNewFile();

}

// the procedures that handles files are put in the same class or package :

public class StorageManager {

    private static final String OLD_FILE_PATH = "my_old_text_file.txt";
    private static final String NEW_FILE_PATH = "my_new_text_file.txt";
    private File oldFile;
    private File newFile;

    public void readOldFileFromDisk() {
        oldFile = new File(OLD_FILE_PATH);
    }

    public void scanOldFileForWhiteSpaces() {
        if (oldFile != null) {
            // process on "oldFile" variable
        } else {
            throw new UnsupportedOperationException("invoke readOldFileFromDisk() first");
        }
    }

    public void scanOldFileForNewLines() {
        if (oldFile != null) {
            // process on "oldFile" variable
        } else {
            throw new UnsupportedOperationException("invoke readOldFileFromDisk() first");
        }
    }

    public void fillInNewFile() {
        newFile = new File(NEW_FILE_PATH);
        // ...
    }
}{

 

만약 맨 처음 예제로 치환한다면,

oldFile 부분이 커넥션 체크 로직으로 바뀔것이고

executeQuery 마지막에 매니저를 통해 커넥션을 릴리즈하는 코드를 작성해줘야 할 것이다.

JDBC매니저의 설계와 동일하다.


참고

(3) Procedural Cohesion — Types Of Cohesion | LinkedIn

 

Procedural Cohesion — Types Of Cohesion

📚Overview of Cohesion: Modularization plays a crucial role in software engineering systems by facilitating easy comprehension and maintenance of the system. Cohesion, defined as the level of functional interconnection among various elements within a mod

www.linkedin.com

 

반응형