1

I made an Interface having all common methods a player should have. I implemented it in my code, my colleague too. But many of the developers who were not aware with my interface created their own methods like playMyPlayer() etc.

How can I force other programmers to implement my interface ?

5
  • 2
    Talk to them? No need to force them if the only reason they didn't use your interface was because they didn't know it was there...
    – user684934
    Oct 25, 2011 at 13:32
  • 1
    ... and if you had to force them, you have larger problems than disagreement over which interfaces to use.
    – user395760
    Oct 25, 2011 at 13:34
  • If you've methods that are taking parameters of the type of this interface, how are they not implementing it. How can they get it to work at all?
    – Jon Hanna
    Oct 25, 2011 at 13:44
  • Well!! i was looking for some technical solutions. I found some way like creating methods which accept Interface type object only, in such a case programmer will become aware with the interface and will implement that interface in his class. But no idea where to fit that class...
    – noquery
    Oct 25, 2011 at 13:53
  • If you don't already have methods that only accept that interface - so as to then use that interface - why do you want to force them to use it anyway?
    – Jon Hanna
    Oct 27, 2011 at 1:45

3 Answers 3

1

Explaning with your own scenario:

Interface

interface Player{
    public void play();
    public void pause();
    public void stop();
}

Classes which are implementing Player Intreface;

class AudioPlayer implements Player{...}

class VideoPlayer implements Player{...}

Some extra class. Meaningless but seems relevant

Class PlayList{}

An enum who plays important role;

enum MEDIAPLAYER {
    AUDIO,VIDEO;

    public Player getPlayer() {
        switch (this) {
            case AUDIO:
                return new AudioPlayer();
            case VIDEO:
                return new VideoPlayer();
            default:
                return new AudioPlayer();
        }
    }
}

Using your code

Player testPlayer = MEDIAPLAYER.getPlayer();
:
testPlayer.play();

Now if any new player is added, its entry will go to MEDIAPLAYER which is returning Player type object. So everyone would have to implement Player

Another approach:

You can create an abstract class, say MasterPlayer, who is having all the abstract methods Player interface has. In addition, MasterPlayer will have some additional methods like managing PlayList

3
  • Thanks! This is exactly what i wanted.... thanks a lot. Is it some design pattern or just a solution for specific problem?
    – noquery
    Oct 28, 2011 at 12:00
  • As per your second approach i can convert Player interface to MasterPlayer abstract class. Is there any advantage to do so? Which approach is better?
    – noquery
    Oct 28, 2011 at 12:20
  • For the second case, you could simply use Player to reference all kinds of players, e.g. VideoPlayer AND AudioPlayer, but calling any of the abstract/virtual members will call the members of the real class. As for a game, you could do a base class GameObject - then put all kinds of game objects into a list and iterate over it, to update all game objects without needing any switch statements or whatever.
    – Mario
    Nov 20, 2011 at 19:35
1

The only way to do this is to create a useful class that requires object instances implementing your interface:

public class UsefulClass {
    public void DoSomethingIrresistable(IPlayer player) { 
        // ... implementation here
    }
}

Anyone that wants the goodies has to find or create a class that implements IPlayer.

Otherwise, why would anyone implement the interface in the first place? You create interfaces to guarantee certain class members exist, not to force people to organize their classes the way you think they should be designed.

1
  • This is same what i thought as I commented above. It is required when we don't let other change name of important methods so in the future i can put a functionality, who calls those methods using reflection, without extra changes. Suggest if some more ways in your mind
    – noquery
    Oct 27, 2011 at 5:47
0

You identified the main problem here yourself, its not that these developers chose not to use your interface, they didnt know it existed.

I think the main thing here is to implement a system of informing your developers what code they can access and a wiki or some other knowledge repository where they can search for what they need.

Once you have a system for informing your developers what interfaces / code samples / whatever they have available to them it is going to be much more easy to make sure they use it. And in all fairness if the code you provide is well written and supports the features they need most developers will use it anyway as it saves them work.

You should however put something in your coding standards / code of conduct about reusing existing code rather than rewriting BUT you cant do this till you have a solid system in place for informing developers what code is available for them to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.