什么是碎片背后的设计逻辑,静态内部类VS独立的公开课? [英] What is the design logic behind Fragments as static inner classes vs standalone public classes?

查看:133
本文介绍了什么是碎片背后的设计逻辑,静态内部类VS独立的公开课?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能够理解的Andr​​oid软件设计的一个重要方面而我刚开始接触,从我所知道的片段设计,以分离被采纳在code,其中的直觉是,活动仍保持原样和片段可以的再利用的其他地方,甚至在不同的活动,或者沿侧的其他片段,在像一个主/从流动或横向的UI。

好了,所以我看到相当多的少数问题上的SO问为什么片段放置静态内部类的活动和答案,就是如果我们没有让他们一成不变的,在片段可持有的引用,活动,如屏幕旋转或重的东西-draw可能泄漏的活动什么的。

这让我又回到了起点,我有问题,好吧,如果片段设计,以解耦code被采纳,那么我们为什么要结婚了片段活动通过将它的的活动类,而不是把它们作为独立的公开课?这难道不是完全矛盾的生存片段的?

有一个项目结构这样的一面是什么?其中每个片段是一个单独的它自己的阶级?鉴于我的片段code可以长到特别喜欢一个1000行试图做的动画,我看到这是更为简洁的时候,去耦,并且在其他比它的预期父活动活动,可重复使用。

  • 请大家还是上面提到的内存泄漏问题受苦?
  • 在我失去了一些关于设计的逻辑是什么?请教育我。
  • 有没有一种方法,其中的片段留作为一个内部类,但仍然可以在其他地方使用?我可能会失去了一些东西,以及...所以不要通知我。

任何其他项目的设计方法,概念,更正我的直觉非常欢迎,因为我刚开始在这里,我很想知道我所有的选项。

感谢您:)

解决方案
  

这让我又回到了起点,我有问题,好吧,如果片段设计,以解耦code被采纳,那么我们为什么要通过把它里面结婚的片段活动活动类,而不是把它们作为独立的公开课?这难道不是完全矛盾的生存片段的?

有关片段的主要驱动力是不脱钩,而是组成:可重复使用的部分,可以很容易地组合在一起的不同配置的用户界面。从可组合遵循模块化,并从中去耦所以是的,脱钩是有,但它不是主要的问题。

阅读有关片段的设计理念

模块化硬币的另一面:如果两件事情属于一个整体,如一个活动,一个片段只与该活动中使用,他们是最好保持在一起,不是S $ P $垫出各地codeBase的使它们更容易演变在一起。项目结构就像你的问题在​​哪里活动和片段都在单独的包不会真的遵循这个原则。

一种常见的方式保持单独的类碎片,但接近有关的活动,这就是在同一封装内使用命名preFIX所以他们一起排序的字母列表:如FooActivity与FooDetailsFragment。

对于简单片段那里没有那么多code和片段只在一个活动中使用,这是完全没有让他们成为静态内部类的活动。

只是尽量保持一致,以便其他人阅读code可以很容易地找到自己的方式围在codeBase的和WTFs /分钟code度量保持在较低水平。

  

难道我们还在上面提到的内存泄漏问题受苦?

没有,泄漏仅适用于持有引用外部类的非静态内部类。包级类没有任何外部类召开一个参考。

由于片段共享托管活动的生命周期,泄漏的外部对象是不是真的有碎片的关注。它只是框架需要能够实例化的碎片,没有任何外部类对象,静态是一个必要条件。

  

有没有一种方法,其中的片段留作为一个内部类,但仍然可以在其他地方使用?我可能会失去了一些东西,以及...所以不要通知我。

您可以参考公开内部类与code或外$内蒙古 Outer.Inner C>与反思(如XML文件)。

尽管这在技术上是可能的,这样说你依靠一个类的内部,这是一个设计的气味。我真的是指内部类的外部类code本身。

I am not able to understand one important aspect of Android software design which I am just starting out with, from what I know Fragment design has been adopted in order to decouple the code, where the intuition is that the Activity remains as is and Fragment can be reused else where, maybe even in a different activity, or maybe along side other fragments, in something like a Master/Detail flow or landscape UI.

Okay so I have seen quite a handful of questions on SO asking as to why Fragments are placed as static inner classes within an Activity and the answer there is that if we do not make them static, the Fragment may hold a reference to the activity and something like a screen rotation or a re-draw might leak the activity or something.

This brings me back to square one and I have the question that, well, if the Fragment design has been adopted in order to decouple the code, then why are we marrying the Fragment to the Activity by putting it inside the activity class and not putting them as standalone public classes? Isn't that totally contradicting to the very existence of Fragments?

What is the down side of having a project structure as such? Where each Fragment is a separate class of its own? Given that my Fragment code can grow to like a 1000 lines especially when trying to do animations I am seeing this as far more neater, decoupled and reusable within activities other than it's intended parent Activity.

  • Do we still suffer from the memory leak issue mentioned above?
  • Am I missing something about the design logic? Please educate me.
  • Is there a way where the Fragments stay as an inner class and still be used elsewhere? I might be missing something here as well... so do inform me.

Any other project design methods, concepts, corrections to my intuition are more than welcome since I am just starting out here, I would love to know all my options.

Thank you :)

解决方案

This brings me back to square one and I have the question that, well, if the Fragment design has been adopted in order to decouple the code, then why are we marrying the Fragment to the Activity by putting it inside the activity class and not putting them as standalone public classes? Isn't that totally contradicting to the very existence of Fragments?

The main driver for fragments isn't decoupling but rather composition: reusable pieces of user interface that can be easily composed together in different configurations. From composability follows modularity and from it decoupling so yes, decoupling is there but it's not the primary concern.

Read on about fragment design philosophy.

The modularity coin has another side: if two things belong together such as an activity and a fragment that is only used with that activity, they are best kept together and not spread out all over the codebase so they are easier to evolve together. A project structure like in your question where activities and fragments are in separate packages would not really follow this principle.

One commonly seen way to keep fragments in separate classes but close to related activities and such is to use a naming prefix so they sort together in alphabetical lists within the same package: e.g. FooActivity with FooDetailsFragment.

For simple fragments where there isn't that much code and the fragment is only used in one activity, it's perfectly fine to have them as static inner classes in that activity.

Just try to be consistent so other people reading the code can easily find their way around in the codebase and the WTFs/minute code metric is kept low.

Do we still suffer from the memory leak issue mentioned above?

No, the leak applies only to non-static inner classes that hold a reference to the outer class. Package-level classes do not have any outer class to held a reference to.

Since fragments share the hosting activity's lifecycle, leaking the outer object is not really the concern with fragments. It's just that the framework needs to be able to instantiate the fragments without any outer class objects, and static is a requirement for that.

Is there a way where the Fragments stay as an inner class and still be used elsewhere? I might be missing something here as well... so do inform me.

You can refer to public inner classes with the dot notation Outer.Inner in code or Outer$Inner with reflection (e.g. XML files).

While this is technically possible, doing so says you're relying on the internals of a class and that's a design smell. I'd really refer to inner classes in the outer class code itself.

这篇关于什么是碎片背后的设计逻辑,静态内部类VS独立的公开课?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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