日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java创新型模式_java设计模式--创建型模式(一)

發布時間:2023/12/2 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java创新型模式_java设计模式--创建型模式(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2016-04-24 10:10:34

創建型模式:工廠方法模式、抽象工廠模式、單例模式、建造者模式、原型模式

注意:工廠模式可以分為三類:?1)簡單工廠模式(Simple Factory)?2)工廠方法模式(Factory Method) 3)抽象工廠模式(Abstract Factory)

這三種模式從上到下逐步抽象,并且更具一般性。

而GOF在《設計模式》一書中將工廠模式分為兩類:工廠方法模式(Factory Method)與抽象工廠模式(Abstract Factory),這是將簡單工廠模式(Simple Factory)看成了工廠方法模式的一種特例,兩者歸為了一類。

工廠方法模式:

(1)簡單工廠模式(又叫靜態工廠方法模式)

通過專門定義一個類來負責創建其他類的實例,被創建的實例通常都具有共同的父類。

模式中包含的角色及其職責:

1.工廠(Creator)角色

簡單工廠模式的核心,它負責實現創建所有實例的內部邏輯。工廠類可以被外界直接調用,創建所需的產品對象。

2.抽象(Product)角色

簡單工廠模式所創建的所有對象的父類,它負責描述所有實例所共有的公共接口。

3.具體產品(Concrete Product)角色

簡單工廠模式所創建的具體實例對象

例子:采集水果,如蘋果、香蕉

Apple和Banana是具體產品角色;Fruit是抽象角色,是Apple和Banana的公共接口;FruitFactory是工廠角色,負責創建Apple和Banana實例。

public interfaceFruit {/** 采集*/

public voidget();

}

public class Apple implementsFruit{/** 采集*/

public voidget(){

System.out.println("采集蘋果");

}

}

