2

In my UI project, I have few screens that share the same header style only the text is specific to the screens. What will be a good way to implement this?

  1. Have the super class create all the header component and open the components to the sub class, the sub class will access to component's setText method to update the text?

or

  1. Have abstract method in super class to create the components, sub class will implement these methods to create the component.

Hope it make sense..

2
  • have abstract method. This way you can never forget to change the text since you will have to override it.. and in future it will be easier to find where the header gets changed.. Just my opinion though Commented Jun 6, 2012 at 16:32
  • 1
    Only the text changes from screen to screen. In other words: The implementation depends on the particular String containing that text. Esteban Araya's answer expresses this dependency through its constructor. Commented Jun 6, 2012 at 16:43

8 Answers 8

3

Do you really need an abstract class?

public class UIScreen {
     public UIScreen(String headerText) {
         //set everything up here w/ the right text
     }
}

// elsewhere ....

UIScreen mainScreen = new UIScreen("Main Screen");
Sign up to request clarification or add additional context in comments.

Comments

0

Create the abstract class with the generic header method, and define one method for all the subclasses to implement would be best.

Comments

0

Depends on whether you want the super class able to be instantiated or not. It's like having an Animal super class and Dog and Cat subclasses. Do you want people to be able to create a generic Animal?

If so, it should be a normal class. If not, it should be declared abstract.

So ultimately the question is: Is there a default behavior? If not, then make it abstract.

Comments

0

The second option is more viable.

Remember: Single Responsibility Principle

Both options work but the second one will go a longer way to reduce coupling in your code.

Comments

0

When the behaviour Keeps changing...encapsulate it in either abstact class or interface.

Its better to have an

 Abstract class, with the non-abstract method to create the Header, 
and an Abstract method to create the text

..In the sub class u can create the text of your choice by implementing the abstract method.

1 Comment

then I will need access to the components , to set the text in the component (Label), so why not just open the components to sub class (but in this way subclass is not forced to change the text)
0

"Have abstract method in super class to create the components, sub class will implement these methods to create the component."

In my opinion this solution is easier to maintain.

Comments

0

You can change your option 1 slightly

public abstract class SuperUI {

   private HeaderComponenet headerComponent;//have getter setter

}

public class BaseUI extends SuperUI{
    private void changeHeaderComponent(){
       headerComponent.setText("Changed");
    }
}

public class HeaderComponent extends JComponent{
   public HeaderComponent(){
      //create the default header here
   }
}

In this case if the default header component has to be changed you don't have to change your SuperUI, as you have separated the header from SuperUI, you can do the same for footer component if need be.

Comments

0

My question is does the subclasses have any useful functionality? If not, why wouldn't you just have the one concrete class and pass it some sort of data container to populate the fields?

If there's no behavioral differences, you'd be much better served just passing data and/or collaborators into either the constructor, or via property setters.

1 Comment

all the subclasses will have separate behavior except the header style.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.