天天看小說

第110章

小A:“工廠方法模式涉及到哪些角色?”

大B:工廠方法模式的角色有:抽象工廠(Creator)角色、具體工廠(ConcreteCreator)角色、抽象產(chǎn)品(Product)角色、具體產(chǎn)品(ConcreteProduct)角色。

抽象工廠(Creator)角色:是工廠方法模式的核心,與應(yīng)用程序無關(guān)。任何在模式中創(chuàng)建的對象的工廠類必須實現(xiàn)這個接口。

具體工廠(ConcreteCreator)角色:這是實現(xiàn)抽象工廠接口的具體工廠類,包含與應(yīng)用程序密切相關(guān)的邏輯,並且受到應(yīng)用程序調(diào)用以創(chuàng)建產(chǎn)品對象。

有兩個這樣的角色:BulbCreator與TubeCreator。

抽象產(chǎn)品(Product)角色:工廠方法模式所創(chuàng)建的對象的超類型,也就是產(chǎn)品對象的共同父類或共同擁有的接口。

具體產(chǎn)品(ConcreteProduct)角色:這個角色實現(xiàn)了抽象產(chǎn)品角色所定義的接口。某具體產(chǎn)品有專門的具體工廠創(chuàng)建,它們之間往往一一對應(yīng)。

//產(chǎn)品Plant接口

publicinterfacePlant{}

//具體產(chǎn)品PlantA,PlantB

publicclassPlantAimplementsPlant{

publicPlantA(){

System.out.println(“createPlantA!”);

}

publicvoiddoSomething(){

System.out.println(“PlantAdosomething……”);

}

}

publicclassPlantBimplementsPlant{

publicPlantB(){

System.out.println(“createPlantB!”);

}

publicvoiddoSomething(){

System.out.println(“PlantBdosomething……”);

}

}

//產(chǎn)品Fruit接口

publicinterfaceFruit{}

//具體產(chǎn)品FruitA,F(xiàn)ruitB

publicclassFruitAimplementsFruit{

publicFruitA(){

System.out.println(“createFruitA!”);

}

publicvoiddoSomething(){

System.out.println(“FruitAdosomething……”);

}

}

publicclassFruitBimplementsFruit{

publicFruitB(){

System.out.println(“createFruitB!”);

}

publicvoiddoSomething(){

System.out.println(“FruitBdosomething……”);

}

}

//抽象工廠方法

publicinterfaceAbstractFactory{

publicPlantcreatePlant();

publicFruitcreateFruit();

}

//具體工廠方法

publicclassFactoryAimplementsAbstractFactory{

publicPlantcreatePlant(){

returnnewPlantA();

}

publicFruitcreateFruit(){

returnnewFruitA();

}

}

publicclassFactoryBimplementsAbstractFactory{

publicPlantcreatePlant(){

returnnewPlantB();

}

publicFruitcreateFruit(){

returnnewFruitB();

}

}

第167章第112章第140章第52章第65章第90章第196章第224章第52章第51章第142章第4章第128章第65章第23章第170章第168章第25章第167章第39章第222章第58章第53章第67章第36章第77章第71章第49章第2章第92章第189章第168章第86章第14章第221章第193章第65章第214章第35章第90章第52章第129章第126章第103章第217章第205章第93章第45章第76章第162章第157章第191章第14章第169章第177章第62章第63章第120章第131章第228章第206章第187章第200章第59章第117章第27章第76章第171章第109章第197章第35章第113章第28章第38章第167章第32章第6章第86章第52章第92章第29章第122章第55章第120章第162章第15章第164章第74章第168章第57章第127章第115章第142章第7章第43章第172章第148章第113章第221章
第167章第112章第140章第52章第65章第90章第196章第224章第52章第51章第142章第4章第128章第65章第23章第170章第168章第25章第167章第39章第222章第58章第53章第67章第36章第77章第71章第49章第2章第92章第189章第168章第86章第14章第221章第193章第65章第214章第35章第90章第52章第129章第126章第103章第217章第205章第93章第45章第76章第162章第157章第191章第14章第169章第177章第62章第63章第120章第131章第228章第206章第187章第200章第59章第117章第27章第76章第171章第109章第197章第35章第113章第28章第38章第167章第32章第6章第86章第52章第92章第29章第122章第55章第120章第162章第15章第164章第74章第168章第57章第127章第115章第142章第7章第43章第172章第148章第113章第221章
主站蜘蛛池模板: 双辽市| 五大连池市| 保亭| 山西省| 鹤庆县| 南昌县| 海淀区| 孟津县| 锡林浩特市| 吴忠市| 三门县| 顺平县| 无为县| 集安市| 武威市| 东宁县| 武川县| 瓦房店市| 通化县| 新沂市| 台前县| 高雄县| 安塞县| 乌恰县| 万载县| 龙游县| 神农架林区| 开封县| 会昌县| 东城区| 双城市| 嘉兴市| 三门峡市| 郸城县| 咸丰县| 调兵山市| 梅河口市| 达州市| 永宁县| 新丰县| 建宁县|