片段2片段沟通 [英] Fragment 2 fragment communicating

查看:125
本文介绍了片段2片段沟通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为什么要使用与其他片段模式时,我可以简单地使用

Why i should use the Communicating with Other Fragments pattern when I could simply use

((MyActivity)getActivity())doFoo();

在我的片段

推荐答案

这是最终的<一个问题href="http://stackoverflow.com/questions/2832017/what-is-the-difference-between-loose-coupling-and-tight-coupling-in-object-orien?lq=1">coupling和重用性。

This is ultimately a question of coupling and re-usability.

如果您使用您发布的code例如,在你的问题你的片段不重复使用与其他活动。它是<一个href="http://stackoverflow.com/questions/2832017/what-is-the-difference-between-loose-coupling-and-tight-coupling-in-object-orien?lq=1">tightly再加 。这很好,如果你的工作在你自己的,你不觉得任何人都可能使用过或更高版本(包括你自己)看了你的code和你肯定永远不会需要使用片段其他地方。但总体来说你还是应该使用正确的模式,只是为了获得进入使用它的习惯。

If you use the code example you posted in your question your Fragment is not re-usable with other activities. It is 'tightly coupled'. It's fine if you're working on your own and you don't think anyone is likely to ever use or read your code later (including yourself), and that your definitely never going to need to use that Fragment anywhere else. But generally speaking you should still use the proper pattern, just to get into the habit of using it.

如果你得到一个位置编码在一家公司保持松耦合将帮助您和您的同事,从长远来看有很大的交易,意味着你可以直接回到你的code和重新使用它在以后的项目,如果要求。

If you ever get a position coding in a company keeping 'loose coupling' will help you and your co-workers a great deal in the long run, and means you can go straight back to your code and re-use it on later projects if called for.

  • <一个href="http://stackoverflow.com/questions/226977/what-is-loose-coupling-please-provide-examples?lq=1">Further阅读。
  • <一个href="http://gamedevelopment.tutsplus.com/tutorials/quick-tip-the-oop-principle-of-coupling--gamedev-1935"相对=nofollow>更进一步的阅读。
  • 甚至更远进一步阅读。
  • Further reading.
  • Even further reading.
  • Even farther further reading.

这篇关于片段2片段沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