2

My question pretty much close to Java notify/observer design pattern . I worked in different java / Android programming. One thing i wanted to understand is how publisher know what are the class implementing the given interface. For example in android when camera is ready there is a method being calling if you implement a particular interface . How android calling this method method when camera open with out registering my class to android.

class mydevice implements camerainterface{

    public void cameraup(){
    // Implementing my logic when camera up. How android know that there is class //mydevice which is implementing my interface
    }

}

How android know my class mydevice and calling this cameraup method.

I hope ,i explained my question in detail to understand.

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
rajeswaran
  • 31
  • 2
  • In java, you can do `if (myObject instanceof FooInterface)` to see if an object implements an interface. – Turtle May 15 '17 at 14:16
  • 1
    He is asking how the underlying system knows it has to invoke his class because it implements the interface – Tim May 15 '17 at 14:18
  • @TimCastelijns The question is on the mapping then? Similar to the web.xml in a web application? – Turtle May 15 '17 at 14:21
  • yes Correct , I would like to understand underlying system knows it. In a particular jar / Packages there may be 100s of class's . but out of 100 class 5 class may implemented that interface. how does android code find those class and calling that implemented method. – rajeswaran May 15 '17 at 14:22
  • Please adhere to the Java naming conventions. – Lew Bloch May 15 '17 at 17:23

4 Answers4

1

In observer pattern, the publisher will define the contract (Interface and it's methods) based on the requirement not the Subscriber. Subscriber(s) will implement the Interface and register them selves as subscriber objects.

But in your case, you are the subscriber and you are trying to defining the contract (camerainterface) which is not observer pattern.

First you have to check which Broadcast or Callbacks Android (in our case Publisher) providing for your requirement and you have to implement that contract as register as a subscriber.

Hope this helps.

Hanman
  • 19
  • 2
  • Thanks for the reply , let me as my question more specific .https://developer.android.com/reference/android/hardware/Camera.PreviewCallback.html . In the link i taken a interface Camera.PreviewCallback as sample. this interface has method called onPreviewFrame(byte[] data, Camera camera) . This method will be called when ever that camera screen preview changes . As i programmer my job just to implement that interface and method. when ever cameraframe changes it get executed.nothing else i am doing. My question is how android know that i have implemented that interface and calling my method. – rajeswaran May 15 '17 at 14:46
  • 1) In order to work with android.hardware.Camera and it's apis, we must have called open(int cameraId) method and camera object (ex: cameraObject). 2) Camera exposed a method setPreviewCallback(Camera.PreviewCallback) which takes any object which is of type Camera.PreviewCallback. In step1 we already had a reference to Camera and we can register our client object which implements Camera.PreviewCallback using cameraObject.setPreviewCallback(Camera.PreviewCallback). This is the place where we subscribed our client to camera hardware. Here android.hardware.Camera is the Publisher. – Hanman May 15 '17 at 16:19
0

Well if I understand correctly your question, you are mixing interface and broadcasting.

Roughly, an interface is never aware of what classes are implementing it.

If you create myDevice that implements say cameraInterface this will not do anything as long as Android isn't aware that it has to do something with this class. There will be a point where you will have to tell Android which class to work with, otherwise it's content will never be triggered.

Broadcasting is when you tell Android "there is that class that must be aware of an event. Trigger it's special method when this event happens.". For example, if you want to trigger some code when the camera is opened (I don't think this is possible, but that's just an example) then you will have to declare your class as a BroadcastReceiver and specify in the Manifest.xml which events it is supposed to listen to.

Arthur Attout
  • 2,701
  • 2
  • 26
  • 49
  • *If you create myDevice that implements say cameraInterface this will not do anything as long as Android isn't aware that it has to do something with this class.* he is asking how android is aware of this, not what an interface or broadcast is – Tim May 15 '17 at 14:25
  • According to the comments, this is something OP must be aware of, as that doesn't seem to be completely clear. – Arthur Attout May 15 '17 at 14:27
  • what do you mean by "this" and "that"? – Tim May 15 '17 at 14:28
  • OP seems to want to know how Android can tell that an interface has been implemented, to trigger the according code. The second line of my post explains why this cannot be done. The rest of the post explains how it can be implemented to do the equivalent. – Arthur Attout May 15 '17 at 14:30
  • Thanks for the reply , let me as my question more specific .developer.android.com/reference/android/hardware/… . In the link i taken a interface Camera.PreviewCallback as sample. this interface has method called onPreviewFrame(byte[] data, Camera camera) . This method will be called when ever that camera screen preview changes . As i programmer my job just to implement that interface and method. when ever cameraframe changes it get executed.nothing else i am doing. My question is how android know that i have implemented that interface and calling my method. – rajeswaran May 15 '17 at 14:52
  • i am not defining my class as BroadcastReceiver . i keep normal class that implement the method . – rajeswaran May 15 '17 at 14:55
  • If you want to use your custom class as a callback, you will have to use `setPreviewCallback(instanceOfYourClass)` – Arthur Attout May 16 '17 at 07:16
0

Your question seems pretty broad to me, so I'll try to take a different route. The concept you are trying to refer to is not specific to Java. Generally, it is applicable to whenever you are passing virtual references to objects and is handled by the compiler at runtime.

What happens is that any time you pass a reference as an interface (or a parent abstract class), the reference of the concrete object gets stored in what is called a virtual lookup table or vtable.

Take a look at this

So, when a publisher raises an event, the reference to concrete object of the publisher gets stored in a vtable, and also gets passed to the observer as interface. You can think of interface as nothing more than a pointer to the actual object.

Community
  • 1
  • 1
Farhan
  • 1,000
  • 1
  • 11
  • 22
-1

How android know my class mydevice and calling this cameraup method?

Attending you're asking how does your JVM know your class implements cameraInterface, they could simply use the instanceof keyword, something like if (myDeviceObject instanceof cameraInterface) .

EDIT As it seems what you're asking is more of a configuration question:

You should take a look at this link, where it's stated that

You can either dynamically register an instance of this class with Context.registerReceiver() or statically declare an implementation with the tag in your AndroidManifest.xml.

Well, this summarize pretty well how class registration works in Android. Either you registered your class from Context, your framework did it for you, or it is defined in your AndroidManifest.xml

For the "your framework did it for you" part: You should search for dependency injection and annotation for injection. It can be use to indicate to the compiler that it should scan the classes to find an implementation of the interface.

Turtle
  • 1,626
  • 16
  • 26
  • Do you mean does JVM scan all the class in the Jar to find which class has implemented what intefaces and register them ? – rajeswaran May 15 '17 at 14:24
  • Adding more , For example i have developed a API and there a interface in that API called weatherupdate which has abstract method changeweather() . If some one want to know weather changes info they need to implement weatherupdate update class and changeweather(int c) method . How do i need to notify all the class where ever my interface implemented and provide them update. I dont want them to register their class like set_listener. just simply implement my interface that will be called when ever weather changed. – rajeswaran May 15 '17 at 14:28