The principle of single responsibility in the Design Model
I recently read <Zen of design patterns> and I feel that this book is very good. Although the demo is simple but clear, I feel it is necessary to repeat the demo of a single principle of responsibility: https://github.com/sanyinchen/UMLDemo if a class has more than one motivation to be changed, it has more than one responsibility. The single responsibility principle means that one class or module should have only one reason for change. Demo: one mobile phone type: Iphone. You need to use a mobile phone to call the phone. Three phases are required: dial the phone number, call, and hook up. In the three phases, the management protocol is used for dialing numbers and hanging up. The call is only for data transmission, so we can design it like this. IconnectionMangager interface: public interface IconnectionMangager {public void dial (); public void hangup ();} IDataManger interface for data transmission public interface IDataManger {public void DataTranfer (IconnectionMangager mangager );} the Iphone class inherits from IDataManger public class Iphone implements IDataManger {@ Override public void DataTranfer (IconnectionMangager mangager) {// TODO Auto-generated method stub mangager. dial (); System. out. println (I'm talking !); Mangager. hangup () ;}} SingleDemo: public static void main (String [] args) {// TODO Auto-generated method stub Iphone = new iphone (); iconnectionMangager mangager = new IconnectionMangager () {@ Override public void hangup () {// TODO Auto-generated method stub System. out. println (I started to call ...);} @ Override public void dial () {// TODO Auto-generated method stub System. out. println (I hung up ...);}}; iphone. dataTranfer (mangager );}