单一职责原则
——专心致志只做一件事
1 package danyizhize; 2 3 class SunWuKong { 4 public void XiangMo(String name) { 5 System.out.println(name + "降魔!"); 6 } 7 8 public void XingLi(String name) { 9 System.out.println(name + "拿行李!"); 10 } 11 12 public void QianMa(String name) { 13 System.out.println(name + "牵白龙马!"); 14 } 15 16 public void QiMa(String name) { 17 System.out.println(name + "骑马!"); 18 } 19 } 20 21 public class DanYiZhiZe { 22 23 public static void main(String[] args) { 24 // TODO Auto-generated method stub 25 SunWuKong sunWuKong = new SunWuKong(); 26 sunWuKong.XiangMo("孙悟空"); 27 sunWuKong.QianMa("孙悟空"); 28 sunWuKong.XingLi("孙悟空"); 29 sunWuKong.QiMa("孙悟空"); 30 } 31 }
运行结果:
孙悟空降魔!
孙悟空牵白龙马!
孙悟空拿行李!
孙悟空骑马!
孙悟空的任务太多,忙不过来!!!
1 package danyizhize; 2 3 class SunWuKong { 4 public void XiangMo(String name) { 5 System.out.println(name + "降魔!"); 6 } 7 } 8 9 class ZhuBaJie { 10 public void QianMa(String name) { 11 System.out.println(name + "牵白龙马!"); 12 } 13 } 14 15 class ShaHeShang { 16 public void XingLi(String name) { 17 System.out.println(name + "拿行李!"); 18 } 19 } 20 21 class TangSeng { 22 public void QiMa(String name) { 23 System.out.println(name + "骑马!"); 24 } 25 } 26 27 public class DanYiZhiZe { 28 29 public static void main(String[] args) { 30 // TODO Auto-generated method stub 31 SunWuKong sunWuKong = new SunWuKong(); 32 ZhuBaJie zhuBaJie = new ZhuBaJie(); 33 ShaHeShang shaHeShang = new ShaHeShang(); 34 TangSeng tangSeng = new TangSeng(); 35 36 sunWuKong.XiangMo("孙悟空"); 37 zhuBaJie.QianMa("猪八戒"); 38 shaHeShang.XingLi("沙和尚"); 39 tangSeng.QiMa("唐僧"); 40 } 41 }
运行结果:
孙悟空降魔!
猪八戒牵白龙马!
沙和尚拿行李!
唐僧骑马!
改动大,将原有的1个类变成了4个类,还需要修改客户端代码!!!
1 package danyizhize; 2 3 class Person { 4 public void Work(String name) { 5 if ("孙悟空".equals(name)) { 6 System.out.println(name + "降魔!"); 7 } else if ("猪八戒".equals(name)) { 8 System.out.println(name + "牵白龙马!"); 9 } else if ("沙和尚".equals(name)) { 10 System.out.println(name + "拿行李!"); 11 } else if ("唐僧".equals(name)) { 12 System.out.println(name + "骑马!"); 13 } 14 } 15 } 16 17 public class DanYiZhiZe { 18 19 public static void main(String[] args) { 20 // TODO Auto-generated method stub 21 Person person = new Person(); 22 23 person.Work("孙悟空"); 24 person.Work("猪八戒"); 25 person.Work("沙和尚"); 26 person.Work("唐僧"); 27 } 28 }
运行结果:
孙悟空降魔!
猪八戒牵白龙马!
沙和尚拿行李!
唐僧骑马!
如果再增加其他人,很有可能变成孙悟空做饭,操作简单,却存在隐患!!!
1 package danyizhize; 2 3 class Person { 4 public void XiangMo(String name) { 5 System.out.println(name + "降魔!"); 6 } 7 8 public void XingLi(String name) { 9 System.out.println(name + "拿行李!"); 10 } 11 12 public void QianMa(String name) { 13 System.out.println(name + "牵白龙马!"); 14 } 15 16 public void QiMa(String name) { 17 System.out.println(name + "骑马!"); 18 } 19 } 20 21 public class DanYiZhiZe { 22 23 public static void main(String[] args) { 24 // TODO Auto-generated method stub 25 Person person = new Person(); 26 person.XiangMo("孙悟空"); 27 person.QianMa("猪八戒"); 28 person.XingLi("沙和尚"); 29 person.QiMa("唐僧"); 30 } 31 }
孙悟空降魔!
猪八戒牵白龙马!
沙和尚拿行李!
唐僧骑马!
需要足够简单的逻辑,才可以在代码上违反单一职责原则;
需要类中方法数量足够少,才可以在方法级别上违反单一职责原则。