天天看點

七大原則01

目錄

    • 2.3單一職責原則
      • 2.3.1基本介紹
      • 2.3.2應用執行個體
      • 2.3.3單一職責原則注意事項和細節
    • 2.4接口隔離原則(Interface Segregation Principle)
      • 2.4.1基本介紹
      • 2.4.2應用執行個體
      • 2.4.3應傳統方法的問題和使用接口隔離原則改進
    • 2.5依賴倒轉原則
      • 2.5.1基本介紹
      • 2.5.2應用執行個體
      • 2.5.3依賴關系傳遞的三種方式和應用案例
      • 2.5.4依賴倒轉原則的注意事項和細節

2.3單一職責原則

2.3.1基本介紹

對類來說的,即一個類應該隻負責一項職責。如類 A 負責兩個不同職責:職責 1,職責 2。當職責 1 需求變更而改變 A 時,可能造成職責 2 執行錯誤,是以需要将類 A 的粒度分解為 A1,A2

2.3.2應用執行個體

以交通工具案例講解

看老師代碼示範

1)方案1 【分析說明】

package com.atguigu.principle.singleresponsibility;

public class SingleResponsibility1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vehicle vehicle = new Vehicle();
		vehicle.run("機車");
		vehicle.run("汽車");
		vehicle.run("飛機");
	}

}

// 交通工具類
// 方式1
// 1. 在方式1 的run方法中,違反了單一職責原則
// 2. 解決的方案非常的簡單,根據交通工具運作方法不同,分解成不同類即可
class Vehicle {
	public void run(String vehicle) {
		System.out.println(vehicle + " 在公路上運作....");
	}
}

           

2)方案2 【分析說明】

package com.atguigu.principle.singleresponsibility;

public class SingleResponsibility2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		RoadVehicle roadVehicle = new RoadVehicle();
		roadVehicle.run("機車");
		roadVehicle.run("汽車");
		
		AirVehicle airVehicle = new AirVehicle();
		
		airVehicle.run("飛機");
	}

}

//方案2的分析
//1. 遵守單一職責原則
//2. 但是這樣做的改動很大,即将類分解,同時修改用戶端
//3. 改進:直接修改Vehicle 類,改動的代碼會比較少=>方案3

class RoadVehicle {
	public void run(String vehicle) {
		System.out.println(vehicle + "公路運作");
	}
}

class AirVehicle {
	public void run(String vehicle) {
		System.out.println(vehicle + "天空運作");
	}
}

class WaterVehicle {
	public void run(String vehicle) {
		System.out.println(vehicle + "水中運作");
	}
}
           

3)方案3 【分析說明】

package com.atguigu.principle.singleresponsibility;

public class SingleResponsibility3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vehicle2 vehicle2  = new Vehicle2();
		vehicle2.run("汽車");
		vehicle2.runWater("輪船");
		vehicle2.runAir("飛機");
	}

}


//方式3的分析
//1. 這種修改方法沒有對原來的類做大的修改,隻是增加方法
//2. 這裡雖然沒有在類這個級别上遵守單一職責原則,但是在方法級别上,仍然是遵守單一職責
class Vehicle2 {
	public void run(String vehicle) {
		//處理
		
		System.out.println(vehicle + " 在公路上運作....");
		
	}
	
	public void runAir(String vehicle) {
		System.out.println(vehicle + " 在天空上運作....");
	}
	
	public void runWater(String vehicle) {
		System.out.println(vehicle + " 在水中行....");
	}
	
	//方法2.
	//..
	//..
	
	//...
}

           

2.3.3單一職責原則注意事項和細節

1)降低類的複雜度,一個類隻負責一項職責。

2)提高類的可讀性,可維護性

3)降低變更引起的風險

4)通常情況下,我們應當遵守單一職責原則,隻有邏輯足夠簡單,才可以在代碼級違反單一職責原則;隻有類中方法數量足夠少,可以在方法級别保持單一職責原則

2.4接口隔離原則(Interface Segregation Principle)

2.4.1基本介紹

1)用戶端不應該依賴它不需要的接口,即一個類對另一個類的依賴應該建立在最小的接口上

2)先看一張圖:

七大原則01

3)類 A 通過接口 Interface1 依賴類 B,類 C 通過接口 Interface1 依賴類 D,如果接口 Interface1 對于類 A 和類 C

來說不是最小接口,那麼類 B 和類 D 必須去實作他們不需要的方法。

4)按隔離原則應當這樣處理:

  • 将接口 Interface1 拆分為獨立的幾個接口(這裡我們拆分成 3 個接口),類 A 和類 C 分别與他們需要的接口建立依賴關系。也就是采用接口隔離原則

2.4.2應用執行個體

