Agile Software Development (4)---TEMPLATE METHOD & Strategy mode

Source: Internet
Author: User
Tags comparable

1.TEMPLATE METHOD

Generics, that is, the pattern, can be based on generics.

We tend to have some algorithms, such as sorting algorithms. Its algorithm part, I can put it in a base class, so the specific type of comparison can be placed in the subclass.

Look at the following bubble sorting algorithm:

 PackageCom.joyfulmath.agileexample.template.method;/** * @authorDeman.lu *@versionOn 2016-06-09 10:04*/ Public Abstract classBubblesorter {Private intOperations = 0; protected intLength = 0; protected intDosort () {Operations= 0; if(length<=1)            returnoperations;  for(intNexttolast = length-2;nexttolast>=0;nexttolast--)             for(intindex = 0;index<=nexttolast;index++)            {                if(Outoforder (index)) {swap (index); }            }        returnoperations; }    protected Abstract voidSwapintindex); protected Abstract BooleanOutoforder (intindex);}

First look at the sort of int:

 PackageCom.joyfulmath.agileexample.template.method;/** * @authorDeman.lu *@versionOn 2016-06-09 10:18*/ Public classIntbubblesorterextendsbubblesorter{Private int[] Array =NULL;  Public intSortint[] thearray) {Array=TheArray; Length=thearray.length; returnDosort (); } @Overrideprotected voidSwapintindex) {        inttemp =Array[index]; Array[index]= Array[index+1]; Array[index+1] =temp; } @Overrideprotected BooleanOutoforder (intindex) {        returnArray[index]>array[index+1]; }}

As long as the comparison and exchange of 2 interfaces can be achieved.

Looking at generic-based subclasses:

 PackageCom.joyfulmath.agileexample.template.method;/** * @authorDeman.lu *@versionOn 2016-06-09 10:23*/ Public classGenericbubblesorter<textendsComparable>extendsBubblesorter {Privatet[] Array =NULL;  Public intsort (t[] thearray) {array=TheArray; Length=thearray.length; returnDosort (); } @Overrideprotected voidSwapintindex) {T temp=Array[index]; Array[index]= Array[index+1]; Array[index+1] =temp; } @Overrideprotected BooleanOutoforder (intindex) {        returnArray[index].compareto (array[index+1]) >0; }}
 Public class Bubbledemo {    publicstaticvoid  action ()    {        new integer[]{                1,2,3,5,6,8,10,0,2,3        };        GenericbubblesorterNew genericbubblesorter<>();        Intbublesorter.sort (array);          for (int i=0;i<array.length;i++)        {            tracelog.i (array[i].tostring ());     }}}

This allows the bubble sort to be achieved.

The principle of agile development is not necessarily to use design patterns, to see the situation, to see the need. So here it can be said that this bubblesorter some redundant, direct genericbubblesorter use, and implementation of the sorting algorithm can be, depending on the situation.

But sometimes we want to isolate the sorting algorithm from the specific user, or I want to modify the sorting algorithm, but without modifying the other code, the coupling is reduced.

2.STRATEGY mode

Introduction to the policy model, you can see my previous blog: design mode 4---policy mode

Here we introduce another mode of bubbling sorting.

 Public classBubblesorter {Private intOperations = 0; protected intLength = 0; PrivateSorthandler Itssorthandle =NULL;  PublicBubblesorter (Sorthandler itssorthandle) { This. Itssorthandle =Itssorthandle; }     Public intsort (Object array) {Itssorthandle.setarray (array); Length=itssorthandle.length (); Operations= 0; if(Length <= 1)            returnoperations;  for(intNexttolast = length-2; Nexttolast >= 0; nexttolast--)             for(intindex = 0; Index <= nexttolast; index++) {                if(Itssorthandle.outoforder (index)) {Itssorthandle.swap (index); } Operations++; }        returnoperations; }}

The sorting algorithm is still placed in the bubblesorter, he does not know who to sort (sorthandler), so Bubblesorter & Sorthandler implementation class is decoupled.

 Public classGenericsorthandle<textendsComparable>ImplementsSorthandler {Privatet[] Array =NULL; @Override Public voidSwapintindex) {T temp=Array[index]; Array[index]= Array[index+1]; Array[index+1] =temp; } @Override Public BooleanOutoforder (intindex) {        returnArray[index].compareto (array[index+1]) >0; } @Override Public intLength () {returnArray.Length; } @Override Public voidSetArray (Object array) { This. Array =(t[]) array; }}

Here you can do 2 substitutions, one is the sorting algorithm, the other is the sort of material. This is the strategy mode,

The algorithm can be replaced, and the environment used by the algorithm is consistent.

 Public classBubbleDemo2 { Public Static voidaction () {integer[] array=Newinteger[]{1,2,3,5,6,8,10,0,2,3        }; Genericsorthandle<Integer> Intbublesorter =NewGenericsorthandle<>(); Bubblesorter Bubblesorter=NewBubblesorter (Intbublesorter);        Bubblesorter.sort (array);  for(inti=0;i<array.length;i++) {tracelog.i (array[i].tostring ()); }    }}

Or that sentence, the use of design patterns, depending on the circumstances, if the requirements, the environment changes, there may be no design patterns, to refactor code, the use of design patterns.

This is agile development that transforms the use of design patterns based on demand changes, including not using any pattern!

Reference:

"Agile Software Development" Robert c. Martin

Agile Software Development (4)---TEMPLATE METHOD & Strategy mode

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.