IOC (5) in Webwork (End)

zhaozj2021-02-16  55

5, example

As a summary, look at an example. Suppose you want to create a measured scale; these ticks are available on the Earth, Venus and Mars. Here is a problem: gravity on each planet is different. This must be flexible to handle this issue in order to ensure that they can get the right weight. Here is an integral part of the IOC:

l Components.xml (IOC profile)

l scale.java (interface of all components)

l Scaleaware.java (Enabler interface)

l MarscaleImpl.java (component)

l VenusScaleImpl.java (components)

l EarthscaleImpl.java (component)

l ScaleAction.java (Action class)

Below is an example of MarsScaleImpl registration in Components.xml:

Application

com.flyingButtress.scale.MarsscaleImpl

com.flyingbuttress.scale.scaleaware

Below is the code of the Action class, which implements the Scaleaware interface:

Public Class ScaleAction Implements Action, Scaleaware

{

Private scale scale;

Public void setscale (Scale Scale)

{

THIS.SCALE = Scale;

}

Public String Execute () THROWS EXCEPTION

{

System.out.println ("The Weight of You IS:" Scale.GetWeight ());

RETURN SUCCESS;

}

}

Now, for the scale sold on Mars, as long as it will set to MarsScaleImpl in Components.XML; for the scale for sale on the Earth, as long as is set to EarthScaleImpl; for sale on Venus The scale, as long as is set to VenusScaleImpl.

转载请注明原文地址:https://www.9cbs.com/read-17537.html

New Post(0)