dongpeng0127 2017-10-22 14:51
浏览 10

是否会导致失去凝聚力?

Many times I experience that if I want to make a not-tight coupled system, it helps to be independent but also hurts the cohesion. Example:

model:

$record = Doctrine::getById(1);
// $record is now a bean: UserClass with getters

view:

<body>
<?php echo $record->getName().'; '.$record->getId(); ?>
</body>

this is bad since now this view is coupled to UserClass. I can refactor it to be independent:

model:

$record = Doctrine::getById(1);
$name = $record->getName();
$id = $record->getId();

view:

<body>
<?php echo $name.'; '.$id; ?>
</body>

now view is maximum independent - but also loses its cohesion. At the first solution, the data are in one place (UserClass) and cannot be scattered - unlike now. We can easily introduce a but which may cause that $id is a from another record while $name is from another?

  • 写回答

1条回答 默认 最新

  • dp411805872 2017-10-22 15:21
    关注

    To maintain what you call cohesion here you should try using patterns such as inheritance, dependency injection and factories. These will allow you to make a more loosely coupled system while maintaining a very high level of cohesion or readability.

    I think you might want to read this SO post and this article

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决