-2

I have following design requirements:

  • interface Server {}
  • public class ServerImpl implements Server {}, must be a Singleton
  • public class ServerABC extends ServerImpl {}
  • public class ServerXYZ extends ServerImpl {}

Now I want to put methods in class Server their implementation should be given by either ServerABC or ServerXYZ.

In ServerImpl

public class ServerImpl implements Server {
    private static Server server;

    public static synchronized RESTClient getInstance() {
        if (server == null) {
            server = new ServerImpl ();
        }
        return server;
    }
}

How can I make this work?

If I put methods in Server interface, ServerImpl has to implement it but I want the subclasses (ServerABC and ServerXYZ) to provide implementation. These methods should be called using ServerImpl object.

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
Coder
  • 3,090
  • 8
  • 49
  • 85
  • 2
    *Why* do you want it to be a singleton? The singleton design pattern is almost always a bad idea. It makes testing harder and generally has all the normal problems of global state. – Jon Skeet Jul 27 '12 at 06:06
  • Need only one instance of Server in JVM. – Coder Jul 27 '12 at 06:08
  • See http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons and google "Singletons are evil" – Ray Toal Jul 27 '12 at 06:11
  • 1
    @user958263: just because you only need one instance does not automatically mean it has to be a singleton - most of the time it's much better to make your class a normal non-singleton class, then just instantiate it only once. – Mac Jul 27 '12 at 06:12
  • 1
    I think it's only a student task. – meadlai Jul 27 '12 at 06:12
  • That's just restating it, not justifying it. Why don't you just ensure *by the way you configure your server* that you end up with one instance? – Jon Skeet Jul 27 '12 at 06:12
  • 1
    I cannot understand this use case: you want a single instance of ServerImpl but you never use it because you actually use ServerABC and ServerXYZ which provide the actual implementations? Why do you need an instance of ServerImpl in the first place ? – Bruno Grieder Jul 27 '12 at 06:20

3 Answers3

1

Make Server interface an abstract class, and prefix the methods with abstract keyword. Then, in the ServerImpl you implement those abstract methods.

EDIT:

Make ServerImpl an abstract class, and prefix those methods with abstract keyword. And in ServerABC and ServerXYZ you implement these abstract methods.

overbet13
  • 1,654
  • 1
  • 20
  • 36
  • I dont want ServerImpl to provide implementation. I want ServerABC and ServerXYZ to provide implementation – Coder Jul 27 '12 at 06:08
0

Create a abstract class named AbstractServer . Put all you necessary methods in this class and extend it in ServerXYZ and ServerABC .

Akhi
  • 2,242
  • 18
  • 24
  • I also want AbstractServer to implement **Server** interface and it should be singleton – Coder Jul 27 '12 at 06:11
0

I consider that the best way is to avoid the Singleton pattern. If you only need one instance, then create just one instance. Singleton is been known to make testing more difficult and doesn't really bring a huge benefit.

What I would do in your situation is:

  • either eliminate the interface and make the class ServerImpl an abstract class. All the methods that should be implemented in a subclass must be marked abstract as well. There are cases where you want that only one of the subclasses to provide an implementation for a specific method. In this case, just throw an UnsupportedOperationException in the classes that must not implement it. OR
  • eliminate the class ServerImpl and simply have the 2 other classes implement the interface. If the class ServerImpl doesn't implement much on its own, then this is the better way to go.
Radu Murzea
  • 10,724
  • 10
  • 47
  • 69