1)類 A 通過接口 Interface1 依賴類 B,類 C 通過接口 Interface1 依賴類 D,請編寫代碼完成此應用執行個體。

2)看老師代碼-沒有使用接口隔離原則代碼

package com.atguigu.principle.segregation;

public class Segregation1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

//接口
interface Interface1 {
	void operation1();
	void operation2();
	void operation3();
	void operation4();
	void operation5();
}

class B implements Interface1 {
	public void operation1() {
		System.out.println("B 實作了 operation1");
	}
	
	public void operation2() {
		System.out.println("B 實作了 operation2");
	}
	public void operation3() {
		System.out.println("B 實作了 operation3");
	}
	public void operation4() {
		System.out.println("B 實作了 operation4");
	}
	public void operation5() {
		System.out.println("B 實作了 operation5");
	}
}

class D implements Interface1 {
	public void operation1() {
		System.out.println("D 實作了 operation1");
	}
	
	public void operation2() {
		System.out.println("D 實作了 operation2");
	}
	public void operation3() {
		System.out.println("D 實作了 operation3");
	}
	public void operation4() {
		System.out.println("D 實作了 operation4");
	}
	public void operation5() {
		System.out.println("D 實作了 operation5");
	}
}

class A { //A 類通過接口Interface1 依賴(使用) B類,但是隻會用到1,2,3方法
	public void depend1(Interface1 i) {
		i.operation1();
	}
	public void depend2(Interface1 i) {
		i.operation2();
	}
	public void depend3(Interface1 i) {
		i.operation3();
	}
}
  
class C { //C 類通過接口Interface1 依賴(使用) D類,但是隻會用到1,4,5方法
	public void depend1(Interface1 i) {
		i.operation1();
	}
	public void depend4(Interface1 i) {
		i.operation4();
	}
	public void depend5(Interface1 i) {
		i.operation5();
	}
}
           

2.4.3應傳統方法的問題和使用接口隔離原則改進

1)類 A 通過接口 Interface1 依賴類 B,類 C 通過接口 Interface1 依賴類 D,如果接口 Interface1 對于類 A 和類 C來說不是最小接口,那麼類 B 和類 D 必須去實作他們不需要的方法

2)将接口 Interface1 拆分為獨立的幾個接口,類 A 和類 C 分别與他們需要的接口建立依賴關系。也就是采用接口隔離原則

3)接口 Interface1 中出現的方法,根據實際情況拆分為三個接口

七大原則01

4)代碼實作

package com.atguigu.principle.segregation.improve;

public class Segregation1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 使用一把
		A a = new A();
		a.depend1(new B()); // A類通過接口去依賴B類
		a.depend2(new B());
		a.depend3(new B());

		C c = new C();

		c.depend1(new D()); // C類通過接口去依賴(使用)D類
		c.depend4(new D());
		c.depend5(new D());

	}

}

// 接口1
interface Interface1 {
	void operation1();

}

// 接口2
interface Interface2 {
	void operation2();

	void operation3();
}

// 接口3
interface Interface3 {
	void operation4();

	void operation5();
}

class B implements Interface1, Interface2 {
	public void operation1() {
		System.out.println("B 實作了 operation1");
	}

	public void operation2() {
		System.out.println("B 實作了 operation2");
	}

	public void operation3() {
		System.out.println("B 實作了 operation3");
	}

}

class D implements Interface1, Interface3 {
	public void operation1() {
		System.out.println("D 實作了 operation1");
	}

	public void operation4() {
		System.out.println("D 實作了 operation4");
	}

	public void operation5() {
		System.out.println("D 實作了 operation5");
	}
}

class A { // A 類通過接口Interface1,Interface2 依賴(使用) B類,但是隻會用到1,2,3方法
	public void depend1(Interface1 i) {
		i.operation1();
	}

	public void depend2(Interface2 i) {
		i.operation2();
	}

	public void depend3(Interface2 i) {
		i.operation3();
	}
}

class C { // C 類通過接口Interface1,Interface3 依賴(使用) D類,但是隻會用到1,4,5方法
	public void depend1(Interface1 i) {
		i.operation1();
	}

	public void depend4(Interface3 i) {
		i.operation4();
	}

	public void depend5(Interface3 i) {
		i.operation5();
	}
}
           

2.5依賴倒轉原則

2.5.1基本介紹

依賴倒轉原則(Dependence Inversion Principle)是指:

1)高層子產品不應該依賴低層子產品,二者都應該依賴其抽象

2)抽象不應該依賴細節,細節應該依賴抽象

3)依賴倒轉(倒置)的中心思想是面向接口程式設計

