简体   繁体   English

如何从库中调用主程序中的类?

[英]How can I call a class in the main program from a library?

I have multiple programs that I need to write which share common functionality/resources. 我有多个需要编写的程序,它们共享共同的功能/资源。 I am looking to see if I can put a substantial part of these programs into a library. 我正在寻找是否可以将这些程序的大部分内容放入库中。 The scenario is as below: 场景如下:

ProgA, ProgB and ProgC are the programs using libX. ProgA,ProgB和ProgC是使用libX的程序。

libX has a class XYZ which will instantiate a class ABC. libX有一个XYZ类,它将实例化一个ABC类。

ProgA, ProgB and ProgC all have different implementations of class ABC. ProgA,ProgB和ProgC都具有ABC类的不同实现。

How do I structure my program to make this happen? 如何构造程序以实现这一目标? I have tried to do research, but was unable to find an answer. 我曾尝试进行研究,但找不到答案。 Thanks in advance... 提前致谢...

You may declare a public interface ABCInterface in your library which contains all methods neccessary for the operation of your library. 您可以在库中声明一个公共接口ABCInterface ,其中包含运行库所需的所有方法。 Your different programs will then declare their classes ABC implementing this interface and pass the reference to your library's methods. 然后,您的不同程序将声明它们的类ABC实现此接口,并将引用传递给库的方法。

In case you need to instanciate the class (once or more often) inside your library you can pass reference to a factory instead of the class instance itself. 如果需要实例化库中的类(一次或多次),则可以将引用传递给工厂,而不是类实例本身。 The following code may give you an idea how to implement such functionality. 以下代码可以使您了解如何实现此类功能。

// Library code
interface ABCInterface {
    public int getValue();
}

interface ABCFactory {
    public ABCInterface createInstance();
}

class Library {
    public static void doWork(ABCFactory factory) {
        ABCInterface obj = factory.createInstance();
        System.out.println(obj.getValue());
    }
}


// Program code
class Program {
    class ABCImplementation implements ABCInterface {
        @Override
        public int getValue() {
            return 42;
        }
    }

    public void useLibrary() {
        Library.doWork(new ABCFactory() {
            @Override
            public ABCInterface createInstance() {
                return new ABCImplementation();
            }
        });
    }
}

it seems to mee like you need to create n inversion of control . 看来您需要创建n 个控制反转
In this case, you need libX to have a pointer to an interface ABC that has all the functionality you want to have libX use, and then, progA, progB and progC will have to implement this interface. 在这种情况下,您需要libX有一个指向接口ABC的指针,该接口具有您想让libX使用的所有功能,然后,progA,progB和progC必须实现此接口。

as you start your program, you need to go to the shared library and inject the running program into the library with a known method such as: 在启动程序时,需要转到共享库,然后使用已知方法将正在运行的程序注入到库中,例如:

setProgram(IMyInterface interface) { 
    this.interface = interface; 
}

this way, when you want to run your known response actions you run the this.interface.doSomething() and that 's it. 这样,当您要运行已知的响应操作时,可以运行this.interface.doSomething()就是这样。

You'll simply have a basic library interface ABC which provides the interface which allows your XYZ class to interact with the classes A , B and C which all implement interface ABC . 您将仅拥有一个基本的库interface ABC ,该接口提供了允许XYZ类与均实现interface ABC的类ABC进行交互的interface ABC

public interface ABC {
    public void method1();
    public void method2();
}

Your library class then just needs a reference to an object which implements interface ABC . 然后,您的库类仅需要引用实现interface ABC的对象。

public class XYZ {
    private ABC abc;
    public XYZ(ABC abc) { this.abc = abc; }
}

And then your A , B and C implementations either implement ABC or create objects which implement ABC on-the-fly. 然后,您的ABC实现要么实现ABC要么创建动态实现ABC的对象。

To me, it sounds a lot like a callback scheme. 对我来说,这听起来很像是回调方案。 Yor library needs a standard way to communicate results to the specific implementation. Yor库需要一种将结果传达给特定实现的标准方法。 ABC is basically an event handler interface. ABC基本上是一个事件处理程序接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM