Replace inheritance with a combination to what can be brought to the Activity and what can be brought to the activity

Source: Internet
Author: User

Replace inheritance with a combination to what can be brought to the Activity and what can be brought to the activity
What can inheritance bring to the Activity by using combinations?

  • Original article: Composition over Inheritance, What it means for your Activities
  • Author: Josh Brown
  • Source: Development Technology Front Line www.devtf.cn
  • Translator: chaossss
  • Proofreader: Mr. Simple
  • Status: complete

As a matter of fact, we have seen in many advanced Java books the suggestion that "Combination rather than inheritance should be preferred during development". Why do we suggestMore inclinedInsteadCompletely replacedThis is because when class A can completely replace another class B (we want B to be the parent class of Class A), we should use inheritance. If A is just having something in common with BActionAnd should not be inherited (more discussion timestamp me ). However, when I read other people's source code, there are too many cases of misuse of inheritance. When many people have created a BaseActivity, all the activities will inherit it, implement business logic in sub-Activity.

In addition, there are many problems. The most vivid example is that Joe Newguy is added to our group and implements ShinyFeatureActivity. There is no rule in the group that forces him to inherit ShinyFeatureActivity from BaseActivity, but he did it ...... We found this problem during Code Review. In addition, if every Activity inherits from BaseActivity, in some cases, you may inherit other activities (such as PreferenceActivity and ListActivity ). Although most Activity subclasses are replaced by corresponding Fragment, some of them do not correspond to Fragment, and some libraries still need corresponding Activity subclasses.

Some of the more potential problems are: sometimes some activities require these behaviors, while other activities require other behaviors, while Java does not support multi-inheritance, this means that we have to put all the actions of the Activity with overlapping behaviors into an independent class. However, this will reduce maintainability and even affect performance.

In fact, the motivation for doing so is simple: code reuse. Indeed, code reuse is important. Most of our public logic needs to be implemented in a certain part of the Activity lifecycle. However, Application. activityLifecycleCallbacks is a tough thing and may need to be used in Application. the onCreate () method registers it. The most ironic thing is: we try our best to avoid using it in the Application. register it in the onCreate () method ......

This is also the origin of Fragment without a bound View. When countless Android Developers regard Fragment as a UI component, Fragment is actually more like a lifecycle component. Why is there no bound View for these Fragment statements? Because in the hands of these developers, the onCreateView () method of Fragment is neither overwritten nor null is returned. In essence, Fragment is a component that can process or manipulate events, but it does not have its own View.

In order to distinguish Fragment without a bound View from Fragemnt for a View, I will name Fragemnt without a bound View* XXHelper *And others are namedXXFragment. For example, AnalyticsHelper represents the Fragment of the association analysis logic, while HeaderFragment displays a title bar. Of course, you can try this, or ignore my suggestions. I feel very useful ~

Because there are no UI components in the Fragment without a bound View, it means that in these Fragment, we do not need to consider the Layout-ID required to initialize the Layout, or the animation required by the View, we can use the factory model to develop these Fragment, improving the ease of use and handling of factory methods. In this case, they can still add Fragment. I have created a Gist to show you how to add Fragment. If you are interested, click here. If you use Android-Studio for development, you can add it toFile and Code TemplatesWhen you create a new classKindSelect it from the drop-down list.

It is very easy to add FooHelper to its parent class. You only need to call FooHelper. attach (this. However, if the parent class does not implement the FooHelper callback interface, a compilation error will occur. In addition, if the attach () method has been called, the returned value of this method will be the previous Fragment. This Gist contains reloads on Fragment and Activity, and converts them to use supported Fragement and FragmentActivity, which is of great significance. It also contains the simplified version of FragmentUtils. getParent () -- getParent () method (details stamp me ).

Obviously, the Fragment without a bound View is much easier than BaseActivity. They encapsulate the processing methods that require lifecycle callback (or onActivityResult (), FragmentManager. The best thing is that we can break down some logics shared by the Activity into modules with a single responsibility. If the Activity requires any logic, we can choose the module to use. If most of your activities require many identical modules, you have no reason not to implement CommonComponentsHelper to process these shared logics, in addition, you do not need to place all the shared dependencies of the Activity in a base class.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.