[Original, tutorial, and serialization] Design Pattern of Android-design principles Chapter 2: single responsibility principle Qiao Feng vs Mu rongfu

Source: Internet
Author: User
<Big talk design model> description and copyright notice of this tutorial

L this document references and uses free images and content on the Internet, and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used for profit.

L if you have any questions or suggestions about this document, go to the official blog

Http://www.cnblogs.com/guoshiandroid/ (with the following contact information), we will carefully refer to your suggestions and modify this document as needed to benefit more developers!

L The latest and complete content of "big talk design mode" will be regularly updated on the official blog of guoshi studio. Please visit the blog of guoshi studio.

Http://www.cnblogs.com/guoshiandroid/get more updates.

Guoshi studio is a technical team dedicated to enterprise-level application development on the Android platform. It is committed to the best Android Application in China.ProgramDevelopment institutions provide the best Android enterprise application development training services.

Official contact information for Enterprise Training and Development Cooperation:

Tel: 18610086859

Email: hiheartfirst@gmail.com

QQ: 1740415547

QQ: 148325348

Guoshi studio is better for you!


 

  View other parts:Overall description and Chapter index of this tutorial

PDF download link

Single Responsibility Principle Qiao FengVSMurong Fu

Use Cases:


Rivers and Lakes: North Qiao Feng and South Mu Rong.

Under the shaoshi Mountains, the world is crowded.

"Come on, I'm afraid of Xiao Feng !", Qiao Feng was involved in the battle of life and death with Murong Fu, youtanzhi, and Ding Chunqiu. Qiao Feng is indeed the best hero in the world. You can beat me with one enemy. Qiao Feng threw out the "excited and regretful" in the 18 th palm of the Dragon Army. At this time, Mu rongfu stepped back, in a hurry, and used his own stunt "youlong yinfeng" to defend Qiao feng from such a strong attack, at this time, Mu rongfu's legs were chosen on the pillar of the pavilion. Wang YuanYi thought, "cousin's use of youlong to attract Phoenix will naturally not be defeated, but in the face of all the heroes in the world, it's unfair to have teamed up with such people as youtanzhi and Ding Chunqiu To Deal With Hero Qiao Feng. What are you thinking about, cousin? ". At this time, Duan Yu, under the protection of Qiao Feng, was anxious and worried that this would not work, lest he have any accidents. After all, Qiao Feng is always a hero, and he is also a top-notch expert in the rivers and lakes, how can this happen? Suddenly, Duan Yu broke away from the crowd and rushed up and said to Mu rongfu, "you two are playing a hero, Mu rongfu. You can come and join me alone !". Murong was initially dissatisfied with Duan Yu's affection for Wang yuhan for a long time, and he was so angry that he said, "looking for death !", Just like Duan Yu. At this time, Duan Yu learned from her fairy sister the great power of "lingbo weibu" and Mu rongfu. At this time, Xiao Zhu is cleaning up Ding Chunqiu. While Qiao Feng is playing games against him. Although he has become a very powerful martial artist, he is not a competitor of Qiao Feng. In a few moves, Qiao Feng threw the game out of the top of the tower, as a result, I got a broken leg and suffered a fiasco! Duan yubai, under the sword of Murong Fu, is rather unyielding. Murong Fuzheng wanted a sword to learn about Duan Yu. Duan zhengchun shouted, "Hugh hurt me !" Blocks Murong Fu's sword. Murong Fu was very angry, and Mu Rong was injured in the ground within a few moves. Seeing that Murong Fu was about to kill his father, and lying on the ground, Duan Yu, said "ah", which gave the "Six-pulse sword" of the Dali state. Within a few moves, Murong was defeated, as he was about to launch another attack, Wang Yu, the lover of his dream, said softly, "he has no mercy at all." When I heard the voice of Wang Yu, I lost my mind, mu rongfu took this opportunity to attack Duan Yu. Qiao Feng saw the situation and shouted: "three younger brothers are careful". While instantly shot, Mu rongfu's weapon was crushed. Mu rongfu hasn't responded yet, qiao Feng has been lifted up in the air, and Qiao Feng shouted: "I'm a good guy at Xiao Feng, how can I name it like you?" Then I threw Murong out of hate! Murong Fu fiasco in front of the hero in the world, conscious face impermanence, to fight the sword, was blocked by the gray people...

