Abstraction Layer (Java)

I am currently working on a project that involves creating an abstraction layer. The aim of the project is to support several implementations of server software in case I may need to switch to it. The list of functions to be abstracted is quite long, so I want to look at a rather painless way to do this.

Other applications will be able to interact with my project and make calls, which ultimately come down to transferring to the server that I use.

This is the problem. I have little experience in this area, and I'm really not sure how to make it a death sandwich. Here's a chain of what it should look like (and what I'm trying to accomplish).

/*
Software that is dependent on mine
    |
Public API layer (called by other software)
    |
Abstraction between API and my own internal code (this is the issue)
    |
Internal code (this gets replaced per-implementation, as in, each implementation needs its own layer of this, so it a different package of entirely different classes for each implementation)
    |
The software I'm actually using to write this (which is called by the internal code)
*/

(, , ) - , .

. ,

public void someMethod() {
    if(Implementation.getCurrentImplementation() == Implementation.TYPE1) {
        // whatever we need to do for this specific implementation
    else {
        throw new NotImplementedException();
    }
}

( , , /, , , , if ) .

, . , , . , , ?

+4
1

?

, , api .

API , . , API, .


, IS, DI Ioc, ... , .

  • API (, )
  • ()
  • wrapper ( IoC, impl)

API:

// my-api.jar
public interface MyAPI {
    String doSomething();
}

public interface MyAPIFactory {
    MyAPI getImplementationOfMyAPI();
}

// red-my-api.jar
public class RedMyAPI implements MyAPI {
    public String doSomething() {
        return "red";
    }
}

// green-my-api.jar
public class GreenMyAPI implements MyAPI {
    public String doSomething() {
        return "green";
    }
}

// black-my-api.jar
public class BlackMyAPI implements MyAPI {
    public String doSomething() {
        return "black";
    }
}

. factory .

// wrapper-my-api.jar
public class NotFunnyMyAPIFactory implements MyAPIFactory {
    private Config config;

    public MyAPI getImplementationOfMyAPI() {
        if (config.implType == GREEN) {
            return new GreenMyAPI();
        } else if (config.implType == BLACK) {
            return new BlackMyAPI();                
        } else if (config.implType == RED) {
            return new RedMyAPI();                
        } else { 
           // throw...
        }
    }
}

public class ReflectionMyAPIFactory implements MyAPIFactory {
    private Properties prop;

    public MyAPI getImplementationOfMyAPI() {
        return (MyAPI) Class.forName(prop.get('myApi.implementation.className'))
    }
}

// other possible strategies

factory . ( ... ), .

, .

Spring, , (Spring DI). Spring, ( ).

  • my-api.jar (, , ).
  • my-api.jar .
  • - my-api.jar jap-.

, , , factory, , , , . , api.

+2

Source: https://habr.com/ru/post/1648835/


All Articles