Implement IOC (1) in Webwork

zhaozj2021-02-16  56

In many applications, the specified class requires the component object. IOC mode allows parent objects (in Webwork's ComponentManager instance) to the resource objects it requires with the Action object, not the object needs yourself to get the resource object.

There are two ways to implement IOC: instantiate and use the enabler interface.

l Institution: When an action object is instantiated, the resource object is passed as a constructor parameter.

l Use the Enabler interface: XWORK uses the Enablers to pass the component object. Action implements the enabler interface with a method called setComponent (ComponentObject R). Thus, when the object is instantiated, the resource object is passed to the object.

Why use IOC?

Using IOC means that you can develop components from the top-down way, do not need to create a registration class that you need to call, and go to yourself instance.

Traditionally achieve services, about the following steps:

l Write components (such as ExchangeRateService);

l Write customer class (XWork's Action class);

l Write the registration class (for example, registry) to keep the component object;

l Write code registration components (for example, registry.registerService (new myexchangerateaservice ()));

l Use registration class to get services in customer classes (such as ExchangeRateRvice Ers = registry.getexchangerateasemp ())

l Call components in customer classes (such as String Basecurrencycode = Ers.getBaseCurrency ())

Use IOC, including the following process:

l Write components (such as ExchangeRateService);

l Register component classes in XWork (for example, ComponentManager.Addenable, ExchangeRateaWare);

l Write a customer class that implements the Enabler interface (for example, an Action class that implements the ExchangeRateaWare interface;

l Call the components directly in the customer class (such as String Basecurrencycode = Ers.getBaseCurrency ())

Use IOC with some benefits:

l More Easy to test: You can use Enabler's way to pass analog objects to test your objects; rather than you must establish the entire container environment to get the required component objects (independent of the container environment);

l Components Description You: When instantiation components, you can easily decide what it needs, without having to find in your code or run errors;

l Use reflex mechanisms to easily find dependencies: this is beneficial to chart generation and runtime optimization;

l When you hold a single class, avoid the super-user-mega-factory mode when all components are applied.

l Adhere to the Demeter principle: Each class only contains the content it actually use, which encourages the small specific function class, which makes it clean; l Allows all context environments independent and clear mutual delivery: local threads in a web application Can work well, but not suitable for high asynchronous applications (such as messaging applications);

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

New Post(0)