Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #14938

Re: How is this "pattern" called?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news.netfront.net!not-for-mail
From Wanja Gayk <brixomatic@yahoo.com>
Newsgroups comp.lang.java.programmer
Subject Re: How is this "pattern" called?
Date Wed, 30 May 2012 14:33:04 +0200
Organization Netfront http://www.netfront.net/
Lines 140
Message-ID <MPG.2a3031c48bbfb9f989705@202.177.16.121> (permalink)
References <pattern-20120518104439@ram.dialup.fu-berlin.de> <ydnlikpbhon.fsf@shell.xmission.com> <lp2dr7da6p0jslnua32ldd4uki1036ibfs@4ax.com> <jp67dc$fe8$1@dont-email.me> <rhedr7ti7idd7ratbaludl5m14jo60p04l@4ax.com> <z_CdncDz595QXSvSnZ2dnUVZ_tOdnZ2d@earthlink.com> <54jdr7164mceis033e8f7amqb3qhjdfv9r@4ax.com> <4fb8590f$0$295$14726298@news.sunsite.dk> <96ejr7hcmgv68n2itnip55edo0cg97vj8a@4ax.com>
NNTP-Posting-Host 77.8.229.58
Mime-Version 1.0
Content-Type text/plain; charset="iso-8859-15"
Content-Transfer-Encoding 8bit
X-Trace adenine.netfront.net 1338381257 76126 77.8.229.58 (30 May 2012 12:34:17 GMT)
X-Complaints-To news@netfront.net
NNTP-Posting-Date Wed, 30 May 2012 12:34:17 +0000 (UTC)
User-Agent MicroPlanet-Gravity/3.0.4
Xref csiph.com comp.lang.java.programmer:14938

Show key headers only | View raw


In article <96ejr7hcmgv68n2itnip55edo0cg97vj8a@4ax.com>, genew@ocis.net 
says...
> 
> On Sat, 19 May 2012 22:38:04 -0400, Arne Vajhøj <arne@vajhoej.dk>
> wrote:
> 
> >On 5/18/2012 6:29 PM, Gene Wirchenko wrote:
> >>       With the amount of noise over patterns though, you would think
> >> that many people need the patterns.  For me, supporting an in-house
> >> application, there is no or little need.
> >
> >Or you have not realized the need.
> 
>      Consider the possibility that the need might not be there at all.
> I need to do X.  I can do X simply with Y.  What need of patterns
> then?

Let me give you an example:

You copy and modify your sorting routine for each class you like to 
sort, everywhere you need it. Simple, easy, does it:

for (int i = 1; i < articles.size(); i++){
  int j = i;
  final Article actual= articles.get(i);
  while (j>0 && articles.get(j-1).getAmount() > actual.getAmount())){
   articles.set(j, articles.get(j-1));
   j--;
  }
  articles.set(j, actual);
 }

[..]

 for (int i = 1; i < articles.size(); i++){
  int j = i;
  final Article actual= articles.get(i);
  while (j>0 && articles.get(j-1).getPrice() > actual.getPrice())){
   articles.set(j, articles.get(j-1));
   j--;
  }
  articles.set(j, actual);
 }

It works, it is straightforward, so what?

If you use the strategy pattern and instead of copying and modifying the 
sorting routine, just have one sorting method in a utility class, that 
you need to call, provide it with a list and a comparison strategy 
(Comparator):

class SortUtil{
 public static <T> sort(final List<T> items, 
                        final Comparator<? super T> strategy){
  for (int i = 1; i < items.size(); i++){
   int j = i;
   final T actual= articles.get(i);
   while (j>0 && strategy.compare(items.get(j-1), actual) > 0){
    items.set(j, items.get(j-1));
    j--;
   }
   items.set(j, actual);
  }
 }
}

You then define your comparison strategy and put it somewhere where it's 
easy and intuive to find:

class Article{

 public static Comparator<Article> COMPARE_BY_AMOUNT
     = new Comparator<Article>(){
  public int compare(Article a, Article b){
   return a.getAmount() - b.getAmount(); //I know it stinks of overflow!
  }
 };

 [..]

  public static Comparator<Article> COMPARE_BY_PRICE
     = new Comparator<Article>(){
  public int compare(Article a, Article b){
   return (int)(a.getPrice() - b.getPrice()); //I know it stinks hard!

  }
 };
}

That might look complicated first, but there is a huge benefit:
Wherever you sort articles, the result is quite readable:

SortUtil.sort(articles, Article.COMPARE_BY_AMOUNT);
SortUtil.sort(articles, Article.COMPARE_BY_PRICE);

