Java design mode (vi) command (Command mode) and Tomcat extension

Source: Internet
Author: User

Basic Concept Command
  • Command mode is a high-cohesion pattern ( satisfying a single duty principle )
  • Concept : Encapsulates a request as an object, allowing you to parameterize the client with different requests, queue requests, or log request logs to provide undo and redo functions for the command .
  • Command patterns are frequently used in projects, and encapsulation and extensibility are well protected.
  • Division of roles in command mode :
    • Client: Create a command and decide which recipients
    • Command: Commands interface, defines an abstract method
    • ConcreteCommand: Specific commands that are responsible for invoking the relevant actions of the recipient
    • Invoker: Requester, responsible for invoking Command object execution request
    • receiver: recipient, responsible for specific implementation and execution of a request
  • The main function of the command pattern is to encapsulate the command , separating the responsibility for issuing the command (Client) from executing the command (Invoker) and also the division of functions.
  • Advantages of the Command mode:
    • decoupling between Classes
      There is no dependency between the caller and the receiver role, the caller only needs to invoke the (*invoke) command, without having to focus on the receiver (receiver)
    • Extensibility
      The command subclass can be easily extended because the coupling exists in the abstraction layer and does not affect the specific implementation of the command
    • Cooperative Nature
      • Can combine the responsibility chain pattern , realizes the command family analytic task
      • the problem of the expansion of command sub-class can be weakened by combining template method pattern
Example command

Like our Diao (SI) (nan), although clever enough to know that there is no probability of being seen by the goddess, but still willing to do the spare tire, the Goddess is obedient (at the very least, the goddess is very gentle when things ...), then the Goddess (Invoker) 's request is also an order, and often programmers use the same innocent programmers around them to meet the Goddess's demands.
The code is implemented as follows:

* Created by Liulin on --4-.*/interface receiver{voidCode ();} Interface command{voidExecute ();} Class DBA implements receiver{@Override     Public void Code() {System.out.println ("Database ready!"); }}class Javaer implements receiver{@Override     Public void Code() {System.out.println ("Background finish!"); }}class Designer implements receiver{@Override     Public void Code() {System.out.println ("Html done!!!"); }}class Webapprequirement implements command{PrivateReceiver A =NewDesigner ();PrivateReceiver B =NewJavaer ();PrivateReceiver C =NewDBA ();@Override     Public void Execute() {A.code ();        B.code ();        C.code (); System.out.println ("The WebApp done!"); }}class invoker{Privatecommand command; Public void Action(){ This. Command.Execute (); } Public void SetCommand(Command command) { This. command = command; }} Public  class commandtest {     Public Static void Main(String [] args) {Invoker Goddess =NewInvoker (); Goddess.setcommand (NewWebapprequirement ());    Goddess.action (); }}

The effect is as follows:

Application of command mode in Tomcat

As an application Server , Tomcat will no doubt receive many requests, and how to allocate and execute these requests is a necessary function, which is mainly implemented by Connector and Container components.

    • The roles in Tomcat correspond to the following:
      • Connector: Abstract invoker
      • HttpConnector: the specific invoker
      • httpprocessor: Command
      • Container: Abstract Receiver of command
      • containerbase: The specific receiver of command
      • Server Components : Client
    • The specific logical implementation is as follows:
      • Server first creates a command requestor HttpConnector object
      • Then create the command Httpprocessor object
      • The command object is then handed to the Containerbase container of the command receiver for processing.
      • The command was eventually executed by Tomcat's container.

Java design mode (vi) command (Command mode) and Tomcat extension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.