Third: single Responsibility Principle

Source: Internet
Author: User
Zookeeper

Preface: I recently read the book "big talk Design Patterns", which is an example of C. Now I want to share my notes and try to write a Java version. The example is also an example in the book. It is implemented in Java. I will also give some understanding suggestions later. Thank you for writing this article.

1. What is a single responsibility principle?

Today we are talking about a single responsibility principle, which should be easy to understand. For a single responsibility, there should be only one reason for its change. That is to say, it only does one simple thing, but does its essential work well. Others do not participate as much as possible.

When designing classes, we often start coding without thinking well. This is a bad habit. We should clarify the business logic, separate the interaction code and logic code, and then separate the logic code based on the division of labor, on the premise that he needs a class, that is, there is a reason that can cause this part of code changes, then this part of code can be individually written into a class. Most ITER (IT people) began to use procedural programming such as C language, and because we humans are used to do things by step, so the first thing that comes to mind in programming is the method, not the class. In an object-oriented language, a class is the smallest unit of a program. Therefore, we need to train our abstract ideas. Everything is a class, and both abstract and specific things can be a class.

The design pattern is not very advanced, but we have not summarized it. The old birds who have years of development experience can look at the design pattern and laugh, because they almost used it, but didn't give a name. Design Patterns are actually very advanced, because from a philosophical point of view (Don't scold me, I just want to promote the use of Marx's course), it is not only applicable to programming, it also applies to life. Like our ITER, everyone knows that if we are determined to work hard in the IT industry, we must find a way to do our best. We cannot be a programmer this year, but we can do sales next year. Otherwise, you cannot do anything. There is another way to go, like android, IOS, WP, javaweb,. net, etc. If there are too many things to do, you will do nothing in ten years. If you only engage in Android, one knowledge point a day, one small project a day, and one big project a day. A year later, you are definitely a big bull.

Ii. code example
Let's talk less about the instance (taking photos on your mobile phone ).

General Design:

Phone. Java

Public class phone {
Private float width;
Private float height;
Private float thick;

Public void takephoto (){
System. Out. println ("Take a photo ");
}

Public void makecall (){
System. Out. println ("make a call ");
}

Public void sendmessage (){
System. Out. println ("send a message ");
}

Public void playmusic (){
System. Out. println ("Play a music ");
}
}

In fact, you can do this:

Camera. Java

Public interface camera {
Public void takephoto ();
}

Mp3.java

Public interface MP3 {
Public void playmusic ();
}

Phone. Java

Public class phone implements camera, MP3 {
Private float width;
Private float height;
Private float thick;

Public void makecall (){
System. Out. println ("make a call ");
}

Public void sendmessage (){
System. Out. println ("send a message ");
}

Public void playmusic (){
// Todo auto-generated method stub
System. Out. println ("Play a music ");
}

Public void takephoto (){
// Todo auto-generated method stub
System. Out. println ("Take a photo ");
}
}

Iii. Summary

Although the single responsibility principle makes the code very clear, it can solve a class that is too large, but it cannot be used in disorder. If you use it too much, your program may be broken down, and the loss will not be worth the candle. In a word, it is enough to find an object-oriented object. Don't get any snow. We can go to new.


Third: single Responsibility Principle

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.