Java – override getpreferredsize() interrupt LSP

I always see suggestions to rewrite getpreferredsize () instead of setpreferredsize () on this website, as shown in these previous threads

> Use of overriding getPreferredSize() instead of using setPreferredSize() for fixed size Components > Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? > Overriding setPreferredSize() and getPreferredSize()

Look at this example:

public class MyPanel extends JPanel{

  private final Dimension dim = new Dimension(500,500); 

  @Override
  public Dimension getPreferredSize(){
      return new Dimension(dim);
  }

 public static void main(String args[]){
      JComponent component = new MyPanel();
      component.setPreferredSize(new Dimension(400,400));
      System.out.println(component.getPreferredSize());
 }

}

It is necessary to set preferredsize()

Getpreferredsize() of

So this clearly breaks the Liskov Substitution Principle

Prefferedsize is a binding property, so when you set a firepropertychange, it is executed So my problem is that when you rewrite getprefferedsize(), you don't need to rewrite setpreferredsize() Are you?

Example:

public class MyPanel extends JPanel{

  private Dimension dim = null; 

  @Override
  public Dimension getPreferredSize(){
      if(dim == null)
       return super.getPreferredSize();
      return new Dimension(dim);
  }

  @Override
  public void setPrefferedSize(Dimension dimension){
        if(dim == null)
            dim = new Dimension(500,500);
        super.setPreferredSize(this.dim); //
  }

 public static void main(String args[]){
      JComponent component = new MyPanel();
      component.setPreferredSize(new Dimension(400,400));
      System.out.println(component.getPreferredSize());
 }

}

Now we see that we get the same result, but the audience will be notified of the real value, and we will not destroy the reason of LSP. Setpreferredsize States sets the preferred size of this component But not how

Solution

Several aspects of this interesting question (MAD has mentioned my fellow developers)

Do we violate the LSP that only overrides getxxsize() (and setxxsize())?

No, if we execute it correctly: -) the first permission is the API document of the attribute. It is best to start from its origin, that is, the component:

Sets the preferred size of this component to a constant value. Subsequent calls to getPreferredSize will always return this value.

This is a binding contract, but we have implemented getter, which must comply with a constant value. If you set:

@Override
public Dimension getPreferredSize() {
    // comply to contract if set
    if(isPreferredSizeSet())
        return super.getPreferredSize();
    // do whatever we want
    return new Dimension(dim);
}

Xxsize is a bound property – really?

In the ancestors of JComponent, there is only circumstantial evidence: in fact, the component triggers propertychangeevent in the setter JComponent itself seems to record facts (BOLD):

@beaninfo preferred: true bound: true description: The preferred size of the component.

Which is... Wrong: the bound attribute means that the listener needs to be notified whenever the value changes, which is what the following (pseudo test) must pass:

JLabel label = new JLabel("small");
Dimension d = label.getPreferredSize();
Propertychangelistener l = new Propertychangelistener() ...
    boolean called;
    propertyChanged(...) 
        called = true;
label.addPropertychangelistener("preferredSize",l);
label.setText("just some longer text");
if (!d.equals(label.getPreferredSize())
   assertTrue("listener must have been notified",l.called);

... but failed For some reason (I don't know why this might be considered appropriate), they want the constant part of xxsize to be a bound property - such an override is simply impossible Of course, there is a historical question: at first, only the presenter can get it (for good reasons) In its backport to AWT, it mutates into a bean attribute that it never has

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
***
下一篇>>