輕松掌握J(rèn)ava工廠模式、抽象工廠模式
在面向?qū)ο缶幊痰某绦蛟O(shè)計(jì)中,我們最常見的操作就是new對象,但在創(chuàng)建一個新對象的過程中,會有一些問題,比如我們需要注意創(chuàng)建新對象的實(shí)現(xiàn)細(xì)節(jié),初始化一些必要的參數(shù)等。這樣會讓我們在講更多的心思放在對象的創(chuàng)建上,而不是程序邏輯的實(shí)現(xiàn)上,嚴(yán)重拖延了我們的程序開發(fā)效率。工廠模式和抽象工廠模式的出現(xiàn)則完美解決了這個問題,讓我們不再關(guān)心對象的創(chuàng)建,更多的在重心放在業(yè)務(wù)的實(shí)現(xiàn)上。
特點(diǎn):
1、程序員直接通過工廠方法創(chuàng)建對象,不再關(guān)注創(chuàng)建對象的細(xì)節(jié)。
2、隱藏對象的實(shí)現(xiàn)細(xì)節(jié),也有利于程序的安全性。
3、降低程序耦合度。
企業(yè)級開發(fā)和常見框架中的應(yīng)用:
Hibernate中的sessionfactory等
工廠模式分類:
簡單工廠模式,程序開發(fā)中最常用的形式,具體代碼如下:
public class Demo {
/**
* demo這個類就是我們平時的操作類,在這個類中我們不用去關(guān)心 創(chuàng)建汽車的實(shí)現(xiàn)細(xì)節(jié)
*/
public static void main(String[] args) {
Car car = CarFactory.createCar("dz");
car.run();
Car car2 = CarFactory.createCar("at");
car2.run();
}
}
interface Car{
public void run();
}
class Dz implements Car{
public void run() {
System.out.println("大眾汽車在跑");
}
}
class At implements Car{
public void run() {
System.out.println("奧拓汽車在跑");
}
}
class CarFactory{
public static Car createCar(String type){
if("dz".equals(type)){
System.out.println("創(chuàng)建了一個大眾車");
return new Dz();
}
if("at".equals(type)){
System.out.println("創(chuàng)建了一個奧拓車");
return new At();
}
return null;
}
}
工廠方法模式,相比于簡單工廠模式,方便擴(kuò)展,不必去修改以前的代碼
public class Demo {
/**
* demo這個類就是我們平時的操作類,在這個類中我們不用去關(guān)心 創(chuàng)建汽車的實(shí)現(xiàn)細(xì)節(jié)
*/
public static void main(String[] args) {
AtFactory atFactory = new AtFactory();
DzFactory dzFactory = new DzFactory();
Car at = atFactory.createCar();
Car dz = dzFactory.createCar();
at.run();
dz.run();
}
}
interface Car {
public void run();
}
class Dz implements Car {
public void run() {
System.out.println("大眾汽車在跑");
}
}
class At implements Car {
public void run() {
System.out.println("奧拓汽車在跑");
}
}
interface CarFactory {
Car createCar();
}
class DzFactory implements CarFactory {
public Car createCar() {
return new Dz();
}
}
class AtFactory implements CarFactory {
public Car createCar() {
return new At();
}
}
抽象工廠方法模式:
public class Demo {
public static void main(String[] args) {
Car carFactory = new GDCarFactory();
FDZ fdz = carFactory.createFdz();
fdz.zhuansu();
}
}
interface FDZ {
void zhuansu();
}
class GDFDZ implements FDZ {
public void zhuansu() {
System.out.println("高端發(fā)動機(jī)轉(zhuǎn)速快");
}
}
class DDFDZ implements FDZ {
public void zhuansu() {
System.out.println("低端發(fā)動機(jī)轉(zhuǎn)速慢");
}
}
interface ZY {
void shushidu();
}
class GDZY implements ZY {
public void shushidu() {
System.out.println("高端座椅很舒適");
}
}
class DDZY implements ZY {
public void shushidu() {
System.out.println("低端座椅不舒適");
}
}
interface LT {
void mosundu();
}
class GDLT implements LT {
public void mosundu() {
System.out.println("高端輪胎不磨損");
}
}
class DDLT implements LT {
public void mosundu() {
System.out.println("低端輪胎磨損快");
}
}
interface Car {
FDZ createFdz();
ZY createZy();
LT createLt();
}
class GDCarFactory implements Car{
@Override
public FDZ createFdz() {
return new GDFDZ();
}
@Override
public ZY createZy() {
return new GDZY();
}
@Override
public LT createLt() {
return new GDLT();
}
}
class DDCarFactory implements Car{
@Override
public FDZ createFdz() {
return new DDFDZ();
}
@Override
public ZY createZy() {
return new DDZY();
}
@Override
public LT createLt() {
return new DDLT();
}
}
三種方法的比較:
1、簡單工廠模式:簡單工廠模式設(shè)計(jì)簡單,代碼量少,但是可擴(kuò)展性卻很差,需要擴(kuò)展時需要修改以前的代碼
2、工廠方法模式:擴(kuò)展性強(qiáng),但增加了代碼復(fù)雜度
3、抽象工廠模式:抽象工廠模式和工廠模式是不同,抽象工廠模式是對產(chǎn)品分等級,但工廠模式是對產(chǎn)品分類,舉個汽車的例子:工廠模式是生產(chǎn)不同品種的汽車,比如奧迪和大眾,而抽象工廠模式則是對同一款汽車進(jìn)行等級劃分,比如同樣都是大眾汽車,我們分了高端車和低端車。從方法上講抽象工廠模式更像是工廠模式的細(xì)化。一個針對的不不同產(chǎn)品,一個針對的是同一個產(chǎn)品家族。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
G1垃圾回收器在并發(fā)場景調(diào)優(yōu)詳解
這篇文章主要為大家介紹了G1垃圾回收器在并發(fā)場景調(diào)優(yōu)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
利用IDEA社區(qū)版創(chuàng)建SpringBoot項(xiàng)目的詳細(xì)圖文教程
大家應(yīng)該都知道Idea社區(qū)版本,默認(rèn)是不能創(chuàng)建SpringBoot項(xiàng)目的,下面這篇文章主要給大家介紹了關(guān)于利用IDEA社區(qū)版創(chuàng)建SpringBoot項(xiàng)目的詳細(xì)圖文教程,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
SpringBoot應(yīng)用中出現(xiàn)的Full GC問題的場景與解決
這篇文章主要為大家詳細(xì)介紹了SpringBoot應(yīng)用中出現(xiàn)的Full GC問題的場景與解決方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
Java中session存儲Users對象實(shí)現(xiàn)記住密碼
這篇文章主要介紹了Java中session存儲Users對象實(shí)現(xiàn)記住密碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
springboot2版本無法加載靜態(tài)資源問題解決
這篇文章主要介紹了springboot2版本無法加載靜態(tài)資源問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
SpringBoot實(shí)現(xiàn)給屬性賦值的兩種方式
在Spring Boot中,配置文件是用來設(shè)置應(yīng)用程序的各種參數(shù)和操作模式的重要部分,Spring Boot支持兩種主要類型的配置文件:properties文件和YAML 文件,這兩種文件都可以用來定義相同的配置,接下來由小編給大家詳細(xì)的介紹一下這兩種方式2024-07-07
java?對象實(shí)例化過程中的多態(tài)特性解析
這篇文章主要介紹了java?對象實(shí)例化過程中的多態(tài)特性解析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
java.lang.StackOverflowError出現(xiàn)的原因及解決
這篇文章主要介紹了java.lang.StackOverflowError出現(xiàn)的原因及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

