学好武功,心法是多么的重要。像是那些练错心法的,比如欧阳锋大叔,心法错了,走火入魔了,死在华山之巅。还有些必先自宫的武功,没准后面一页就说上一页是不需要的。所以6大原则,大家也把心法吃透,才能练好武功啊。
首先看下设计模式6大原则的概览。
- 1、开闭原则 | 对扩展开放,对修改关闭。
- 2、里氏代换原则 | 子类都可以做到代替基类的出现。
- 3、依赖倒转原则 | 针对接口编程,依赖于抽象而非具体。
- 4、接口隔离原则 | 使用多个隔离的接口,比使用单个接口要好。(降低类之间的耦合度)
- 5、迪米特法则(最少知道原则)| 一个实体应当尽量少地与其他实体之间发生相互作用。
- 6、单一职责 | 一个类应该只有一个引起它变化的原因,一个职责。
当然这么看还是太抽象了,下面会分4天(4篇)小漫画+代码的形式详细来讲解~
第一天
![85e990af335b52f09b30894d3a563f74.png](https://i-blog.csdnimg.cn/blog_migrate/7320933e5ad5c96f96fd0ab6acc79518.jpeg)
上次链接:设计模式开篇
![5ee81c9041f248f692796a4ed52d7b69.png](https://i-blog.csdnimg.cn/blog_migrate/a8dd87adc4ea523e346f758823a5348c.jpeg)
public class Animal { public void eat() { System.out.println("eat something"); } public void sleep() { System.out.println("sleeping ~~"); }}复制代码
![b68f23546afe839675322f841627858e.png](https://i-blog.csdnimg.cn/blog_migrate/5e3ea878523b2bfcb5d90f6e1baeecf5.jpeg)
public void eat (String kind) { if (kind.equals("rabbit")) { System.out.println("eat some carmine"); } if (kind.equals("tigger")) { System.out.println("eat some meat"); }}复制代码
![e4d429d39884b3f0e9aadd6f1cb00b09.png](https://i-blog.csdnimg.cn/blog_migrate/cc2f435dde88997d7743d1e513965f28.jpeg)
![a826692661119a2131b324f856e4e8de.png](https://i-blog.csdnimg.cn/blog_migrate/8858c8362e74ab6883a6d3476602833f.jpeg)
public class Rabbit extends Animal { public void eat() { System.out.println("eat some carmine"); }}复制代码
![8fa5af4cc56b264e91e6e65d84684b3e.png](https://i-blog.csdnimg.cn/blog_migrate/78091f80ec18fe1678657107eeabe643.jpeg)
![6e7e5191e04d89fd14618a1a53319471.png](https://i-blog.csdnimg.cn/blog_migrate/932c7ed3120c77bdefd40f5b57dec6c2.jpeg)
public class Cooker { public void cook(Potato potato) { System.out.println("开始做菜了"); System.out.println(potato.getFood()); }}class Potato { public String getFood() { return "酸辣土豆丝"; }}复制代码
![7adece6bb90059340a3069425978cb1e.png](https://i-blog.csdnimg.cn/blog_migrate/040d0a6a3e51c466982ed3d2da8efb55.jpeg)
public interface FoodMaterial { String getFood();}class Potato implements FoodMaterial{ @Override public String getFood() { return "酸辣土豆丝"; }}class Chicken implements FoodMaterial{ @Override public String getFood() { return "可乐鸡翅"; }}public class Cooker { public void cook(FoodMaterial foodMaterial) { System.out.println("开始做菜了"); System.out.println(foodMaterial.getFood()); } public static void main (String []args) { Cooker cooker = new Cooker(); cooker.cook(new Potato()); }}复制代码
![c3f789828db46b0c18fc13069c49b294.png](https://i-blog.csdnimg.cn/blog_migrate/d7697e58ce68f91aa52a88a4fb5458b0.jpeg)
第二天
![3ab256e2669a7fab7f6221be14e77796.png](https://i-blog.csdnimg.cn/blog_migrate/0da04950dd4cb93e70cbebcfa5f4a843.jpeg)
![8e895b95fac25fb2ee74167a0d692f56.png](https://i-blog.csdnimg.cn/blog_migrate/8b8b56ef7396a5e45ddfbd628636aa20.jpeg)
public interface EmployService { void sign (); //打卡 void knockCode();//敲代码 void recruit(); //招聘 void relationShip(); //员工关系 void performanceEvaluation(); //绩效管理 void procedureDesign(); //程序设计 void makeBugs(); //哈哈哈}public class HrServiceImpl implements EmployService{ @Override public void sign() { System.out.println("打卡"); } @Override public void knockCode() { } @Override public void recruit() { System.out.println("招聘"); } @Override public void relationShip() { System.out.println("员工关系"); } @Override public void performanceEvaluation() { System.out.println("绩效考核"); } @Override public void procedureDesign() { } @Override public void makeBugs() { }}public class ProgrammerServiceImpl implements EmployService { @Override public void sign() { System.out.println("打卡"); } @Override public void knockCode() { System.out.println("敲代码"); } @Override public void recruit() { } @Override public void relationShip() { } @Override public void performanceEvaluation() { } @Override public void procedureDesign() { System.out.println("程序设计"); } @Override public void makeBugs() { System.out.println("造点bug"); }}public class ProgrammerClient { public void sign (EmployService employService) { employService.sign(); } public void knockCode(EmployService employService) { employService.knockCode(); } public void procedureDesign(EmployService employService) { employService.procedureDesign(); } public void makeBugs(EmployService employService) { employService.makeBugs(); }}public class HrClient { public void sign (EmployService employService) { employService.sign(); } public void performanceEvaluation(EmployService employService) { employService.performanceEvaluation(); } public void recruit(EmployService employService) { employService.recruit(); } public void relationShip(EmployService employService) { employService.relationShip(); }}复制代码
![9bf46aa1933b7a0b59c98c0bed6c27b7.png](https://i-blog.csdnimg.cn/blog_migrate/d1a8b73aca1488bcf3b7591250d56fd8.jpeg)
public interface EmployService { void sign(); //打卡}public interface HrService { void recruit(); //招聘 void relationShip(); //员工关系 void performanceEvaluation(); //绩效管理}public class HrServiceImpl implements HrService,EmployService{ @Override public void sign() { System.out.println("打卡"); } @Override public void recruit() { System.out.println("招聘"); } @Override public void relationShip() { System.out.println("员工关系"); } @Override public void performanceEvaluation() { System.out.println("绩效管理"); }}public interface ProgrammerService { void knockCode();//敲代码 void procedureDesign(); //程序设计 void makeBugs(); //哈哈哈}public class ProgrammerServiceImpl implements ProgrammerService,EmployService { @Override public void sign() { System.out.println("签到"); } @Override public void knockCode() { System.out.println("敲代码"); } @Override public void procedureDesign() { System.out.println("程序设计"); } @Override public void makeBugs() { System.out.println("造点bug"); }}复制代码
![015e53c0afd31d40b9edcd34f10b458b.png](https://i-blog.csdnimg.cn/blog_migrate/60c3f0f076cc38053d67b23e4db6effe.jpeg)
![67d6a327bf68d86d7b5451fdecc6e7e9.png](https://i-blog.csdnimg.cn/blog_migrate/f59d15ed41165efe738119ed92613141.jpeg)
第三天
![7b109f869c70473da83cbeb42fc57e41.png](https://i-blog.csdnimg.cn/blog_migrate/ca7ebe115335e7b37026a35e13f780c8.jpeg)
public interface CarService{ String getName (); void drive (); String color();}public class Sedan implements CarService { private String name; private String color; private int speed; public Sedan(String name, String color,int speed{ this.name = name; this.color = color; this.speed = speed; } @Override public String getName() { return this.name; } @Override public void drive() { System.out.println("普通的小车普通的开" + this.speed); } @Override public String color() { return this.color; }}复制代码
![c52576dd27af07fb6571b2a6cfddc806.png](https://i-blog.csdnimg.cn/blog_migrate/b7592d0e00fed928f7cd356eeaeba929.jpeg)
public class FastSedan extends Sedan { public FastSedan(String name, String color, int speed) { super(name, color, speed); } public void drive() { System.out.println("飙车的飙,飙起来" + this.getSpeed() * 2); }}复制代码
![09dc15d5471c02fbbddfd48bb14a4fc9.png](https://i-blog.csdnimg.cn/blog_migrate/8867b4543396a347c09cb7a9d280dd2d.jpeg)
public class Boss { //发布任务给cto public void pushJob(CTO cto) { List list = new ArrayList(); for (int i = 0;i < 5;i++) { list.add(new Programmer()); } cto.assignmentTask(list); }}public class CTO { //分配任务 public void assignmentTask(List programmers) { for (Programmer programmer:programmers) { programmer.backPot(); } }}public class Programmer { //背锅 public void backPot () { System.out.println("程序员背起了小锅"); }}public class Client { public static void main (String []args) { Boss boss = new Boss(); boss.pushJob(new CTO()); }}复制代码
![b6bd89fa472ed72f50a16ae78f541a03.png](https://i-blog.csdnimg.cn/blog_migrate/819a3a5d23a5992186eb4e93ed91d409.jpeg)
最后一天
我们来巩固一下
![4d0924012d0ed4e47a7b9e76523cb647.png](https://i-blog.csdnimg.cn/blog_migrate/1b69fe6342d77165745fa0bc438b2459.jpeg)
![189b90ffa61edb213869dd409d9e322d.png](https://i-blog.csdnimg.cn/blog_migrate/323217f3e4405f16343192508d911574.jpeg)
![146946be5b9f2ae3e331ee29a191050e.png](https://i-blog.csdnimg.cn/blog_migrate/94d4148add93222a27c3aaeca81aa25a.jpeg)
![316b97e7ef5e8fff65a59f45ef153caa.png](https://i-blog.csdnimg.cn/blog_migrate/767b2c03cbccd0248d3527f8bd879adf.jpeg)
![99a927696e48ffb1608902fbfae7f11b.png](https://i-blog.csdnimg.cn/blog_migrate/a6cd6739982617394f8df1d067e31cc6.jpeg)
公众号「Java小咖秀」,专注Java领域 , 和小伙伴们一起学习进步~
作者:Java小咖秀
链接:https://juejin.im/post/5f0727826fb9a07e894ab243