- 💗作者簡介:大家好,我是卷心菜~~,第二季新星計劃java領域第十名,
- 📝個人主頁:卷心菜的CSDN博客
- 📕系列專欄:本文寫在Java設計模式專欄:不就是java設計模式嗎
- 📧如果有錯誤的地方,請指正!
- 🌻向日葵向陽生長,我也是,
文章目錄
- 前言
- 一、為什么要學習設計模式?
- 二、常用的七大原則
- 三、單一職責原則
- 1、基本介紹
- 2、代碼對比
- 3、注意事項和使用細節
- 四、介面隔離原則
- 1、基本介紹
前言
自己最近在學習Java的設計模式,本篇講的是設計模式常用七大設計原則的單一職責原則和介面隔離原則,自己每記錄一些知識筆記就會首發在CSDN博客上,能夠確保學習完設計模式之后,能夠對其有一定程度上的理解和知識記錄,形成一個相對不錯的知識點框架,來在寒假記錄自己學習的點點滴滴,希望大家喜歡💗
一、為什么要學習設計模式?
在撰寫軟體的程序中,設計模式是為了讓軟體具有更好的以下優點:
代碼重用性(相同功能的代碼不用多次撰寫)可讀性(編程規范性,便于其他程式員的閱讀和理解)可擴展性(當需要增加新的功能時,非常的方便)可靠性(當增加新的功能后,對原來的功能沒有影響)使程式呈現高內聚、低耦合的特性
二、常用的七大原則
單一職責原則介面隔離原則依賴倒轉原則里氏替換原則開閉原則迪米特法則合成復用原則
三、單一職責原則
1、基本介紹
對類來說,一個類應該只負責一項職責,如果類A負責兩個不同的職責:職責1、職責2,當職責1需求變更而改變A時,可能造成職責2執行錯誤,所以需要將類A分解為A1、A2,
這么說有點抽象,下面用代碼來演示一下,化抽象為具體,
2、代碼對比
代碼一:
public class class01 {
public static void main(String[] args) {
Vehicle1 vehicle = new Vehicle1();
vehicle.run("汽車");
vehicle.run("飛機");
vehicle.run("輪船");
}
}
class Vehicle1 {
public void run(String vehicle){
System.out.println(vehicle + "在高速上行駛");
}
}
通過代碼一可以看出,這樣設計的結果是:飛機和輪船也是在高速上行駛,違反了單一職責原則,這顯然是錯誤的,接下來看看代碼二,與其對比一下,
代碼二:
public class class02 {
public static void main(String[] args) {
VehicleRoad road = new VehicleRoad();
road.run("汽車");
VehicleWater water = new VehicleWater();
water.run("輪船");
}
}
class VehicleRoad {
public void run(String vehicle) {
System.out.println(vehicle + "在高速上行駛");
}
}
class VehicleWater {
public void run(String vehicle) {
System.out.println(vehicle + "在水中行駛");
}
}
通過代碼二,我們根據交通工具運行方法不同,分解成不同的類,解決了代碼一的問題,但是有一個問題就是,這樣很浪費資源,改動很大,貌似不太完美,接下來看看代碼三
代碼三:
public class class03 {
public static void main(String[] args) {
Vehicle2 vehicle = new Vehicle2();
vehicle.runRoad("汽車");
vehicle.runWater("輪船");
}
}
class Vehicle2{
public void runRoad(String vehicle){
System.out.println(vehicle + "在高速上行駛");
}
public void runWater(String vehicle){
System.out.println(vehicle + "在水中行駛");
}
}
代碼三通過修改方法,與代碼二比較,對原來的類沒有做很大的修改,只是增加方法,這里雖然沒有在類這個級別上遵守單一職責原則,但是在方法級別上,仍然是遵守單一職責原則,
3、注意事項和使用細節
要降低類的復雜度,一個類只負責一項職責確保提高類的可讀性,可維護性降低變更引起的風險通常情況下,我們應當遵守單一職責原則,只有邏輯 足夠簡單,才可以在代碼級別違反單一職責原則;只有類中方法數量足夠少,可以在方法級別保持單一職責原則
四、介面隔離原則
1、基本介紹
客戶端不應該依賴它不需要的介面,一個類對另一個類的依賴應該建立在最小的介面上,
先看看一張圖:

