The principle of single responsibility in the Design Model

Source: Internet
Author: User

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 );}

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.