voidmain() {
StatelessWidget adapter =newStatelessWidget();
adapter.select(newRadioButtonWidget());
}
classStatelessWidget {
voidselect(ClickListener clickListener) {
if (clickListener isListItemWidget) {
clickListener.changeTheBackground();
} elseif (clickListener isRadioButtonWidget) {
clickListener.check();
}
clickListener.onClick(1);
}
}
mixinClickListener {
voidonClick(int position);
}
classListItemWidgetimplementsClickListener {
@overridevoidonClick(int position){
print("Clicked list item $position");
}
voidchangeTheBackground() {
print("Change the background color of the item view");
}
}
classRadioButtonWidgetimplementsClickListener {
@overridevoidonClick(int position){
print("Clicked radio button $position");
}
voidcheck() {
print("Enable the radio button");
}
}
Good
voidmain() {
StatelessWidget adapter =newStatelessWidget();
adapter.select(newRadioButtonWidget());
}
classStatelessWidget {
voidselect(ClickListener clickListener) {
clickListener.onClick(1);
}
}
mixinClickListener {
voidonClick(int position);
}
classListItemWidgetimplementsClickListener {
@overridevoidonClick(int position){
print("Clicked list item $position");
_changeTheBackground();
}
void_changeTheBackground() {
print("Change the background color of the item view");
}
}
classRadioButtonWidgetimplementsClickListener {
@overridevoidonClick(int position){
print("Clicked radio button $position");
_check();
}
void_check() {
print("Enable the radio button");
}
}
I — The Interface Segregation Principle (ISP):
Bad
classImageViewWidgetwithOnClickListener {
@overridevoidonImageClick(int position) {
// Yes, I have received a callback, go to the next activity.print("Clicked position is $position");
}
@overridevoidonRadioButtonClick(int position) {
// This is no longer needed for this activity, but still I have been implemented for no use...
}
}
mixinOnClickListener {
voidonImageClick(int position);
voidonRadioButtonClick(int position);
}
Good
classImageViewWidgetwithOnImageClickListener {
@overridevoidonImageClick(int position) {
// Yes, I have received a callback, go to the next activity.print("Clicked position is $position");
}
}
mixinOnImageClickListener {
voidonImageClick(int position);
}
mixinOnRadioButtonClickListener {
voidonRadioButtonClick(int position);
}
D - The Dependency Inversion Principle (DIP)
Bad
classRepositoryImpl {
List<FeedItem> getDataFromApi() {
// do your api call
}
List<FeedItem> getDataFromCache() {
// get the data from the database
}
}
Good
classRepositoryImpl {
RemoteDataSource remoteDataSource;
LocalDataSource locaDataSource;
RepositoryImpl(this.remoteDataSource, this.locaDataSource);
}
abstractclassRemoteDataSource {
List<FeedItem> getDataFromApi();
}
abstractclassLocalDataSource {
List<FeedItem> getDataFromCache();
}
classRemoteDataSourceImplextendsRemoteDataSource{
// Lets assume this class has an access to http clientList<FeedItem> getDataFromApi(){
// do your api call
}
}
classLocalDataSourceImplextendsLocalDataSource {
// Lets assume this class has an access to database instanceList<FeedItem> getDataFromCache(){
// get the data from the database
}
}