17

I am working with JavaFx TableView and found there are some classes to use a TableView for example SimpleStringProperty, StringProperty, SimpleBooleanProperty and BooleanProperty, etc. Now I am wondering about which one to use for TableView either SimpleStringProperty or only StringProperty and what are the difference between them.

Biswadip Dey
  • 509
  • 2
  • 7
  • 20

1 Answers1

21

StringProperty is the abstract base class for observable string properties, SimpleStringProperty is a concrete implementation.

The rule is:

  1. Show StringProperty in your API
  2. Use SimpleStringProperty as the concrete implementation in your code

You sometimes see JavaFX code itself createing anonymous inner classes from StringPropertyBase and the reason for this is that it is a bit more effecient memorywise but nothing you normally have to bother yourself.

tomsontom
  • 5,856
  • 2
  • 23
  • 21
  • 2
    Or to be more specific: `StringProperty`is the base class for all writable string properties. – eckig Jan 21 '15 at 07:42
  • @tomsontom If I'm going to use a SimpleStringProperty, why wouldn't I use the same in the API and class definition? – simpleuser Feb 08 '16 at 04:31
  • 2
    This is lesson 1 in API design and object orientation only provide as much information as required in the API this allows you to switch the internals in future without breaking the users of the API – tomsontom Feb 08 '16 at 15:25
  • @tomsontom but if you then change the API to want something (presumably) derived from StringProperty, existing users would not see any compilation issues since a SimpleStringProperty could still be stored in a StringProperty as well as the new derived type, and would then not work correctly. By specifying SimpleStringPropery in the API, it immediately becomes obvious if you change the API. – simpleuser Feb 08 '16 at 19:31
  • @simpleuser "and would then not work correctly" why? – Franklin Yu Nov 14 '17 at 20:02