I want to write bean-friendly classes. I observed a tendency (mainly with beans) to move the required parameters to setters from the constructor (and use the method init()when this is done when setting the initial state).
init()
This method concerns me because I want my classes to be usable without a bean infrastructure, like Java objects. I believe that I need to check the correct state of the object in each style of the method statement.
Quick demo for the above:
class A { public int x; public int y; private int sum; private boolean initialized = false; public void init() { sum = x + y; initialized = true; } private void initCheck() { if (!initialized) { throw new IllegalStateException("Uninitialized object."); } } public int getXMulSum() { initCheck(); return x * sum; } public int getYMulSum() { initCheck(); return y * sum; } }
Is there a better practice?
Given that you do not want to use the framework ...
, . . . .
, , , - , , , . , . Factory, bean. , - bean, .
Builder, .
" ( beans) "
. bean, "" , . , , , , , bean, . .
, , .
, , , .
, , . , . Spring .
, . init(), .
Source: https://habr.com/ru/post/1535314/More articles:delegate () vs on () in jquery? - jqueryVertical alignment with Pure CSS (from YUI) - cssSupport for ActionBar and GameBaseActivity - androidhow to call a class of a class as a parameter in java - javaPrevent Listview links from being intercepted using its Child MapView - androidHow to make ngInfiniteScroll launched in Bootstrap modal? - angularjsHow to include data files with ado packages in Stata - stataStoring passwords of external systems in encrypted form in a J2EE application - java-eeSecret structure in org mode? - emacsThe preferred recoverable way to store passwords in a database - databaseAll Articles