此文全篇圍繞“是什麼”和“爲什麼”
在列舉設計模式之前,我們首先要明白兩個問題:
(1)什麼是設計模式?
(2)爲什麼要用設計模式?
以下內容爲詳解?
1.什麼是設計模式?
設計模式值軟件開發人員在軟件開發過程中面臨的一般問題的解決方案,這些解決方案是衆多軟件開發人員經過相當長的的一端時間的實驗和錯誤總結出來的。
2.爲什麼要用設計模式?
設計模式是一套被反覆使用的、多數人知曉的、經過分類編目的、代碼設計經驗的總結。使用設計模式是爲了重用代碼、讓代碼更容易被他人理解、保證代碼的可靠性。
3.Java 中常用的設計模式,將以下圖爲基準作以詳細的介紹:
首先介紹工廠模式(簡單工廠模式,工廠方法模式、抽象工廠模式):
(1)什麼是簡單工廠模式?
簡單工廠模式:專門定義一個類用來創建其他類的實例化對象,被創建的實例通常都有共同的父類
思考場景1:李某想去手機商城買一步手機,此時呢,有兩款手機李某比較喜歡,一部是HUAWEI,一部是XiaoMi
基於以上場景,結合簡單工廠模式的設計思想,類圖如下:
代碼參考:
//抽象產品類
interface Mobile{
void printMobile();
}
//具體產品類HUAWEI、XiaoMi
class HUAWEI implements Computer1{
public void printComputer(){
System.out.println("this is HUAWEI");
}
}
class XiaoMi implements Mobile{
public void printComputer(){
System.out.println("this is XiaoMi");
}
}
//一個手機工廠
class MobileFactory1{
public static Computer1 getInstance(String type){
Mobile mobile=null;
if(type.equals("HUAWEI")){
mobile=new HUAWEI();
}else if(type.equals("XiaoMI")){
mobile=new XiaoMi();
}
return monile;
}
}
public class TestFactory{
public static void buy(Mobile mobile){
mobile.printComputer();
}
public static void main(String[] args) {
TestFactory testFactory=new TestFactory();
Scanner scanner=new Scanner(System.in);
System.out.println("請輸入您想要的手機型號...");
String type=scanner.nextLine();
Mobile mobile= MobileFactory1.getInstance(type);
testFactory.buy(mobile);
}
}
簡單工廠模式的優點:
i)簡單,易於實現;ii)把類的實例化交給工廠,易於實現
缺點:添加具體產品需要修改工廠,違反了OCP開放封閉原則
(2)什麼是工廠方法模式?
工廠方法模式:定義一個用來創建創建對象的接口,讓子類決定實例化哪一個類,讓子類決定實例化延遲到子類
設計思想:針對每一個產品提供一個工廠類,在客戶端中判斷使用哪個工廠去創建一個對象
基於思考場景一:我們可以形象有一個總工長專門負責生產各種各樣的手機,而這個工廠裏邊有兩個子工廠:HUAWEI和XiaoMi,如果李某想要HUAWEI,就交由HUAWEI這個子工廠去生產(即創建對象),想要XiaoMi就交由XiaoMi這個子工廠去生產
設計類圖:
參考代碼:
interface Mobile{
void printMobile();
}
class HUAWEI implements Mobile{
public void printComputer(){
System.out.println("this is HUAWEI");
}
}
class XiaoMi implements XiaoMi{
public void printComputer(){
System.out.println("this is XiaoMi");
}
}
interface MobileFactory{
Mobile creat();
}
class HUAWEIFactory implements MobileFactory{
public Mobile creat(){
return new HUAWEI();
}
}
class XiaoMiFactory implements MobileFactory{
public Mobile creat(){
return new Surface();
}
}
public class TestFactory{
public void buyMobile(Mobile mobile){
mobile.printMObile();
}
public static void main(String[] args) {
TestFactory testFactory=new TestFactory();
MobileFactory mobileFactory=new HUAWEIFactory();
testFactory.buyMobile(mobileFactory.creat());
}
}
工廠方法模式的優點:
i)降低了代碼的耦合度,對象的生成交給子類完成;
ii)實現了開放封閉原則,每次添加子產品不需要修改原來的代碼;
缺點:
i)增加了代碼量,每個具體產品都需要一個具體工廠
ii)當增加抽象產品也就是添加一個其他產品族,需要修改工廠,違背OCP
(3)什麼是抽象工程模式?
抽象工廠模式:提供一個創建一系列相關或互相依賴對象的接口,而無需指定它們的具體類
思考場景二:某工廠要生產兩款電腦:Macbook Pro/Surfacebook,這是候負責該工廠的產品經理說要生產新的一類產品操作系統Mac Os 和Windows 8。
設計類圖:
參考代碼:
interface Computer1{
void printComputer();
}
class MacBook implements Computer1{
public void printComputer(){
System.out.println("this is Mac");
}
}
class SurfaceBook implements Computer1{
public void printComputer(){
System.out.println("this is a surfaceBook");
}
}
interface OperatingSystem{
void printSystem();
}
class MacOsSystem implements OperatingSystem{
public void printSystem(){
System.out.println("this is a Mac os");
}
}
class Windows implements OperatingSystem{
public void printSystem() {
System.out.println("this is a windows 8");
}
}
interface productionFactory{
Computer1 createComputer();
OperatingSystem createSystem();
}
class AppleFactory implements productionFactory{
public Computer1 createComputer(){
return new MacBook();
}
public OperatingSystem createSystem(){
return new MacOsSystem();
}
}
class MsFactory implements productionFactory{
public Computer1 createComputer() {
return new SurfaceBook();
}
public OperatingSystem createSystem() {
return new Windows();
}
}
public class TestFactory{
public void buyComputer(Computer1 computer1){
computer1.printComputer();
}
public void use(OperatingSystem operatingSystem){
operatingSystem.printSystem();
}
public static void main(String[] args) {
TestFactory testFactory=new TestFactory();
productionFactory factory=new AppleFactory();
Computer1 computer1=factory.createComputer();
OperatingSystem system=factory.createSystem();
testFactory.buyComputer(computer1);
testFactory.use(system);
}
}
優點:
i)代碼解耦,滿足OCP開放封閉原則
ii)抽象工廠模式中我們可以定義實現不止一個接口,一個工廠也可以生成不止一個產品類,對於複雜對象的生產相當靈活易擴展;
缺點:
抽象工廠模式是工廠方法模式的擴展,擴展產品族的時候,代碼量會成倍增加,會使整個代碼顯得很笨重,並且會違反OCP,因爲要修改所有的工廠。
2.代理設計模式:
什麼是代理設計模式?
兩個類共同實現一個接口,其中一個子類負責真實業務的實現,另外一個子類輔助真實業務主題的操作(類似於生活中代購的的例子)
舉個例子:你想買一臺外星人筆記本,但是你又不知道購買渠道,這時,你將你的想法告訴代理,有代理去完成這些操作
參考代碼:
interface Isubject{
public void buyComputer();
}
class RealSubject implements Isubject{
public void buyComputer(){
System.out.println("買一臺外星人電腦");
}
}
class ProxySubject implements Isubject{
private Isubject subject;
public ProxySubject(Isubject subject){
this.subject=subject;
}
public void produceComputer(){
System.out.println("1.生產外星人電腦");
}
public void afterSale(){
System.out.println("3.外星人電腦售後團隊");
}
public void buyComputer() {
this.produceComputer();
this.subject.buyComputer();
this.afterSale();
}
}
class Factory{
public static Isubject getInstance(){
return new ProxySubject(new RealSubject());
}
}
public class TestFactory{
public static void main(String[] args) {
Isubject subject=Factory.getInstance();
subject.buyComputer();
}
}
單例:一個類只允許產生一個實例化對象
(1)餓漢式單例:不管是否使用戶Singleton類的對愛性,只要該類加載了,那麼一定會 自動創建一個公共的instance對象。
參考代碼:
lass Singleton{
private static Singleton instance=new Singleton();
private Singleton(){
//構造方私有化化,外部無法調用構造方法產生實例化對象
//所以應該在類的內部產生實例化對象
}
public final static Singleton getInstance(){
return instance;
}
public void print(){
System.out.println("hello world");
}
}
public class TestSingleton{
public static void main(String[] args) {
Singleton singleton=null;//聲明對象
singleton=Singleton.getInstance();
singleton.print();
}
}
懶漢式單例: 當第一次使用Singleton對象的時候纔會爲其產生實例化對象的操作
參考代碼:
class Singleton{
private static Singleton instance;
private Singleton(){
}
public static Singleton getInstance(){
if(instance==null){
instance=new Singleton();
}
return instance;
}
public void print(){
System.out.println("hello world");
}
}
public class TestSingleton{
public static void main(String[] args) {
Singleton singleton=null;
singleton=Singleton.getInstance();
singleton.print();
}
}
單例模式的特點: 構造方法私有化, 外部無法產生新的實例化對象, 只能通過static方法取得實例化對象
此文全篇圍繞“是什麼”和“爲什麼”
在列舉設計模式之前,我們首先要明白兩個問題:
(1)什麼是設計模式?
(2)爲什麼要用設計模式?
以下內容爲詳解?
1.什麼是設計模式?
設計模式值軟件開發人員在軟件開發過程中面臨的一般問題的解決方案,這些解決方案是衆多軟件開發人員經過相當長的的一端時間的實驗和錯誤總結出來的。
2.爲什麼要用設計模式?
設計模式是一套被反覆使用的、多數人知曉的、經過分類編目的、代碼設計經驗的總結。使用設計模式是爲了重用代碼、讓代碼更容易被他人理解、保證代碼的可靠性。
3.Java 中常用的設計模式,將以下圖爲基準作以詳細的介紹:
首先介紹工廠模式(簡單工廠模式,工廠方法模式、抽象工廠模式):
(1)什麼是簡單工廠模式?
簡單工廠模式:專門定義一個類用來創建其他類的實例化對象,被創建的實例通常都有共同的父類
思考場景1:李某想去手機商城買一步手機,此時呢,有兩款手機李某比較喜歡,一部是HUAWEI,一部是XiaoMi
基於以上場景,結合簡單工廠模式的設計思想,類圖如下:
代碼參考:
//抽象產品類
interface Mobile{
void printMobile();
}
//具體產品類HUAWEI、XiaoMi
class HUAWEI implements Computer1{
public void printComputer(){
System.out.println("this is HUAWEI");
}
}
class XiaoMi implements Mobile{
public void printComputer(){
System.out.println("this is XiaoMi");
}
}
//一個手機工廠
class MobileFactory1{
public static Computer1 getInstance(String type){
Mobile mobile=null;
if(type.equals("HUAWEI")){
mobile=new HUAWEI();
}else if(type.equals("XiaoMI")){
mobile=new XiaoMi();
}
return monile;
}
}
public class TestFactory{
public static void buy(Mobile mobile){
mobile.printComputer();
}
public static void main(String[] args) {
TestFactory testFactory=new TestFactory();
Scanner scanner=new Scanner(System.in);
System.out.println("請輸入您想要的手機型號...");
String type=scanner.nextLine();
Mobile mobile= MobileFactory1.getInstance(type);
testFactory.buy(mobile);
}
}
簡單工廠模式的優點:
i)簡單,易於實現;ii)把類的實例化交給工廠,易於實現
缺點:添加具體產品需要修改工廠,違反了OCP開放封閉原則
(2)什麼是工廠方法模式?
工廠方法模式:定義一個用來創建創建對象的接口,讓子類決定實例化哪一個類,讓子類決定實例化延遲到子類
設計思想:針對每一個產品提供一個工廠類,在客戶端中判斷使用哪個工廠去創建一個對象
基於思考場景一:我們可以形象有一個總工長專門負責生產各種各樣的手機,而這個工廠裏邊有兩個子工廠:HUAWEI和XiaoMi,如果李某想要HUAWEI,就交由HUAWEI這個子工廠去生產(即創建對象),想要XiaoMi就交由XiaoMi這個子工廠去生產
設計類圖:
參考代碼:
interface Mobile{
void printMobile();
}
class HUAWEI implements Mobile{
public void printComputer(){
System.out.println("this is HUAWEI");
}
}
class XiaoMi implements XiaoMi{
public void printComputer(){
System.out.println("this is XiaoMi");
}
}
interface MobileFactory{
Mobile creat();
}
class HUAWEIFactory implements MobileFactory{
public Mobile creat(){
return new HUAWEI();
}
}
class XiaoMiFactory implements MobileFactory{
public Mobile creat(){
return new Surface();
}
}
public class TestFactory{
public void buyMobile(Mobile mobile){
mobile.printMObile();
}
public static void main(String[] args) {
TestFactory testFactory=new TestFactory();
MobileFactory mobileFactory=new HUAWEIFactory();
testFactory.buyMobile(mobileFactory.creat());
}
}
工廠方法模式的優點:
i)降低了代碼的耦合度,對象的生成交給子類完成;
ii)實現了開放封閉原則,每次添加子產品不需要修改原來的代碼;
缺點:
i)增加了代碼量,每個具體產品都需要一個具體工廠
ii)當增加抽象產品也就是添加一個其他產品族,需要修改工廠,違背OCP
(3)什麼是抽象工程模式?
抽象工廠模式:提供一個創建一系列相關或互相依賴對象的接口,而無需指定它們的具體類
思考場景二:某工廠要生產兩款電腦:Macbook Pro/Surfacebook,這是候負責該工廠的產品經理說要生產新的一類產品操作系統Mac Os 和Windows 8。
設計類圖:
參考代碼:
interface Computer1{
void printComputer();
}
class MacBook implements Computer1{
public void printComputer(){
System.out.println("this is Mac");
}
}
class SurfaceBook implements Computer1{
public void printComputer(){
System.out.println("this is a surfaceBook");
}
}
interface OperatingSystem{
void printSystem();
}
class MacOsSystem implements OperatingSystem{
public void printSystem(){
System.out.println("this is a Mac os");
}
}
class Windows implements OperatingSystem{
public void printSystem() {
System.out.println("this is a windows 8");
}
}
interface productionFactory{
Computer1 createComputer();
OperatingSystem createSystem();
}
class AppleFactory implements productionFactory{
public Computer1 createComputer(){
return new MacBook();
}
public OperatingSystem createSystem(){
return new MacOsSystem();
}
}
class MsFactory implements productionFactory{
public Computer1 createComputer() {
return new SurfaceBook();
}
public OperatingSystem createSystem() {
return new Windows();
}
}
public class TestFactory{
public void buyComputer(Computer1 computer1){
computer1.printComputer();
}
public void use(OperatingSystem operatingSystem){
operatingSystem.printSystem();
}
public static void main(String[] args) {
TestFactory testFactory=new TestFactory();
productionFactory factory=new AppleFactory();
Computer1 computer1=factory.createComputer();
OperatingSystem system=factory.createSystem();
testFactory.buyComputer(computer1);
testFactory.use(system);
}
}
優點:
i)代碼解耦,滿足OCP開放封閉原則
ii)抽象工廠模式中我們可以定義實現不止一個接口,一個工廠也可以生成不止一個產品類,對於複雜對象的生產相當靈活易擴展;
缺點:
抽象工廠模式是工廠方法模式的擴展,擴展產品族的時候,代碼量會成倍增加,會使整個代碼顯得很笨重,並且會違反OCP,因爲要修改所有的工廠。
2.代理設計模式:
什麼是代理設計模式?
兩個類共同實現一個接口,其中一個子類負責真實業務的實現,另外一個子類輔助真實業務主題的操作(類似於生活中代購的的例子)
舉個例子:你想買一臺外星人筆記本,但是你又不知道購買渠道,這時,你將你的想法告訴代理,有代理去完成這些操作
參考代碼:
interface Isubject{
public void buyComputer();
}
class RealSubject implements Isubject{
public void buyComputer(){
System.out.println("買一臺外星人電腦");
}
}
class ProxySubject implements Isubject{
private Isubject subject;
public ProxySubject(Isubject subject){
this.subject=subject;
}
public void produceComputer(){
System.out.println("1.生產外星人電腦");
}
public void afterSale(){
System.out.println("3.外星人電腦售後團隊");
}
public void buyComputer() {
this.produceComputer();
this.subject.buyComputer();
this.afterSale();
}
}
class Factory{
public static Isubject getInstance(){
return new ProxySubject(new RealSubject());
}
}
public class TestFactory{
public static void main(String[] args) {
Isubject subject=Factory.getInstance();
subject.buyComputer();
}
}
單例:一個類只允許產生一個實例化對象
(1)餓漢式單例:不管是否使用戶Singleton類的對愛性,只要該類加載了,那麼一定會 自動創建一個公共的instance對象。
參考代碼:
lass Singleton{
private static Singleton instance=new Singleton();
private Singleton(){
//構造方私有化化,外部無法調用構造方法產生實例化對象
//所以應該在類的內部產生實例化對象
}
public final static Singleton getInstance(){
return instance;
}
public void print(){
System.out.println("hello world");
}
}
public class TestSingleton{
public static void main(String[] args) {
Singleton singleton=null;//聲明對象
singleton=Singleton.getInstance();
singleton.print();
}
}
懶漢式單例: 當第一次使用Singleton對象的時候纔會爲其產生實例化對象的操作
參考代碼:
class Singleton{
private static Singleton instance;
private Singleton(){
}
public static Singleton getInstance(){
if(instance==null){
instance=new Singleton();
}
return instance;
}
public void print(){
System.out.println("hello world");
}
}
public class TestSingleton{
public static void main(String[] args) {
Singleton singleton=null;
singleton=Singleton.getInstance();
singleton.print();
}
}
單例模式的特點: 構造方法私有化, 外部無法產生新的實例化對象, 只能通過static方法取得實例化對象