代碼一:
public class class01 {
public static void main(String[] args) {
A a = new A();
a.method1(new B());
C c = new C();
c.method2(new D());
}
}
interface Interface1{
void opertion1();
void opertion2();
void opertion3();
void opertion4();
void opertion5();
}
class B implements Interface1{
public void opertion1() {
System.out.println("B 實作了 operation1");
}
public void opertion2() {
System.out.println("B 實作了 operation2");
}
public void opertion3() {
System.out.println("B 實作了 operation3");
}
public void opertion4() {
System.out.println("B 實作了 operation4");
}
public void opertion5() {
System.out.println("B 實作了 operation5");
}
}
class D implements Interface1{
public void opertion1() {
System.out.println("D 實作了 operation1");
}
public void opertion2() {
System.out.println("D 實作了 operation2");
}
public void opertion3() {
System.out.println("D 實作了 operation3");
}
public void opertion4() {
System.out.println("D 實作了 operation4");
}
public void opertion5() {
System.out.println("D 實作了 operation5");
}
}
class A { // A 類 通過介面Interface1 依賴 B類,但只使用1、2、3方法
public void method1(Interface1 interface1){
interface1.opertion1();
}
public void method2(Interface1 interface1){
interface1.opertion2();
}
public void method3(Interface1 interface1){
interface1.opertion3();
}
}
class C { // C 類 通過介面Interface1 依賴 D類,但只使用1、4、5方法
public void method1(Interface1 interface1){
interface1.opertion1();
}
public void method2(Interface1 interface1){
interface1.opertion4();
}
public void method3(Interface1 interface1){
interface1.opertion5();
}
}
代碼一中,類A通過介面Interface1 依賴類B,類C通過介面Interface1 依賴類D,因為介面Interface1 對于類A 和類C來說不是最小介面,所以要想實作圖片的功能,需要實作介面的所有方法,違反了介面隔離原則! 接下來看看代碼二,與其對比一下,
代碼二:
public class class02 {
public static void main(String[] args) {
AA aa = new AA();
aa.method1(new BB());
CC cc = new CC();
cc.method1(new DD());
}
}
interface Interface2{
void opertion1();
}
interface Interface3{
void opertion2();
void opertion3();
}
interface Interface4{
void opertion4();
void opertion5();
}
class BB implements Interface2,Interface3{
public void opertion1() {
System.out.println("BB 實作了operation1");
}
public void opertion2() {
System.out.println("BB 實作了operation2");
}
public void opertion3() {
System.out.println("BB 實作了operation3");
}
}
class DD implements Interface2,Interface4{
public void opertion1() {
System.out.println("DD 實作了operation1");
}
public void opertion4() {
System.out.println("DD 實作了operation4");
}
public void opertion5() {
System.out.println("DD 實作了operation5");
}
}
class AA {
public void method1(Interface2 interface2){
interface2.opertion1();
}
public void method2(Interface3 interface3){
interface3.opertion2();
}
public void method3(Interface3 interface3){
interface3.opertion3();
}
}
class CC {
public void method1(Interface2 interface2){
interface2.opertion1();
}
public void method2(Interface4 interface4){
interface4.opertion4();
}
public void method3(Interface4 interface4){
interface4.opertion5();
}
}
代碼二的UML類圖:

代碼二采用了介面隔離原則,根據實際情況,把代碼一中的介面Interface1拆分成三個介面,即最小介面,這樣就完美的去除了類BB和類DD不用實作他們不需要的方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/404344.html
標籤:java
