dsfdsf48652 2012-07-20 14:39
浏览 37
已采纳

OOP内聚/耦合混淆

I have a question regarding the correct way of splitting objects.

Say I have two objects, such as Tool and Technology. A Technology can have multiple Tool objects associated with it, but is it considered low cohesion/tight coupling for a Technology to know directly about a Tool? Is it bad practice to have the Technology object to contain an array of Tool objects or should there be some sort of third class that would tie them together?

Tool
private id;
private title;

Technology
private id;
private title;
private tools;

Could you provide a simple overview of how it would be best to associate the to objects with one another? At first glance it seems like it would be ok to me, but would it really be appropriate for a Technology object to have getter/setter methods for adding Tools?

  • 写回答

4条回答 默认 最新

  • dpli36193 2012-07-20 14:47
    关注

    If Technology needs Tool objects, there's nothing wrong with it keeping them.

    If the Tools are relevant to the Technology object state, than it should contain them inside of it.

    Relevant possible methods:

    public addTool(Tool $tool);
    public removeTool(Tool $tool);
    public removeToolByIndex($index);
    public getToolFromIndex($index);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害