I am hoping to use the MVP pattern in Android that I roughly came up with the following packages and classes:
Here are 3 packages:
com.myview
mainActivity //public
com.ipresenter
IPresenter //public
com.PresenterImpl
Presenter //protected
Presenter implements IPresenter.
What I intend to do is to make Presenter protected and can be instantiated only in mainActivity. Other words, I would like to limit its visibility and creation of this class can only be made possible through mainActivity class.
Since its constructor is implicitly declared as protected, I cannot instantiate Presenter within class mainActivity.
How could I do this without having to make Presenter public? How can I limit the visibility of a (or some) class within a package and allow its creation only through another class that lies within another package?
I thought about the classLoader Iterator method, but is this an overkill or it is not necessary because there are other ways / patterns?
Many books seem to suggest to limit the visibility of classes as much as we can, so I am taking this opportunity to learn and utilize good practice.
Addendum:
In addition to the aforementioned, my ultimate goal is to be able to declare the presenter within the mainActivity using the interface type. So, if in the future I had more than one presenters, they would all still be of the interface type IPresenter.
Related
If GestureDetector.SimpleOnGestureListener does the same thing as GestureDetector.OnGestureListener but without requiring unused code, then what is the point of OnGestureListener at all?
Rarely do I ever need to handle every type of gesture, and if I did need to do so I could do it in SimpleOnGestureListener anyways.
SimpleOnGestureListener is not the same as OnGestureListener, It is a class that implements three interfaces including OnGestureListener and the main reason is to avoid too much code. And as you said you can just not use OnGestureListener at all, but it could not be eliminated from the framework because SimpleOnGestureListener implements it.
see this https://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html
SimpleOnGestureListener also implements:
OnDoubleTapListener and
OnContextClickListener
and putting the methods in three different interfaces is to make them more understandable because they have different meaning, they don't want to make it necessary to every developer to implement unwanted methods.
But as they found that they are often used together they created a sort of helper class that gather them with default implementation, this way they can keep the interfaces separated and gathered at the same time. There is no difference between using any of the two methods.
In my program I have a menu system in which I have a separate class for each menu, for example MainMenu would be a separate class. But the class is only supposed to be instanced once, and after I instance it, it is saved in a list which is all is used for after that. Should I use another solution than a separate class? Or should I make the constructor private and then make a private instance inside the class? I feel like this violates OOP, but I don't see another solution.
Don't make the constructor private, this makes problems when you later want to unit test it.
Just instantiate it once. There are no software terrorist which secretly instantiate your class multiple times.
And avoid Singeltons, you nearly cannot reset the instance later when trying to unit test that.
I have two classes, ImageMap, extending ImageView and PageView extending GLSurfaceView, I am using the ImageMap to mainly have hot spots on drawables but I also need to add a page flip/curl animation to it, in order to do that I need those two classes to act as one object, any idea how to do that?
It is totally clear to me that multiple inheritance is not allowed in java.
There is no way of really extend two classes. What you can do is:
You make a wrapper object, that holds one instance of each object. and simply do this.ImageMap.filed1 and so. This is more convenient while developing the class. This also allows you to proxy method invocations.
You define interfaces which should be implemented, and you make a new class which implements both. This is only for class that use this class to have the interface, without really caring about the implementation.
You may need both things, since the first is about "how to do it" and the second about "how it will be presented to objects that use it".
Your question is not about Android; it's about Java.
Java does not allow for multiple inheritance.
Your reasoning is inaccurate regarding the following:
in order to do that I need those two classes to act as one object
That's not the case. An 'Activity', for example, does not have to be an event handler; it's enough if your 'Activity' can have an event handler, e.g. as an inner class which can access the Activity's variables.
Why are the lifeCycle methods in android have access specifiers as protected ?
what i understand about Access-specifiers is as below::
But why should we need to make all the life-cycle methods as
protected
I notice this when i override the lifecycle methods
I know over-riding the methods of Activity class as methods in
Activity class are defined protected
But why are they defined as protected
They are protected for encapsulation within the framework package android.app and subclasses.
They are to be called by android.app.ActivityManager (same package) only. Depending on the method implementation, things could get messed up, if one can call those methods arbitrarily, from anywhere.
So, this is by design and that design helps to avoid certain conceptual errors.
If you really must have a public method, just implement one and use it from outside and within the corresponding lifecycle method.
However, though not recommended in this case, one could override protected methods with public methods.
i am defining here why public and protected and how it work:
It's useful to have public onClick methods because you can "force" certain buttons to be clicked programmatically. A common example of this is causing the same to code to execute when the user presses the enter key, or pressed the Submit button.
I don't think Android calls Activity.onCreate directly. Note that Activity inherits from Context (which does have a public constructor). It is my understanding that the constructor triggers some events to occur, and the onCreate/Pause/Resume/Destroy methods are called internally to the class at the appropriate time.
For example, when you create an activity, the view XML file has to be parsed and inflated. This happens automatically, so there's something happening behind the scenes that you don't directly control.
Not public because those lifecycle methods are essentially used internally by the SDK and are not meant to be called by any other classes (you are not supposed to call anywhere activity.onResume() from any class, this is done automatically).
Not private to allow some custom code to be ran by subclasses.
I have a parent class in Android that inherits from Activity and all my other activities inherit from that parent class. This parent does some life cycle stuff in onPause and onResume that all my activities need. The problem is I have a Map activity that must inherit from Android's MapActivity yet I still need this activity to have my parent classes life cycle methods. Is there a way to have the MapActivity inherit from two parents? Or maybe a partial class I'm not really sure here. Any Ideas would be great.
Thanks,
Bryan
The short answer is no. You cannot have a class that inherits from two classes in Java. The standard recommendation would be to use an interface, but I don't think that's right for you in this case.
Perhaps that you can achieve the code reuse you are looking for by using composition, for example, instead of inheritance. If you post a code example, I could give you a more specific answer.
Sorry but in Java you can only extend one class. However you can implement multiply interfaces. You could have a BaseMapActivity class extend a MapActivity and then have your MainMapActivity extend that BaseMapActivity. The easiest way would be to copy the code from the already existing base Activity and put it into the MainMapActivity.
In Java you can only extend from a single class, however you are able to implement multiple classes from a single class. Another thing to consider is chaining extended subclasses together (this simulates multiple inheritance).
A better description can be found here: http://www.javaworld.com/javaworld/jw-10-2005/jw-1024-multiple.html
It’s funny, I had exactly the same problem earlier this day (but with a PreferenceActivity).
Unfortunately, I don’t think it’s possible, I ended up making a copy of my parent class and changing the name and the extends Activity into extends PreferenceActivity.