Results 1 to 4 of 4
  1. #1
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default A Question on Composition

    Hello everyone, I've been fighting over a little GUI composition program. I know I've had a previous post similar to the contents of this one but I believe my question will be distinct enough to merit another post.
    My idea is to be able to make template GUIs whereby a client can just provide a listener to attach to various components on the GUI. I'm quickly realizing their are two dependencies that make it impossible to separate scope of the component from both where the component will be added and the listener that will dictate how the component will respond. That is the 1) dependency of component to both the other visual components (such as a JTextField being added to a JFrame) and the 2) dependency of the component to the listener that will make the component respond (actionListener used to process the JTextField when a button is pressed).

    I have taken the advice of choosing composition over inheritance as I have seen many people warn over multiple posts (including mine).

    So my question is of the two approaches I have currently tried to use which is "cleaner", "more correct", and most importantly provides "optimal reusablility".

    In my first approach:
    I have an interface Problem that implements ActionListener. This interface has a calculate method. The idea is that the class that implements this interface sets the component (JTextField in this case) by using the class's calculate method in the actionListener.
    My other class is the template GUI in this case BasicTextIOPanelFactory. This class builds a final GUI JPanel to add to a JFrame.
    In this approach the client makes the component that is interdependent, the JTextField. It attachs JTextField to the ActionListener implementing Problem. It then passes the JTextField and ActionListener to BasicTextIOPanelFactory so that that JTextField can be added to the GUI and the ActionListener can be added to the JButton in the BasicTextIOPanelFactory.

    Second approach:
    I have the same classes listed above but now I have placed the component (the JTextField) in the ActionListener implementing Problem. I also have added the method public Component getComponent() to the interface. Now I only have to pass the interface to the BasicTextIOPanelFactory. My only fear in this approach is that the BasicTextIOPanelFactory cannot be secured to receive a specific component type to add. It just adds whatever getComponent returns. I have to keep this method generic though so that future templates can return different types of components.

    I know this sounds extremely ambiguous but any help would be greatly appreciated. Once again my goal is for cleaner code that has optimal reuse. I will post my code next in case none of this made sense.

  2. #2
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: A Question on Composition

    Here is my code for my second approach, this is the one currently saved:
    Java Code:
    public class Main
    {
        public static void main(String[] args)
        {
            makeGUI();
        }
        public static JPanel makeIOPanel()
        {
            BasicTextIOPanelFactory ioFactory = new BasicTextIOPanelFactory();
            return ioFactory.CreatePanel("Please enter text here:", new RevStringAction());
        }
        
        public static void makeGUI()
        {
            final JFrame frame = new JFrame("RevString");
            frame.add(makeIOPanel());
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            SystemTrayHelper.trayify(frame);
            frame.setVisible(true);
        }
    }
    
    public class BasicTextIOPanelFactory
    {
        private JPanel ioPanel;
        private JPanel centerPanel;
        private JPanel southPanel;
        private JButton submit;
        private JLabel promptLabel;
    
         public JPanel CreatePanel(String prompt, Problem aProblem)
        {
            ioPanel = new JPanel();
            ioPanel.setLayout(new BorderLayout());
            
            promptLabel = new JLabel(prompt, SwingConstants.RIGHT);
            centerPanel = new JPanel();
            centerPanel.add(promptLabel);
            
            centerPanel.add(aProblem.getComponent());
            
            ioPanel.add(centerPanel, BorderLayout.CENTER);
            
            submit = new JButton("Submit");
            submit.addActionListener(aProblem);
            southPanel = new JPanel();
            southPanel.add(submit);
            
            ioPanel.add(southPanel, BorderLayout.SOUTH);
            
            return ioPanel;
        }
    }
    
    public class RevStringAction implements Problem<String>
    {
        JTextField comp;
        /**
         * 
         * @param comp Provides the component that will be updated by this listener.
         */
        public RevStringAction()
        {
            comp = new JTextField(new JTextFieldLimit(20), "", 12);
        }
        
        public Component getComponent()
        {
            return comp;
        }
        
        public void actionPerformed(ActionEvent event)
        {
            String inputBuffer = comp.getText().trim();
            comp.setText(calculate(inputBuffer));
        }
        
        public String calculate(String input)
        {
            StringBuilder buff = new StringBuilder(input);
            String reved = buff.reverse().toString();
            return reved;
        }
    }
    I didn't bother to post Problem as I described the methods used above nor did I post JTextFieldLimit as its just a PlainDocument to restrict JTextField input.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: A Question on Composition

    I've read this three times and I'm still not sure what you're trying to do. Let me take a shot:

    You're creating a framework for generating UIs. The user of this framework will supply Problem objects that will perform some calculation and update a component of your UI. Am I right so far?

    What I don't understand is why these objects implement ActionListener, i.e., why they modify the UI themselves. Or for that matter, why they contain any UI code at all. It seems like these objects are controllers in the MVC pattern.

    So, I would remove everything from your Problem interface except the calculate method. Your GUI framework would create the ActionListener that calls calculate(...) and updates the associated Component. Your createPanel(...) method could still have the same signature.
    Get in the habit of using standard Java naming conventions!

  4. #4
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: A Question on Composition

    WOW. I seriously went on a tangent in the wrong direction, its no wonder I'm so confused. I kept getting locked up with this idea that I needed the ActionListener to be a in a separate class and contain the logic. Now I fixed it by making ActionListener an inner class, changed RevStringAction to RevString, and RevString only extends Problem.

    Thank you for taking the time to read it, and more for doing it three times!

Similar Threads

  1. Composition and Inheritance
    By BeyondWorldsEnd in forum New To Java
    Replies: 3
    Last Post: 01-08-2012, 11:19 AM
  2. Composition vs inheritance
    By mindus in forum Advanced Java
    Replies: 3
    Last Post: 09-27-2010, 08:41 AM
  3. composition Vs Inheritance
    By mindus in forum New To Java
    Replies: 1
    Last Post: 09-26-2010, 08:53 PM
  4. I need some help with composition, i think
    By Nerijus in forum New To Java
    Replies: 2
    Last Post: 03-04-2010, 07:24 PM
  5. What is the Composition in Java
    By Santoshbk in forum Advanced Java
    Replies: 4
    Last Post: 04-10-2008, 12:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •