Kevin Greer's Stuff
[ start | index | login ]
start > comment-start > 2006-01-20 > 1-2

1-2 commented start/2006-01-20/1

Created by Jonathan Feinberg. Last edited by Jonathan Feinberg, 2 years and 233 days ago. Viewed 88 times. #2
[diff] [history] [edit] [rdf]
labels
attachments
I recently implemented this very pattern in a web application, in which I wanted to represent the current state (in the sense of REST) in such a way that I could say

state.getNextPage().getRest()

to get the URL which represents "the next page of results".

In order to permit construction of arbitrary, unknown future subclasses, I use the factory method.

// pseudo-code
class StateWithPage implements State {
  public State getNextPage() {
     return createState(this.page + 1);
  }

protected State createState(final int page) { return new StateWithPage(page); } }

class StateWithPageAndColor extends StateWithPage { protected State createState(final int page) { return new StateWithPageAndColor(page, this.color); } }

2 comments (by DaveHunter, Jonathan Feinberg) | post comment
peerbox.com | Copyright 2005-2006 Kevin G. R. Greer