4)依賴倒轉原則是基于這樣的設計理念:相對于細節的多變性,抽象的東西要穩定的多。以抽象為基礎搭建的架構比以細節為基礎的架構要穩定的多。在 java 中,抽象指的是接口或抽象類,細節就是具體的實作類

5)使用接口或抽象類的目的是制定好規範,而不涉及任何具體的操作,把展現細節的任務交給他們的實作類去完成

2.5.2應用執行個體

請程式設計完成 Person 接收消息 的功能。

1)實作方案 1 + 分析說明

package com.atguigu.principle.inversion;

public class DependecyInversion {

	public static void main(String[] args) {
		Person person = new Person();
		person.receive(new Email());
	}

}


class Email {
	public String getInfo() {
		return "電子郵件資訊: hello,world";
	}
}

//完成Person接收消息的功能
//方式1分析
//1. 簡單,比較容易想到
//2. 如果我們擷取的對象是 微信,短信等等,則新增類,同時Perons也要增加相應的接收方法
//3. 解決思路:引入一個抽象的接口IReceiver, 表示接收者, 這樣Person類與接口IReceiver發生依賴
//   因為Email, WeiXin 等等屬于接收的範圍,他們各自實作IReceiver 接口就ok, 這樣我們就符号依賴倒轉原則
class Person {
	public void receive(Email email ) {
		System.out.println(email.getInfo());
	}
}

           

2)實作方案 2(依賴倒轉) + 分析說明

package com.atguigu.principle.inversion.improve;

public class DependecyInversion {

	public static void main(String[] args) {
		//用戶端無需改變
		Person person = new Person();
		person.receive(new Email());
		
		person.receive(new WeiXin());
	}

}

//定義接口
interface IReceiver {
	public String getInfo();
}

class Email implements IReceiver {
	public String getInfo() {
		return "電子郵件資訊: hello,world";
	}
}

//增加微信
class WeiXin implements IReceiver {
	public String getInfo() {
		return "微信資訊: hello,ok";
	}
}

//方式2
class Person {
	//這裡我們是對接口的依賴
	public void receive(IReceiver receiver ) {
		System.out.println(receiver.getInfo());
	}
}

           

2.5.3依賴關系傳遞的三種方式和應用案例

1)接口傳遞

2)構造方法傳遞

3)setter 方式傳遞

4)代碼示範

package com.atguigu.principle.inversion.improve;

public class DependencyPass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ChangHong changHong = new ChangHong();
//		OpenAndClose openAndClose = new OpenAndClose();
//		openAndClose.open(changHong);
		
		//通過構造器進行依賴傳遞
//		OpenAndClose openAndClose = new OpenAndClose(changHong);
//		openAndClose.open();
		//通過setter方法進行依賴傳遞
		OpenAndClose openAndClose = new OpenAndClose();
		openAndClose.setTv(changHong);
		openAndClose.open();

	}

}

// 方式1: 通過接口傳遞實作依賴
// 開關的接口
// interface IOpenAndClose {
// public void open(ITV tv); //抽象方法,接收接口
// }
//
// interface ITV { //ITV接口
// public void play();
// }
// 
// class ChangHong implements ITV {
//
//	@Override
//	public void play() {
//		// TODO Auto-generated method stub
//		System.out.println("長虹電視機,打開");
//	}
//	 
// }
 實作接口
// class OpenAndClose implements IOpenAndClose{
// public void open(ITV tv){
// tv.play();
// }
// }

// 方式2: 通過構造方法依賴傳遞
// interface IOpenAndClose {
// public void open(); //抽象方法
// }
// interface ITV { //ITV接口
// public void play();
// }
// class OpenAndClose implements IOpenAndClose{
// public ITV tv; //成員
// public OpenAndClose(ITV tv){ //構造器
// this.tv = tv;
// }
// public void open(){
// this.tv.play();
// }
// }


// 方式3 , 通過setter方法傳遞
interface IOpenAndClose {
	public void open(); // 抽象方法

	public void setTv(ITV tv);
}

interface ITV { // ITV接口
	public void play();
}

class OpenAndClose implements IOpenAndClose {
	private ITV tv;

	public void setTv(ITV tv) {
		this.tv = tv;
	}

	public void open() {
		this.tv.play();
	}
}

class ChangHong implements ITV {

	@Override
	public void play() {
		// TODO Auto-generated method stub
		System.out.println("長虹電視機,打開");
	}
	 
}
           

2.5.4依賴倒轉原則的注意事項和細節

1)低層子產品盡量都要有抽象類或接口,或者兩者都有,程式穩定性更好.

2)變量的聲明類型盡量是抽象類或接口, 這樣我們的變量引用和實際對象間,就存在一個緩沖層,利于程式擴充和優化

3)繼承時遵循裡氏替換原則