Yes, you can also have that if you have a lot of copied methods like 
this:
sortArticlesByAmountAsc(articles);
sortArticlesByPriceAsc(articles);

But the huge difference is: If you find an error in your sorting 
routine, or if you want to change your sorting routine from insertin 
sort to quicksort, you only need to fix it in one place and every code 
that ises it will be fixed and sped up immediately.

You don't need to hunt down every copy that you have made (you will 
forget one for sure) and apply the same change to all of them (you will 
make a mistake somewhere). And since code evolves the fix might look 
different everywhere, so searching for the code you need to change is 
doomed to miss some occurrences.

Also you can easily re-use the comparison strategy, because is not 
buried under a load of sorting loopcode, but seperate from it. You can 
use it to compare two articles:

if(Article.COMPARE_BY_PRICE.compare(a1, a2)){
 highlightError(articleChoiceA);
}

Now imagine your code review shows that the code stinks as the price is 
of type "double", and you need to change that for "BigDecimal", then you 
can fix the Comparator once and every part of your code that uses it  
instantly works - so you have saved yourself a lot of time.
You did not need to hund down every sort routine you have copied and 
modified, not even search for the compiler errors in simple if-clauses 
where you had a < > operator first.

And since you know the Java-API, you also know that Collections.sort() 
was built the same way. For a good reason.

Kind regards,
-Wanja-

-- 
..Alesi's problem was that the back of the car was jumping up and down 
dangerously - and I can assure you from having been teammate to 
Jean Alesi and knowing what kind of cars that he can pull up with, 
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]

--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: How is this "pattern" called? Jim Janney <jjanney@shell.xmission.com> - 2012-05-18 10:03 -0600
  Re: How is this "pattern" called? Gene Wirchenko <genew@ocis.net> - 2012-05-18 10:50 -0700
    Re: How is this "pattern" called? markspace <-@.> - 2012-05-18 12:20 -0700
      Re: How is this "pattern" called? Gene Wirchenko <genew@ocis.net> - 2012-05-18 14:13 -0700
        Re: How is this "pattern" called? Patricia Shanahan <pats@acm.org> - 2012-05-18 14:35 -0700
          Re: How is this "pattern" called? Gene Wirchenko <genew@ocis.net> - 2012-05-18 15:29 -0700
            Re: How is this "pattern" called? Arne Vajhøj <arne@vajhoej.dk> - 2012-05-19 22:38 -0400
              Re: How is this "pattern" called? Lew <noone@lewscanon.com> - 2012-05-20 11:34 -0700
                Re: How is this "pattern" called? Arne Vajhøj <arne@vajhoej.dk> - 2012-05-20 14:59 -0400
                Re: How is this "pattern" called? Patricia Shanahan <pats@acm.org> - 2012-05-20 12:03 -0700
                Re: How is this "pattern" called? markspace <-@.> - 2012-05-20 13:19 -0700
                Re: How is this "pattern" called? Wanja Gayk <brixomatic@yahoo.com> - 2012-05-30 14:32 +0200
                Re: How is this "pattern" called? Lew <noone@lewscanon.com> - 2012-06-02 09:25 -0700
              Re: How is this "pattern" called? Gene Wirchenko <genew@ocis.net> - 2012-05-20 20:40 -0700
                Re: How is this "pattern" called? Wanja Gayk <brixomatic@yahoo.com> - 2012-05-30 14:33 +0200
          Re: How is this "pattern" called? Wanja Gayk <brixomatic@yahoo.com> - 2012-05-30 14:32 +0200
        Re: How is this "pattern" called? markspace <-@.> - 2012-05-18 15:28 -0700
        Re: How is this "pattern" called? Arne Vajhøj <arne@vajhoej.dk> - 2012-05-19 22:37 -0400
          Re: How is this "pattern" called? Gene Wirchenko <genew@ocis.net> - 2012-05-20 20:43 -0700
            Re: How is this "pattern" called? Lew <noone@lewscanon.com> - 2012-05-21 00:09 -0700
              Re: How is this "pattern" called? Gene Wirchenko <genew@ocis.net> - 2012-05-21 10:04 -0700
            Re: How is this "pattern" called? Wanja Gayk <brixomatic@yahoo.com> - 2012-05-30 23:58 +0200
    Re: How is this "pattern" called? Arne Vajhøj <arne@vajhoej.dk> - 2012-05-19 22:33 -0400
      Re: How is this "pattern" called? Gene Wirchenko <genew@ocis.net> - 2012-05-20 20:44 -0700
        Re: How is this "pattern" called? Lew <noone@lewscanon.com> - 2012-05-21 00:11 -0700

csiph-web