I think what you want is to define an interface for your API, in the interface you put the functionality, it's like a contract between who uses the interface and implements it:
interface miAPI
{
int hazAlgo();
int hazOtraCosa(String dato);
}
So the public part of your api only returns the interface, regardless of the object that implements it
public static class API
{
static miAPI getApi()
{
//aquí puedes meter la lógica que necesites o usar el patrón de creación que estimes
return new miImplementacion();
}
}
Use from outside would be for example:
int usarAPI()
{
API api = API.getApi();
return api.hazAlgo() + api.hazOtraCosa("hola");
}
You do not have access to the class that implements your API or your API part
What is missing is the api provider:
public static class API
{
static miAPI getApi()
{
//aquí puedes meter la lógica que necesites o usar el patrón de creación que estimes
return new miImplementacion();
}
}
and the private class that implements your API:
private static class miImplementacion implements miAPI
{
@Override
public int hazAlgo()
{
return 0;
}
@Override
public int hazOtraCosa(String dato)
{
return 0;
}
}
This is a very simple illustrative example.
I recommend that the interfaces keep them related to their responsibility and that they do only the part they know, for example if the interface is Alumno
, that it has only asistirAClase
and hacerExamen
, if you need corregirExamen
should go in other interface Profesor
I usually name all interfaces with an i in front, so I always know that they are interfaces: iAlumno, iProfesor.
I recommend that you read about design patterns: Design patterns
note: a class can implement several interfaces, following the example of student / teacher with another way to offer your api:
public interface iAlumno
{
String hacerExamen(String examen);
String asistirAClase(String clase);
}
public interface iProfesor
{
String crearExamen();
int corregirExamen();
}
public class ClaseAPI implements iAlumno, iProfesor
{
private iAlumno alumno;
private iProfesor profesor;
ClaseAPI()
{
alumno = new Alumno();
profesor = new Profesor();
}
@Override
public String hacerExamen(String examen)
{
return alumno.hacerExamen(examen);
}
@Override
public String asistirAClase(String clase)
{
return alumno.asistirAClase(clase);
}
@Override
public String crearExamen()
{
return profesor.crearExamen();
}
@Override
public int corregirExamen()
{
return profesor.corregirExamen();
}
}
private class Alumno implements iAlumno
{
@Override
public String hacerExamen(String examen)
{
return null;
}
@Override
public String asistirAClase(String clase)
{
return null;
}
}
private class Profesor implements iProfesor
{
@Override
public String crearExamen()
{
return null;
}
@Override
public int corregirExamen()
{
return 0;
}
}