Definition: 

Single Responsibility Principle: For a class, there should be only one reason for its change. In other words, a class has a single function and only needs to be related to it.

If an extra unrelated function or other class function is completed, there will be too many factors that cause a class change. If one class needs to be modified, other related connectionsCodeWill be affected, which directly leads to the difficulty of debugging once the system has problems, and this is not conducive to maintenance.

Following the single responsibility principle will also bring great convenience to the test.

Violating the single responsibility principle will reduce class cohesion and enhance class coupling.

Violating the single responsibility principle will lead to an increase in geometric series. Because the associations between classes are too strong, each class will have an impact on other classes, an error in a class may lead to errors in other associated classes, and new errors may occur when the associated classes are joined.

In software development, people are increasingly aware of the importance of a single responsibility principle. The artist only needs to be responsible for the interface of the artist, and the business layer staff only need to write the Business Code, the data layer staff only need to pay attention to the work of the data layer. In this way, everyone makes a special trip to work together, and the work efficiency has been greatly improved.

Currently, the classic MVC mode of software development also reflects the single responsibility principle. Model-View-control (MVC) is a three-layer architecture model of model, view, and controller. M is an exponential data model, V is a user interface, and C is a controller. The MVC mode is used to separate data and performance. The same data layer can have different display layers. Changes in the data layer and the display layer do not affect each other. This greatly improves Software maintainability and reusability, and facilitates software management and improves software development efficiency.

As shown in:

Story analysis: 

Wang yuyu, a doctor of martial arts, helped us to join almost all of the martial arts in the world and lost Mu Rong, who was "one by one, also applied to the other". He lost to Duan Yu, who knew the Six-pulse sword,

It also lost to beat Qiao Feng, who beat the world with the X-Dragon force. Aside from other reasons, Murong fu will lose because although he knows a lot about martial arts and even knows about more than 80% of the martial arts in the world, it is rare to be proficient because of his enthusiasm; from another point of view, Mu rongfu is also affected by the cultivation of so many martial arts. You think, how can a person of a young age cultivate so many martial arts skills? How can he have time to master all kinds of martial arts skills and be tired. In fact, Mu rongfu is like a wolf, facing a large group of samples, and wants to eat everything! As a result, it is possible that each goat is killed, but there is no time to taste and digest it. Duan Yu and Qiao Feng are different. Duan Yuhui has six secrets. Here we mainly analyze Qiao Feng. Qiao Feng was adopted by Shaolin temple since his childhood. He worked hard and worked hard to cultivate his skills, until he reached the point where he had no choice but to win. Of course, a hero like Qiao Feng is naturally experienced in practice. In this way, Mu rongfu, the "one by one, another by another", met Qiao Feng, who beat the world without rivals with the 18 palm of the Dragon Army, and Mu rongfu lost.

From the perspective of design principles, Mu rongfu lost because he violated the single responsibility principle. He put too much energy into various martial arts, and he lost in martial arts. Of course, it is undeniable that Murong Fu is indeed a young and promising martial arts talent. However, in actual combat, you need to pay attention to your real strength, rather than deciding the outcome based on the number of martial arts you know. The practice of too many martial arts categories will inevitably make people too tired, and it is very easy to get angry. Just like eating, eating too much is not necessarily a good thing, and it is almost certainly not a good thing. This is because, on the one hand, it is too much to eat, it is not good for your digestive system and body, and it is easy to gain weight. On the other hand, it is not necessarily much to eat, in addition, the digestion capability is often weakened because eating too much brings too much burden and affects the functions of the digestive system. Qiao Feng is not the same. He is laying a solid foundation and focuses on reducing the growth of the Dragon's hand until he can do whatever he wants and use it freely. Of course, Gao shihaoxia, like Qiao Feng, has a better understanding of the essence of martial arts and the essence of martial arts than mu rongfu. In addition, Qiao Feng has a specific problem, that is, being strong in the face of strong, and being brave in the Vietnam War.

The single responsibility principle tells us that a class should have only one reason for this change. This improves the maintainability and reusability of the class. If Qiao Feng and Mu rongfu are compared to two categories, the main cause of the change is to drop the dragon's 18 ops, and Qiao Feng has done the ultimate in reducing the dragon's 18 ops, whether the enemy is a friend, both of them can be resolved with the "x-Dragon" drop-down. Murong Fu, however, has caused too many reasons for his change. Although "by the means of one, he still applies", almost all Jianghu people fear three points, but because of the many and not refined, after encountering a master like Qiao Feng, I am afraid I will not be able to "let alone.

