Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mức độ gắn kết với Application Service #40

Closed
tuananhhedspibk opened this issue Oct 3, 2021 · 0 comments
Closed

Mức độ gắn kết với Application Service #40

tuananhhedspibk opened this issue Oct 3, 2021 · 0 comments

Comments

@tuananhhedspibk
Copy link
Owner

tuananhhedspibk commented Oct 3, 2021

Mức độ gắn kết ở đây được hiểu là phạm vi mà module có thể bao quát, tập trung được.

Để đo mức độ gắn kết ta có "Lack of Cohesion in Methods - (LCOM)" . Được tính dựa theo tỉ lệ giữa: tổng số biến instance và số biến instance được các methods sử dụng.

Cùng lấy ví dụ về "mức độ gắn kết cao" và "mức độ gắn kết thấp".

class LowCohesion {
  private number v1;
  private number v2;
  private number v3;
  private number v4;

  number methodA() {
    return v1 + v2;
  }

  number methodB() {
    return v3 + v4;
  }
}

Ở class LowCohesion thì methodA chỉ sử dụng v1, v2 chứ không sử dụng v3, v4. Điều tương tự cũng xảy ra với methodB.

Nếu tách thành 2 class như sau:

class HighCohesionA {
  private number v1;
  private number v2;

  number methodA() {
    return v1 + v2;
  }
}

class HighCohesionB {
  private number v3;
  private number v4;

  number methodB() {
    return v3 + v4;
  }
}

Mỗi method của class đều sử dụng toàn bộ các thuộc tính của class.

ApplicationService với mức độ gắn kết thấp

Với ví du về UserApplication ở các phần trước, ta thấy chỉ có register method là sử dụng cả userRepositoryuserService còn delete method thì chỉ sử dụng userRepository vậy nên theo quan điểm về mức độ gắn kết thì cách viết này cho mức độ gắn kết thấp. Ta có thể tách thành 2 class UserRegisterServiceUserDeleteService

Do tách class nên để tập hợp chúng lại, ta cần sử dụng package

VD:

  • Application.User.UserRegisterService
  • Application.User.UserDeleteService

Screen Shot 2021-10-04 at 8 28 07

Khi mọi xử lí liên quan đến user đều có thể được tìm thấy trong folder User thì sẽ rất dễ dàng cho dev sau này.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant