Question

I'm writing a little framework that should wrap some routines. I base it on Spring, make use of @Autowired. In my framework I wire interfaces and abstract classes. So that the implementating project can provide the actual implementation when using the framework as dependency.

But: how can I force anyone who works with my framework having to implement a specific interface (apart from documentation of course)?

Is there a way?

Example of a framework class:

interface IFoo {
   void run();
}


abstract class Bar {
   @Autowired private Ifoo ifoo;


   void routine() {
       ifoo.run();
   }
}

Implementation project: can I force the user to either:

class CustomBar extends Bar

or: CustomFoo implements IFoo ?

My goal is to write some kind of template engine framework. An then the user of this dependency only has to provide logic by implementing certain interfaces or abstract classes, without having to care about the execution logic that is wrapped up inside the template framework I create.

Was it helpful?

Solution

Can I force the user to either:

   class CustomBar extends Bar

or:

   CustomFoo implements IFoo ?

They can't avoid it. Let's look at them in turn:

In order to provide an instance of Bar to your framework, they have to create the instance. But Bar cannot be instantiated, it's an abstract class (you declared it as abstract class Bar). To create an instance, you have to subclass it and then create an instance of the subclass.

Similarly, IFoo is an interface. You can't instantiate an interface. To create an instance that uses the interface, you have to define a class that implements the interface, then create an instance of that class.

Absolutely you want to document things as well, but by making the classes abstract and using interfaces, you're forcing them to subclass/implement if they ever want to create instances to pass into your framework.

OTHER TIPS

I think you have to document your framework : To do a specific think you have to create a class which implements a specific interface. (like in the spring documentation)

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