public class Banana implementsFruit{/** 采集*/

public voidget(){

System.out.println("采集香蕉");

}

public classFruitFactory {/** 獲得Apple類的實例*/

public staticFruit getApple() {return newApple();

}/** 獲得Banana類實例*/

public staticFruit getBanana() {return newBanana();

}

}

public classMainClass {public static voidmain(String[] args) {

Fruit apple=FruitFactory.getApple();

Fruit banana=FruitFactory.getBanana();

apple.get();

banana.get();

}

還可以如下修改FruitFactory類

public classFruitFactory {/** getFruit方法,獲得所有產品對象*/

public static Fruit getFruit(String type) throwsInstantiationException, IllegalAccessException, ClassNotFoundException {

Class fruit=Class.forName(type);return(Fruit) fruit.newInstance();

}

}

這樣動態的加載和創建Class類,但是沒有注意大小寫;

進一步修改為:

public classFruitFactory {/** getFruit方法,獲得所有產品對象*/

public static Fruit getFruit(String type) throwsInstantiationException, IllegalAccessException, ClassNotFoundException {if(type.equalsIgnoreCase("apple")) {return Apple.class.newInstance();

}else if(type.equalsIgnoreCase("banana")) {return Banana.class.newInstance();

}else{

System.out.println("找不到相應的實例化類");return null;

}

}

}

然后

public classMainClass {public static void main(String[] args) throwsInstantiationException, IllegalAccessException, ClassNotFoundException {

Fruit apple= FruitFactory.getFruit("Apple");

Fruit banana= FruitFactory.getFruit("Banana");

apple.get();

banana.get();

}

}

簡單工廠模式的優缺點:

在這個模式中,工廠類是整個模式的關鍵所在。它包含必要的判斷邏輯,能夠根據外界給定的信息,決定究竟應該創建哪個具體類的對象。用戶在使用時可以直接根據工廠類去創建所需的實例,而無需了解這些對象是如何創建以及如何組織的。有利于整個軟件體系結構的優化。

不難發現,簡單工廠模式的缺點也正體現在其工廠類上,由于工廠類集中了所有實例的創建邏輯,所以“高內聚”方面做的并不好。另外,當系統中的具體產品類不斷增多時,可能會出現要求工廠類也要做相應的修改,擴展性并不很好。例如增加一種水果-梨子,需要在FruitFactory中繼續增加else if語句,不符合開放封閉原則。這時候考慮下面的工廠方法模式。

(2)工廠方法模式(又叫多態工廠模式)

工廠方法模式的意義是定義一個創建產品對象的工廠接口,將實際創建工作推遲到子類當中。核心工廠類不再負責產品的創建,這樣核心類成為一個抽象工廠角色,僅負責具體工廠子類必須實現的接口,這樣進一步抽象化的好處是使得工廠方法模式可以使系統在不修改具體工廠角色的情況下引進新的產品。

模式中包含的角色及其職責:

1.抽象工廠(Creator)角色

工廠方法模式的核心,任何工廠類都必須實現這個接口。

2.具體工廠( Concrete

Creator)角色

具體工廠類是抽象工廠的一個實現,負責實例化產品對象。

3.抽象(Product)角色

工廠方法模式所創建的所有對象的父類,它負責描述所有實例所共有的公共接口。

4.具體產品(Concrete Product)角色

工廠方法模式所創建的具體實例對象

例子:采集水果,如蘋果、香蕉、梨子

Apple、Banana、Pear是具體產品角色;Fruit是抽象角色,是Apple、Banana、Pear的公共接口。他們的構造和前面簡單工廠模式中一樣。

AppleFactory、BananaFactory、PearFactory是具體工廠角色,負責創建Apple、Banana、Pear的實例;FruitFactory是抽象工廠角色,是AppleFactory、BananaFactory、PearFactory的公共接口。

public interfaceFruitFactory {//水果廠

publicFruit getFruit();

}

public class AppleFactory implementsFruitFactory {//蘋果廠,返回蘋果實例

publicFruit getFruit() {return newApple();

}

}

public class BananaFactory implementsFruitFactory {//香蕉廠,返回香蕉實例

publicFruit getFruit() {return newBanana();

}

}

public class PearFactory implementsFruitFactory {//梨子廠,返回梨子實例

publicFruit getFruit() {return newPear();

}

}

public classMainClass {public static voidmain(String[] args) {//獲得AppleFactory

FruitFactory ff = newAppleFactory();//通過AppleFactory來獲得Apple實例對象

Fruit apple =ff.getApple();

apple.get();//獲得BananaFactory

FruitFactory ff2 = newBananaFactory();//通過BananaFactory來獲得Banana實例對象

Fruit banana =ff2.getBanana();

banana.get();//獲得PearFactory

FruitFactory ff3 = newPearFactory();//通過PearFactory來獲得Pear實例對象

Fruit pear =ff3.getPear();

pear.get();

}

}

和簡單工廠模式比較:工廠方法類的核心是一個抽象工廠類,而簡單工廠模式把核心放在一個具體類上。

工廠方法模式之所以有一個別名叫多態性工廠模式是因為具體工廠類都有共同的接口,或者有共同的抽象父類。

當系統擴展需要添加新的產品對象時,僅僅需要添加一個具體對象以及一個具體工廠對象,原有工廠對象不需要進行任何修改,也不需要修改客戶端,很好的符合了“開放-封閉”原則。而簡單工廠模式在添加新產品對象后不得不修改工廠方法,擴展性不好。工廠方法模式退化后可以演變成簡單工廠模式。

抽象工廠模式

抽象工廠模式是所有形態的工廠模式中最為抽象和最其一般性的。抽象工廠模式可以向客戶端提供一個接口,使得客戶端在不必指定產品的具體類型的情況下,能夠創建多個產品族的產品對象。

Apple、Pear屬于產品等級結構;Apple又分南方Apple和北方Apple;Pear也分南方Pear和北方Pear。

一個工廠負責生產南方的Apple、Pear,這是一個產品族;一個工廠負責生產北方的Apple、Pear,這是另一個產品族。

模式中包含的角色及其職責:

1.抽象工廠(Creator)角色

抽象工廠模式的核心,包含對多個產品結構的聲明,任何工廠類都必須實現這個接口。

2.具體工廠( Concrete? Creator)角色

具體工廠類是抽象工廠的一個實現,負責實例化某個產品族中的產品對象。

3.抽象(Product)角色

抽象模式所創建的所有對象的父類,它負責描述所有實例所共有的公共接口。

4.具體產品(Concrete Product)角色

抽象模式所創建的具體實例對象

注意:抽象工廠中方法對應產品結構,具體工廠對應產品族。

FruitFactory是抽象工廠角色,里面聲明了蘋果和梨子;SouthFactory和NorthFactory是具體工廠角色,SouthFactory返回南方蘋果和梨子實例,NorthFactory返回北方蘋果和梨子實例。

public interfaceFruit {/** 采集*/

public voidget();

}

public abstract class Apple implementsFruit{/** 采集蘋果*/

public abstract voidget();

}

public abstract class Pear implementsFruit{/** 采集梨子*/

public abstract voidget();

}

public class SouthApple extendsApple {public voidget() {

System.out.println("采集南方蘋果");

}

}

public class NorthApple extendsApple {public voidget() {

System.out.println("采集北方蘋果");

}

}

public class SouthPear extendsPear {public voidget() {

System.out.println("采集南方梨子");

}

}

public class NorthPear extendsPear {public voidget() {

System.out.println("采集北方梨子");

}

}

public interfaceFruitFactory {//實例化Apple

publicFruit getApple();//實例化Pear

publicFruit getPear();

}

public class SouthFruitFactory implementsFruitFactory {publicFruit getApple() {return newSouthApple();

}publicFruit getPear() {return newSouthPear();

}

}

public class NorthFruitFactory implementsFruitFactory {publicFruit getApple() {return newNorthApple();

}publicFruit getPear() {return newNorthPear();

}

}

public classMainClass {public static voidmain(String[] args) {

FruitFactory ff= newNorthFruitFactory();

Fruit apple=ff.getApple();

apple.get();

Fruit banana=ff.getPear();

pear.get();

FruitFactory ff2= newSouthFruitFactory();

Fruit apple2=ff2.getApple();

apple2.get();

Fruit banana2=ff2.getPear();

pear2.get();

}

}

如果再增加一個產品族:溫室水果,包括溫室蘋果和溫室梨子,只要增加相應的溫室蘋果類、溫室梨子類和溫室工廠類,符合“開放-封閉“”原則。但是若增加的是一個產品,如香蕉,則會違反該原則。

補充:工廠模式在開發中的應用

一個簡單的計算器(主要是加法運算):

OperationFactory是抽象工廠類,AddOperationFactory是具體工廠類;Operation是抽象角色類;AddOperation和SubtractionOperation是具體角色類。

public abstract classOperation {private doublenum1;private doublenum2;public doublegetNum1() {returnnum1;

}public void setNum1(doublenum1) {this.num1 =num1;

}public doublegetNum2() {returnnum2;

}public void setNum2(doublenum2) {this.num2 =num2;

}public abstract doublegetResult();

}

public class AddOperation extendsOperation {public doublegetResult() {double result = this.getNum1() + this.getNum2();returnresult;

}

}

public class SubtractionOperation extendsOperation {public doublegetResult() {double result = this.getNum1() - this.getNum2();returnresult;

}

}

public interfaceOperationFactory {publicOperation getOperation();

}

public class AddOperationFactory implementsOperationFactory{publicOperation getOperation() {return newAddOperation();

}

}

importjava.util.Scanner;public classMainClass {public static voidmain(String[] args) {//1.接受控制臺輸入

System.out.println("---計算器程序---");

System.out.println("輸入第一個操作數");

Scanner scanner= newScanner(System.in);

String strNum1=scanner.nextLine();

System.out.println("輸入運算符");

String oper=scanner.nextLine();

System.out.println("輸入第二個操作數");

String strNum2=scanner.nextLine();double result = 0;double num1 =Double.parseDouble(strNum1);double num2 =Double.parseDouble(strNum2);//2.進行運算

if("+".equals(oper)) {

OperationFactory factory= newAddOperationFactory();

Operation operation=factory.getOperation();

operation.setNum1(num1);

operation.setNum2(num2);

result=operation.getResult();

}//3.返回結果

System.out.println(strNum1 + oper + strNum2 + "=" +result);

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Java创新型模式_java设计模式--创建型模式(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。