Java OCA OCP Practice Question 384

Question

Given:

public class Main {
  private D d;/*from ww  w.  j  a v a  2s  . com*/
  public void m() {
    d.m();
  }
     public static Main getInstance() {
    return new Main();
  }
}
class D {
  public void m() {}
}

Which design pattern or principle is implemented?

  • A. DAO
  • B. Factory
  • C. IS-A
  • D. Object composition
  • E. Singleton


D is correct.

Note

The object composition principle of method forwarding is shown.

Option E is tricky, but incorrect.

Although getInstance() is a common name for a method in a singleton, the method doesn't return a static object.

While it does create an object, it isn't a factory either, since there is no superclass.




PreviousNext

Related