AlgoMaster Logo
AlgoMasterDesign Plugin Editoreasy

Design Plugin Editor

easy

Design the plugin system used by a PluginEditor class. Every plugin follows the same contract: it has a name and knows how to transform a piece of text. The editor can therefore run any installed plugin without knowing its concrete type or implementation.

The PluginEditor class is provided. Complete the shared plugin contract and the three concrete plugin types so the editor behaves as follows:

  • PluginEditor() creates an editor with no installed plugins.
  • boolean install(String name) installs a known plugin and returns true. If the name is unknown or the plugin is already installed, leave the editor unchanged and return false.
  • String[] installed() returns the installed plugin names in installation order.
  • int pluginCount() returns the number of installed plugins.
  • String run(String text) passes the text through every installed plugin in order. Each plugin receives the previous plugin's output, and the final transformed text is returned. With no plugins installed, return the original text unchanged.

Implement these plugins:

  • UpperPlugin reports the name "upper" and converts text to uppercase.
  • TrimPlugin reports the name "trim" and removes leading and trailing spaces.
  • ExclaimPlugin reports the name "exclaim" and appends !.

Installation order matters because transformations are chained. For example, running trim and then exclaim on " b " produces "b!", while running exclaim and then trim produces "b !".

Adding another plugin later may require registering it with the editor's factory, but it must not require changing run. Read the supplied class to see the exact interface, abstract class, or method names expected in your language.

Example 1:

Input:

Output:

Explanation: Two plugins are installed and run in installation order, the second receiving what the first produced.

Example 2:

Input:

Output:

Explanation: exclaim first produces ab!, then upper changes the letters to give AB!.

Constraints:
  • 1 <= text.length <= 60
  • name is a lowercase word.
  • At most 100 calls will be made across all methods.

How the design is graded

needs 7/10 to pass
  • Realization

    Full marks when a single plugin contract declares the transformation and the name, each plugin is a separate type implementing it, and the editor holds the contract rather than the concrete types. Lose points heavily when run contains an if or switch over plugin names, or when the editor stores names and transforms the text itself.

  • Chaining

    Full marks when run threads each plugin's output into the next in installation order, so a text passing through two plugins reflects both. Lose points when only the first or last plugin is applied, when they run in another order, or when each plugin receives the original text.

  • Structure and naming

    Full marks when an unknown plugin name is refused, installing the same plugin twice is refused, and the plugin names live on the plugins themselves. Lose points when a name appears both in the editor and inside its own type, or for printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new PluginEditor()null
install("trim")true
install("upper")true
run(" hi ")"HI"
installed()["trim","upper"]
pluginCount()2

Two plugins are installed and run in installation order, the second receiving what the first produced.

Run checks these cases. Submit also runs a larger hidden set.