Qt plug-depth understanding of plug-in system

Original: https://blog.csdn.net/liang19890820/article/details/78134253

Brief

To develop a successful system, plug-in is the only way.

  • For children, the wood plastic building blocks, these blocks can spell small house animals.
  • For construction workers, building rubble is zero, with these little building blocks to build high-rise buildings.
  • For programmers, the building blocks can be seen as a plug-in (a little more advanced), with these small building blocks can build large-scale systems.

Many people think of plug-ins is very complex, kept it shut. In fact, using the angle of the frame of view, it is still pretty simple. The most difficult, in fact, is not the frame itself, but rather change the existing development model. There is a habit called "rigid" ah, yes. . . For program ape / Yuan, it is in such a state, and coding machines What is the difference?

| Copyright: a go, two or three miles, shall not be reproduced without the bloggers allowed.

Why introduce plug-in

In the software development process, there is a very challenging task: to build to meet future customer needs.

Especially software into the expansion / maintenance phase, even more so. However, at this stage, it will always be a lot worse situations:

  • Software design is unreasonable
  • No corresponding documents
  • Team members leave (or transferred to other project team)
  • ……

At this time, if a new developer to participate in the product improvement, then congratulations! Not only feel particularly difficult when the extensions, but there is also the risk of breaking existing functionality. Of course, if you can find the start-up team members to help, I will be very happy. . .

In addition, to add new functionality to existing product development costs are very high. Because the entire system to be tested again, and released as another version.

To solve these problems, plug-ins appeared!

What is a plug-in

Excerpt from Wikipedia:

Plugin (Plugin) is a computer program, and by the application (for example: web browser, email client) interaction to increase the number of applications for specific functions required. The most common are games, web browser and media player plug-ins.

Plug-oriented mission

Plug-in is usually deployed as dynamic libraries, dynamic libraries so that plug-in has a number of advantages:

  • Hot Swap (reload the new implementation, without shutting down the system)
  • The source code separate from the application
  • Facilitate internal developers to add functionality
  • It allows third-party developers to extend applications safely (additional functionality without modifying the core system)

Sometimes, however, a static library is the best choice for plug-in:

  • For some systems (many embedded systems) do not support dynamic library
  • Due to security issues, are not allowed to load external code
  • Core systems need to pre-load some plug-ins, it is statically linked into the executable program (to prevent users from accidentally deleting)

Of course, a good plug-in system should support both dynamic plug-ins, plug-ins and support static. This way, you can deploy the same set of plug-in system in different environments with different constraints in.

Plug-oriented principles

Principles: programming to interfaces, rather than program-oriented implementation.

We are on the plug, a plug-based system interface, which is the basic concept: the system can load plug-ins, but it does not know anything, and communicates with them via a set of well-defined interfaces and protocols.

In the use of plug-in extensions Qt application , we can see that:

class IPerson
{
public:
    virtual ~IPerson() {}
    virtual QString name() = 0;  // 人有名字
    virtual void eat() = 0;  // 人需要吃东西
    virtual void sleep() = 0;  // 人需要睡觉
    virtual void doSomething() = 0;  // 人还需要干其他事
};

Plug goal is to achieve IPerson, the goal is to call IPerson service layer, business layer IPerson do not know exactly how to achieve, and implementers need not be concerned about is how to call the business layer.

Elements plugin framework

To achieve a plug-in framework, consider the following elements:

  • How to register the plug

  • How to call plugin

  • How to test plug-in
    framework to support automated testing: including unit testing, integration testing.

  • Life cycle management plug-in
    life cycle plug-controlled by the plug-in framework, need to consider the following questions:

  • How lifecycle plug-in conversion?
  • Once the class whether a shift in the life cycle of plug-in, refer to this plug-in can be notified.
  • Management and maintenance of plug-ins
    for plug-in framework, which is a basic function. mainly includes:

    • Provide a name, version, status information such as plug-ins, and you can get a list of plug-ins, plug-ins and other process log records.
    • Provides a plug load, start, stop, uninstall capabilities.
  • Assembled plug (additional evaluation elements)
    assembled plug-in means that multiple plug-ins can be assembled into a flexible chain, then the chain of calls.

  • Error handling plug
    when the plug when an error occurs during processing, the best result is to call the plug-in stops and records related logs, additional plug-ins to make this case an error correction process ( note: not affect the plug-in framework and other normal operation of the plug-in).

  • Guess you like

    Origin blog.csdn.net/a844651990/article/details/92786894