Here, we think of Qiao Feng as the method of reducing the dragon's 18 ops, while Mu Rong's martial arts are also seen as the method of Mu Rong's Fu. The modeling image is as follows:

JavaCode implementation: 

Qiao Feng's class:

PackageCom. diermeng. designpattern. SRP;

/*

* Qiao Feng

*/

Public ClassQiaofeng {

/*

* Name

*/

String name;

 

/*

* Construction method without Parameters

*/

PublicQiaofeng (){}

/*

* Construction Method with Parameters

*/

PublicQiaofeng (string name ){

This. Name = Name;

}

 

PublicString getname (){

ReturnName;

}

Public VoidSetname (string name ){

This. Name = Name;

}

 

/*

* Qiao Feng's skill presentation

*/

Public VoidGongfu ()

{

If(This. Getname ()! =Null){

System.Out. Println ("I am" +This. Getname () + "I am able to cope with the use of the X-Dragon force, whether it is an enemy or a friend, thanks to my single responsibility principle! ");

}

Else{

System.Out. Println! ");

}

}

}

 

 

Murong complex class diagram:

PackageCom. diermeng. designpattern. SRP;

/*

* Murong Fu

*/

Public ClassMurongfu {

/*

* Name

*/

String name;

 

/*

* Construction method without Parameters

*/

PublicMurongfu (){}

/*

* Construction Method with Parameters

*/

PublicMurongfu (string name ){

This. Name = Name;

}

 

PublicString getname (){

ReturnName;

}

Public VoidSetname (string name ){

This. Name = Name;

}

 

/*

* Murong Fu's kung fu presentation

*/

Public VoidGongfu ()

{

If(This. Getname ()! =Null){

System.Out. Println ("I am" +This. Getname () + "I will do a lot of martial arts. For the vast majority of people, I can do the same thing, but if I encounter a master like Qiao Feng, I will be over, this is because we have not followed the single responsibility principle! ");

}

Else{

System.Out. Println, this is because we have not followed the single responsibility principle! ");

}

}

}

Create a test class with the following code:

PackageCom. diermeng. designpattern. SRP. client;

 

ImportCom. diermeng. designpattern. SRP. murongfu;

ImportCom. diermeng. designpattern. SRP. qiaofeng;

 

Public ClassGongfutest {

 

/**

*@ ParamARGs

*/

Public Static VoidMain (string [] ARGs ){

 

Qiaofeng =NewQiaofeng ("Qiao Feng ");

Murongfu =NewMurongfu ("Murong Fu ");

 

Qiaofeng. gongfu ();

Murongfu. gongfu ();

}

 

}

The program running result is as follows:

I am Qiao Feng and have been able to cope with this challenge. Thank you for following the single principle of responsibility!

I'm Murong Fu, and I will do a lot of martial arts. For most people, I can do what I want to do. But if I encounter a master like Qiao Feng, I will be over, this is because we have not followed the single responsibility principle!

 

Existing applications: 

Here we have used the MVC model as an example to analyze the application of the single responsibility principle.

Model vehicles, view layers, and control layers are responsible for and independent from each other. A model can have multiple views, and a view can have multiple controllers, the same controller can also be composed of multiple models. The basic process of MVC is as follows: the user interacts with the view, and the view accepts and feedbacks the user's action. The view sends the user's request to the corresponding controller, and a controller determines which model to call, then, the model calls the corresponding business logic to process user requests. If data needs to be returned, the model returns the corresponding data to the control, and the Controller calls the corresponding view, the data returned by the view is formatted and rendered in a way that is as friendly as possible to the user.

Tip: 

Focus on one thing and everything.

What kind of harmonious and beautiful will the world be if everyone in this world is focused on doing one thing and taking it to its utmost?

Qiao Feng, who follows the principle of single responsibility, made Shaolin Temple's sweeping monk sigh: "the 18 th palm of the Dragon is the first in the world !"

Establish a high goal, and then make every effort to achieve this goal.

May you have a single responsibility principle!

Note: This document references and uses free and open images and content on the Internet and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used for profit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.