“if” statement vs OO Design - 2

Posted by hilal on Stack Overflow See other posts from Stack Overflow or by hilal
Published on 2010-12-21T16:37:41Z Indexed on 2010/12/25 10:54 UTC
Read the original article Hit count: 565

I encountered similar problem “if” statement vs OO Design - 1 but it is slightly different. Here is the problem that open the popup (different objects/popups) onValueChange of listbox

Popup1 p1; // different objects
Popup2 p2; // different objects
Popup3 p3;
...

listbox.add("p1");
listbox.add("p2");
listbox.add("p3");
...

listbox.addChangeHandler() {
    if(getSelectedItem().equals("p1")){
       p1 = new Popup1();
       p1.show();
    } else if() {...}
      ....
}

I don't want to write "if" that if p1 then p1 = new Popup1(); p1.center();

How I can handle this situation? Any design-pattern?

Here is my solution but it is so costly

map() {

    map.put("p1", new Popup1());
    map.put("p2", new Popup2());
    map.put("p3", new Popup3()); 
}

onValueChange() {
    map.get(selectedItem).show();
}

One drawback is initialization all the popups. but it is require only when valueChange

© Stack Overflow or respective owner

Related posts about java

Related posts about design-patterns