官术网_书友最值得收藏!

  • Expert Android Programming
  • Prajyot Mainkar
  • 392字
  • 2021-07-08 10:29:14

Single Responsibility Principle

The Single Responsibility Principle states that:

A class should have one, and only one, reason to change.

The idea behind this principle is to design a class that has one responsibility or various methods with unique functionality. According to this principle, a method should not do more than one task at a time. Each function must be designated a unique task.

Let's take, for example, the adapter of a recyclerView:

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) { 
   PlaceItem item = list.get(position); 
 
   String name = item.getName() != null ? item.getName() : ""; 
   String description = item.getDescription() != null ? item.getDescription() : ""; 
   String location = item.getAddress() != null ? item.getAddress() : ""; 
   String rating = String.format("%.02f", item.getRating()); 
   String distance = item.getDistance()+"km"; 
   holder.name.setText(name); 
   holder.location.setText(location); 
   holder.description.setText(description); 
   holder.rating.setText(rating); 
   holder.distance.setText(item.getDistance()); 
   Picasso.with(context) 
           .load(R.drawable.im_backdrop) 
           .placeholder(R.drawable.placeholder_200) 
           .error(R.drawable.placeholder_200) 
           .into(holder.image); 
} 

The preceding code is the onBindViewHolder of the adapter of a recyclerView. It does not satisfy the single responsibility principle, as we are formatting the values as we are setting the text to the views. The onBindViewHolder of the recyclerView adapter is mainly responsible for binding the view with the values of its object class.

In the preceding code, the onBindViewHolder is doing more tasks than it should. If we have kept the same format in more than one place and there comes a need to make changes to the format, then we will have to make the changes everywhere and this may result in software logic duplication issues. If we don't update the code in some places, it may cause an error. If the code was a logical change that had been replicated, the entire flow of the app would change. To prevent this, we have to write the code in a way that we don't have to make many changes if the features are changed.

As in the case of the preceding example, we can refactor it by the following:

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) { 
   PlaceItem item = list.get(position); 
 
   holder.name.setText(CommonFunctions.checkNull(item.getName())); 
   holder.description.setText(CommonFunctions.checkNull(item.getDescription())); 
   holder.location.setText(CommonFunctions.checkNull(item.getAddress())); 
   holder.rating.setText(CommonFunctions.formatRating(item.getRating())); 
   holder.distance.setText( CommonFunctions.formatDistance(item.getDistance())); 
 
   Picasso.with(context) 
           .load(R.drawable.im_backdrop) 
           .placeholder(R.drawable.placeholder_200) 
           .error(R.drawable.placeholder_200) 
           .into(holder.image); 
} 

Now, as can be seen, the code checking and formatting are not done in the onBindViewHolder function but they are being done elsewhere. Due to this, the change of the functionality or format can be done in the common function instead of each instance of the code.

主站蜘蛛池模板: 山东省| 肇州县| 会宁县| 大同市| 航空| 边坝县| 麦盖提县| 万源市| 宁陵县| 板桥市| 咸宁市| 娄烦县| 东山县| 册亨县| 宁安市| 济源市| 固阳县| 泗阳县| 思茅市| 新巴尔虎左旗| 海原县| 绥化市| 峡江县| 芦山县| 吉首市| 同仁县| 武邑县| 抚顺县| 衡东县| 横峰县| 潮州市| 蓬莱市| 北辰区| 灵丘县| 福海县| 盐亭县| 重庆市| 白玉县| 密山市| 新疆| 建平县|