Composite. Again!

From: Tsolak Petrosian (tsolakp_at_yahoo.com)
Date: 04/08/04


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



Relevant Pages

  • JFrame Resize Issues (Redux)
    ... However, when the LookAndFeel is set, Java's Window Manager ... public int OffSetX = 0; ... private Rectangle positRectangle; ... public void componentHidden{ ...
    (comp.lang.java.programmer)
  • Re: Change ToolBox Control Names in Code Behind when pulling them in?
    ... private _DTE applicationObject; ... public void OnConnection(object application, Extensibility.ext_ConnectMode ... /// Array of parameters that are host application specific. ... private void windowEvents_WindowActivated(Window GotFocus, Window LostFocus) ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: New Swing Window Not Drawn
    ... The other actions that need to happen while this window appears ... public void activate{ ... thread and Swing should be able to have time to render it? ... All interaction with Swing components should be done on the Event ...
    (comp.lang.java.programmer)
  • Re: problem in coding window
    ... i am new with java and am learning it from Schildt's complete reference, i try to convert an applet example there to a window program, it got compiled but result is not as expected it is only showing the last checkbox, plz help me with corrections or correct code import java.awt.*; ... public class CBGroup extends Frame ... Checkbox win98,winNT,solaris,mac; ... public void itemStateChanged ...
    (comp.lang.java.help)
  • Re: Swing - paintComponent not called
    ... public void actionPerformed{ ... you are launching a new window in a non-EDT thread, ... To create the window in the EDT--just move the ProgressFrame creation ...
    (comp.lang.java.programmer)