Domanda

This concept is unclear with me.

I have worked on several frameworks for an instance Spring. To implement a feature we always implement some interfaces provided by the framework.

For an instance if I have to create a custom scope in Spring, my class implements a org.springframework.beans.factory.config.Scope interface. Which has some predefined low level functionality which helps in defining a custom scope for a bean.

Whereas in Java I read an interface is just a declaration which classes can implement & define their own functionality. The methods of an interface have no predefined functionality.

interface Car
{
topSpeed();
acclerate();
deaccelrate();
}

The methods here don't have any functionality. They are just declared.

Can anyone explain this discrepancy in the concept? How does the framework put some predefined functionality with interface methods?

È stato utile?

Soluzione

There is no difference between how a framework handles interfaces and how your code does. There is a concept called Interception. Frameworks often times have classes that intercept calls on interfaces and run their own code before or after your implementation runs.

A simple roll-your-own way to do interception in a framework that you are writing would be to use the decorator pattern. Many DI frameworks provide a means of doing interception and use more sophisticated ways of accomplishing it such as through the use of reflection.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
scroll top