Question

I don't know what real difference between two type of using when I want to implement a class: (I run and see that they are same result)

first example: implement Renderer directly to class.

second example: create a sub-class and make this class implements Renderer

The second that I usually meet when read document on internet/ebook. Maybe solution 2 has something more benefit that I don't know.

GLSurfaceView glView;

public class TriangleOpenGLTest implements Renderer{

    public void onCreate(Bundle savedInstanceState){

        glView = new GLSurfaceView(this);
        glView.setRenderer(this);   //this line

    }

}

And:

GLSurfaceView glView;

public class TriangleOpenGLTest implements Renderer{

    public void onCreate(Bundle savedInstanceState){

        glView = new GLSurfaceView(this);
        glView.setRenderer(new Render() );     //And this line

    }

    class Render() implements Renderer{
           //Override function here 

}

Please teach me.

thanks :)

Was it helpful?

Solution

The two ways are good but I prefer the second way. Why ?

I think that is clearer. In that way, you separate clearly the specific business code about the Renderer interface and the other business code about your TriangleOpenGLTest.

It's easy to distinguish what is specific to the TriangleOpenGLTest and what is specific to the implementation of the rendering.

OTHER TIPS

You should strive for high cohesion - a class should do one job (more or less).

If a class implements an interface then that interface is part of that class' API (e.g. it shows up in its Javadoc that the class implements the interface). So you should ask yourself e.g. is the class really a Renderer and is intended to be used everywhere a Renderer can be used? Or is it just an implemenation detail?

If it's just an implementation detail then it's better to use another class (e.g. a local inner or even an anonymous class) rather than leaking the interface in the API.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top