如何才能code在应用程序项目库项目调用code?项目、应用程序、code

2023-09-08 08:38:42 作者:老者与歌

在努力减少重复劳动,我有我的应用程序的工作空间分割成3个项目:

In an effort to reduce duplication, I have my app's workspace split into 3 projects:

主(A库项目,所有的共同code生活)免费(为了使免费版)付费(为了让付费版本)

行为的免费版和付费版有时必须有所不同。我怎样才能调入,从库项目的最终项目?

Behavior in the free version and the paid version must differ sometimes. How can I "call into" the final projects from the library project?

下面是一些示例psuedo- code来说明我的问题:

Here is some sample psuedo-code to illustrate my question:

private void makeADecision() {
    if (AppCode.isPaid()) {
        // do one thing
    } else {
        // do something else
    }
}

在免费的项目:

public class AppCode {
    public static bool isPaid() {
        return false;
    }
}

在付费项目:

public class AppCode {
    public static bool isPaid() {
        return true;
    }
}

这基本上是我都试过了,但它不会编译,因为主要的项目不知道在App code类。

That is basically what I have tried, but it won't compile because the Main project doesn't know about the AppCode class.

请记住,这仅仅是一个例子,所以尽量不要把重点放在应用程序如何能够知道它是付费版本与否。 :)到目前为止,我已经找到了最好的解决办法是把一个字符串中的所有三个项目的资源,然后根据其价值决定,但我不喜欢这种方法。除了是丑,我会preFER保持功能,它属于。这样我可以从prevent实才功能被编译成免费版本在所有我能避免包括任何自由只有$ C $在付费版本℃。

Bear in mind that this is only an example, so try not to focus on how an app can tell if it is the paid version or not. :) So far the best solution I have found is to put a string in the resources of all three projects and then make a decision based on its value but I don't like that method. Besides being ugly, I would prefer to keep functionality where it belongs. That way I can prevent "paid-only" functionality from being compiled into the free version at all and I can avoid having to include any "free-only" code in the paid version.

推荐答案

第1步:定义库中的一个接口,所以它是提供给所有三方,其方法是什么业务,你希望库上执行应用

Step #1: Define an interface in the library, so it is available to all three parties, whose methods are whatever operations you want the library to perform on the app

第2步:让应用程序通过一些库提供的API提供接口的实现图书馆

Step #2: Have the app supply an implementation of the interface to the library via some library-supplied API

第三步:有提供接口的库调用方法需要

Step #3: Have the library call methods on the supplied interface as needed