<span id="Label3"></p><p><p>The command pattern is the underlying pattern of many other behavioral patterns.<br>The policy mode is a special case of the command pattern, and the policy mode and the template method pattern are the implementation of the algorithm substitution, but the substitution method is Different.<br>Let's talk about the three patterns below.</p></p>Command mode <blockquote> <blockquote> <p>Encapsulates a request as an object so that you can parameterize the customer with different requests, queue requests or log requests, and support undoable operations.</p> </blockquote> </blockquote><p><p>It is easy to pass (inject) an object in java, but it does not support the direct transitive behavior (that is, the transfer function or the delivery method), but only indirectly by passing (injecting) an object and then invoking its Behavior.<br>If you extract such behavior as a class, called a command class, its implementation is the command object:</p></p> <table> <tbody> <tr> <td class="gutter">123456789101112131415161718192021</td> <td class="code"><span class="keyword"><span class="keyword">public <span class="class"><span class="keyword">class <span class="title">Receiver {</span> </span></span> </span></span> <span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">action<span class="params">() {</span> </span> </span> </span></span></span>}}<span class="keyword"><span class="keyword">public <span class="class"><span class="keyword">Interface <span class="title">ICommand {</span> </span></span> </span></span> <span class="function"><span class="function"><span class="keyword">void <span class="title">Execute<span class="params">();</span> </span> </span></span></span>}<span class="keyword"><span class="keyword">public <span class="class"><span class="keyword">class <span class="title">Command <span class="keyword">implements <span class="title">ICommand {</span> </span> </span> </span></span> </span></span> <span class="comment"><span class="comment">//inject real objects into action</span></span> <span class="keyword"><span class="keyword">Private receiver receiver;</span></span> <span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">setreceiver<span class="params">(receiver receiver) {</span> </span> </span> </span></span></span> <span class="keyword"><span class="keyword">this.receiver = receiver;</span></span>}<span class="meta"><span class="meta">@Override</span></span> <span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">Execute<span class="params">() {</span> </span> </span> </span></span></span> <span class="comment"><span class="comment">//this can contain more logical code</span></span>Receiver.action (); }}</td> </tr> </tbody> </table><p><p>Call such a command object, I feel as if I call it directly, why take off the pants fart, superfluous?<br><br>congratulations, you asked for the order Pattern.<br>The command pattern separates the different behaviors and the information they need, so that the behavior itself (the behavior can be simple and Complex) becomes independent, parameterized or dynamically replaced, flexibility is greatly improved, we only need to focus on the implementation of the command object, and the command object can be completely shared.<br>When faced with a variety of command objects, the inevitable need to queue or management, such as the thread pool, etc., here do not Narrate.<br>In fact, the need for behavioral targeting is often a simple command encapsulation that requires no state, no queuing, no management, and we can simplify the command:</p></p> <table> <tbody> <tr> <td class="gutter">1234567</td> <td class="code"><span class="keyword">public <span class="class"><span class="keyword">class <span class="title">Command <span class="keyword">implements <span class="title">ICommand {</span> </span> </span> </span></span> </span> <span class="meta">@Override</span> public <span class="function"><span class="keyword"> <span class="keyword">void <span class="title">Execute<span class="params">(receiver receiver) {</span> </span> </span> </span></span> <span class="comment">//this can contain more logic code</span> receiver.action (); }}</td> </tr> </tbody> </table><p><p>Lambda expressions can be said to be an extreme implementation of such a simplified version, the UML class diagram of the command pattern is as Follows:<br></p></p>Policy mode <blockquote> <blockquote> <p>Define a series of algorithms, encapsulate them one by one, and make them interchangeable with each other. This mode allows the algorithm to be independent of the customers who use it.</p> </blockquote> </blockquote><p><p>First look at the UML class diagram of the Strategy mode:<br></p></p><p><p>There is no discovery and the above command mode UML class diagram has a high overlap, the difference is:<br>The command pattern emphasizes a encapsulation of the existing behavior, and the policy pattern emphasizes the substitution of a series of unknown algorithms that require an independent implementation.</p></p> <table> <tbody> <tr> <td class="gutter">12345678910111213141516171819202122232425262728</td> <td class="code"><span class="keyword"><span class="keyword">public <span class="class"><span class="keyword">Interface <span class="title">istrategy {</span> </span></span> </span></span> <span class="function"><span class="function"><span class="keyword">int <span class="title">doalgorithm<span class="params">(<span class="keyword">int ... array);</span> </span> </span> </span></span></span>}<span class="comment"><span class="comment">//return to first element</span></span><span class="keyword"><span class="keyword">public <span class="class"><span class="keyword">class <span class="title">Strategya <span class="keyword">implements <span class="title">istrategy {</span> </span> </span> </span></span> </span></span> <span class="meta"><span class="meta">@Override</span></span> <span class="function"><span class="function"><span class="keyword">public <span class="keyword">int <span class="title">doalgorithm<span class="params">(<span class="keyword">int ... array) {</span> </span> </span> </span> </span></span></span> <span class="keyword"><span class="keyword">return array[<span class="number">0];</span> </span></span>}}<span class="comment"><span class="comment">//return to the last element</span></span><span class="keyword"><span class="keyword">public <span class="class"><span class="keyword">class <span class="title">Strategyb <span class="keyword">implements <span class="title">istrategy {</span> </span> </span> </span></span> </span></span> <span class="meta"><span class="meta">@Override</span></span> <span class="function"><span class="function"><span class="keyword">public <span class="keyword">int <span class="title">doalgorithm<span class="params">(<span class="keyword">int ... array) {</span> </span> </span> </span> </span></span></span> <span class="keyword"><span class="keyword">return array[array.length- <span class="number">1];</span> </span></span>}}<span class="keyword"><span class="keyword">public <span class="class"><span class="keyword">class <span class="title">Context {</span> </span></span> </span></span> <span class="keyword"><span class="keyword">Private IStrategy strategy;</span></span> <span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">setstrategy<span class="params">(istrategy strategy) {</span> </span> </span> </span></span></span> <span class="keyword"><span class="keyword">this.strategy = strategy;</span></span>}<span class="function"><span class="function"><span class="keyword">public <span class="keyword">int <span class="title">getData<span class="params">(<span class="keyword">int ... array) {</span> </span> </span> </span> </span></span></span> <span class="comment"><span class="comment">//different Strategies will get different results</span></span> <span class="keyword"><span class="keyword">return strategy.doalgorithm (array);</span></span>}}</td> </tr> </tbody> </table>Template method Mode <blockquote> <blockquote> <p>Defines the skeleton of an algorithm in an operation, and delays some steps into Subclasses. Templatemethod allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.</p> </blockquote> </blockquote><p><p>Here the algorithm and the strategy of the pattern of algorithms are referred to the generic algorithm, can be easily understood as a concrete implementation.<br>The method of template method substitution algorithm is the most direct, is the inheritance coverage, one can understand. Of course the focus of the template approach is to define the structure of these algorithms:</p></p> <table> <tbody> <tr> <td class="gutter">1234567891011121314151617181920212223242526272829</td> <td class="code"><span class="keyword"><span class="keyword">Public <span class="keyword">Abstract <span class="class"><span class="keyword">class <span class="title">AbstractClass {</span> </span></span> </span> </span></span> <span class="comment"><span class="comment">//in This order to execute</span></span> <span class="function"><span class="function"><span class="keyword">private <span class="keyword">void <span class="title">Templatemethod<span class="params">() {</span> </span> </span> </span></span></span>PrimitiveOperation1 (); PrimitiveOperation2 (); PrimitiveOperation3 (); }<span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">PrimitiveOperation1<span class="params">() {</span> </span> </span> </span></span></span>}<span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">PrimitiveOperation2<span class="params">() {</span> </span> </span> </span></span></span>}<span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">PrimitiveOperation3<span class="params">() {</span> </span> </span> </span></span></span>}}<span class="keyword"><span class="keyword">public <span class="class"><span class="keyword">class <span class="title">Concreteclass <span class="keyword">extends <span class="title">abstractclass {</span> </span> </span> </span></span> </span></span> <span class="meta"><span class="meta">@Override</span></span> <span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">PrimitiveOperation1<span class="params">() {</span> </span> </span> </span></span></span> <span class="comment"><span class="comment">//concrete Class Code</span></span>}<span class="meta"><span class="meta">@Override</span></span> <span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">PrimitiveOperation2<span class="params">() {</span> </span> </span> </span></span></span> <span class="comment"><span class="comment">//concrete Class Code</span></span>}<span class="meta"><span class="meta">@Override</span></span> <span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">PrimitiveOperation3<span class="params">() {</span> </span> </span> </span></span></span> <span class="comment"><span class="comment">//concrete Class Code</span></span>}}</td> </tr> </tbody> </table><p><p>The ingenious place (if hard to say that there are ingenious Words) is that Templatemethod only need to hang "hooks" and defer the implementation of these hooks to subclasses, which is useful in some scenarios with fixed skeletons, such as Life Cycle.<br>The template method uses inheritance compared to the policy pattern using the combination substitution algorithm, and the UML class diagram is as Follows:<br></p></p>Summary<p><p>Three modes are well understood and are the basic mode of object-oriented Application.<br>These models are the most direct embodiment of inheritance and polymorphism, although very simple, but tell everyone a truth: although the object-oriented and profound, but original aim!</p></p><p><p>Command mode, policy mode, Template method mode for Android design mode</p></p></span>
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