What is this technique called in Java?

Posted by ShaChris23 on Stack Overflow See other posts from Stack Overflow or by ShaChris23
Published on 2010-03-16T01:05:28Z Indexed on 2010/03/16 1:09 UTC
Read the original article Hit count: 377

Filed under:

I'm a C++ programmer, and I was reading this site when I came across the example below. What is this technique called in Java? How is it useful?

class Application {
...
  public void run() {
    View v = createView();
    v.display();
...
  protected View createView() {
    return new View();
  }
...
}    

class ApplicationTest extends TestCase {

  MockView mockView = new MockView();

  public void testApplication {
    Application a = new Application() {    <---
      protected View createView() {        <---
        return mockView;                   <--- whao, what is this?
      }                                    <---
    };                                     <---
    a.run();
    mockView.validate();
  }

  private class MockView extends View
  {
    boolean isDisplayed = false;

    public void display() {
      isDisplayed = true;
    }

    public void validate() {
      assertTrue(isDisplayed);
    }
  }
}

© Stack Overflow or respective owner

Related posts about java