Composite. Again!
From: Tsolak Petrosian (tsolakp_at_yahoo.com)
Date: 04/08/04
- Next message: Alan Gauld: "Re: The Failure of OOP"
- Previous message: Universe: "Re: The Failure of OOP"
- Next in thread: Daniel T.: "Re: Composite. Again!"
- Reply: Daniel T.: "Re: Composite. Again!"
- Reply: H. S. Lahman: "Re: Composite. Again!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 7 Apr 2004 16:11:43 -0700
I once opened similar topic, but didn't get satisfying answer.
I'll try again with following example.
Compoiste approach:
public interface View{
public void open();
public void close();
}
public class ConcreteView implements View{
private View[] subViews;
public void open(){
for (int i = 0; i < subViews.length; i++) subViews[i].open();
}
public void close(){
for (int i = 0; i < subViews.length; i++) subViews[i].close();
}
}
public class Window{
private View view = null
puiblic Window(View view){
this.view = view;
}
public void windowOpened(){
view.open();
}
public void windowClosed(){
view.close();
}
}
Non Composite Approach:
public interface View{
public void open();
public void close();
public View[] getSubViews();
}
public class ConcreteView implements View{
private View[] subViews;
public void open(){}
public void close(){}
public View[] getSubViews(){
return subViews;
}
}
public class Window{
private List views;
puiblic Window(View view){
loadViews(view);
}
public void windowOpened(){
while( views.hasNext() ) views.next().open();
}
public void windowClosed(){
while( views.hasNext() ) views.next().close();
}
private void loadViews(List views, View view){
views.add(view);
View[] sub_views = view.getSubViews();
for (int i = 0; i < subViews.length; i++) loadViews( views,
subViews[i] );
}
}
The composite approach looks nice but tries to fool the Window by
pretending to be a whole; on other hand the second approach seems to
make more sense by making the Window special container, which manages
pool of views and their "open" and "close" status.
In second example the view will be just a factory of other views and
wont discriminate subviews and be like a dictator by not exposing them
to outside of itself (like in real world once born we are not covered
by our parents but let to developed on our own).
Tsolak Petrosian
- Next message: Alan Gauld: "Re: The Failure of OOP"
- Previous message: Universe: "Re: The Failure of OOP"
- Next in thread: Daniel T.: "Re: Composite. Again!"
- Reply: Daniel T.: "Re: Composite. Again!"
- Reply: H. S. Lahman: "Re: Composite. Again!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|