Previously I was reading up on Java Spring framework and they have setter injection concept. They relies heavily on JavaBeans notation so during run-time, the Spring Container know how to invoke the setter method successfully.
E.g
<bean id="myDao" class="com.test.MyDAO">
<property name="dataSource" ref="dataSource"/>
<property name="sqlTemplate" ref="sqlSessionFactory"/>
</bean>
Above configure property name dataSource and sqlTemplate. So how does Spring Container know how to invoke them and passed in the parameters as declared ?
Concept is simple. They uses JavaBeans notation and all our classes just need to define a setter method.
E.g
class MyDAO {
public void setDataSource(...);
public void setSqlTemplate(...);
}
Spring will infer from the property name and then call the relevant method set<PropertyName> automatically.
So this is a classic example on why setter/getter is needed for some Java framework. Idea is simple and it works. Rather than writing off getter/setters are evil, I would prefer to say keep your options open. You never know when you need it cuz some Java framework require this too.
Likewise, the concept applies to C++. If one day, certain C++ framework decide to adopt the JavaBeans notation, then the C++ class setter/getter is mandatory indeed.