Android - Can't Apply Decorator Pattern on Act

2019-08-30 17:23发布

问题:

I would like to be able to dynamically build an Activity that has a few of the capabilities that we have (such as prevent scrolling, monitors internet connectivity, prevent orientation change, etc.).

We don't want to have to create an abstract BaseActivity that contains all possible properties and abstract methods for all capabilities. We would have a lot of empty function implementations in subclasses of BaseActivity who only need to actually implement a few of those functions. Worse yet, if we think of another capability (another set of properties and functions to add to BaseActivity), we would have to edit EVERY child of BaseActivity to implement the new abstract functions (most likely going to be empty).

Another approach of BaseActivity inheritance is to create child classes that implement only a few of the capabilities we have. But this will require us to create a child class for every combination of capabilities, which is too many even with few capabilities. Plus, this would result in lots of duplicate code for 2+ child classes that would implement the same capability.

So, I thought of implementing the Decorator Pattern, but I don't think we ever actually instantiate a new activity. We always create an Intent and specify SomeActivity.class in its constructor, then we call startActivity(new Intent(getApplicationContext(), SomeActivity.class));

Is there a way to intercept the actual instantiation of an Activity, perform Decorator Pattern manipulation on it, and then let the OS/Application put it on top of the stack?

If not, what other approaches are feasible?

回答1:

We don't want to have to create an abstract BaseActivity that contains all possible properties and abstract methods for all capabilities.

You can accomplish this by deleting the keyword abstract. That takes approximately eight keystrokes per keyword.

Is there a way to intercept the actual instantiation of an Activity, perform Decorator Pattern manipulation on it, and then let the OS/Application put it on top of the stack?

Only by forking Android.

If not, what other approaches are feasible?

Create a non-abstract BaseActivity that "contains all possible properties" and non-abstract "methods for all capabilities". Override the methods as needed in subclasses. Where it makes sense, have the subclasses chain to the superclass, so the superclass can have default behavior that the subclass can augment.

You can see this in Activity itself. You do not have to implement onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy(), onSaveInstanceState(), onRestoreInstanceState(), and many other methods. Those methods are not abstract, and the default implementations are reasonable (with the typical exception of onCreate()). Subclasses can override those methods where desired to add functionality, or in some cases replace the existing functionality (e.g., onSaveInstanceState() can chain to the superclass or not, as desired).



回答2:

I think this post can help you. Decorator for activities