1. 抽象工廠模式的介紹
抽象工廠模式
(Abstract Factory Pattern)屬于創(chuàng)建型模式,是圍繞一個(gè)超級工廠創(chuàng)建其他工廠。該超級工廠又稱為其他工廠的工廠。
在抽象工廠模式
中,接口是負(fù)責(zé)創(chuàng)建一個(gè)相關(guān)對象的工廠,不需要顯式指定它們的類。每個(gè)生成的工廠都能按照工廠模式提供對象。
通過使用抽象工廠模式
,可以將客戶端與具體產(chǎn)品的創(chuàng)建過程解耦,使得客戶端可以通過工廠接口來創(chuàng)建一族產(chǎn)品。
抽象工廠模式
包含4個(gè)核心角色:文章來源:http://www.zghlxwxcb.cn/news/detail-858103.html
- 抽象工廠(Abstract Factory):聲明了一組用于創(chuàng)建產(chǎn)品對象的方法,每個(gè)方法對應(yīng)一種產(chǎn)品類型。抽象工廠可以是接口或抽象類。
- 具體工廠(Concrete Factory):實(shí)現(xiàn)了抽象工廠接口,負(fù)責(zé)創(chuàng)建具體產(chǎn)品對象的實(shí)例。
- 抽象產(chǎn)品(Abstract Product):定義了一組產(chǎn)品對象的共同接口或抽象類,描述了產(chǎn)品對象的公共方法。
- 具體產(chǎn)品(Concrete Product):實(shí)現(xiàn)了抽象產(chǎn)品接口,定義了具體產(chǎn)品的特定行為和屬性。
2. 抽象工廠模式的類圖
文章來源地址http://www.zghlxwxcb.cn/news/detail-858103.html
3. 抽象工廠模式的實(shí)現(xiàn)
3.1 創(chuàng)建摩托車的接口
package blog;
/**
* 摩托車
*/
public interface Motorcycle {
void by();
}
3.2 創(chuàng)建摩托車的具體實(shí)現(xiàn)
package blog;
/**
* 踏板摩托車
*/
public class Scooter implements Motorcycle{
@Override
public void by() {
System.out.println("騎著一輛踏板車");
}
}
package blog;
/**
* 彎梁摩托車
*/
public class UnderBone implements Motorcycle{
@Override
public void by() {
System.out.println("騎著一輛彎梁車");
}
}
3.3 創(chuàng)建汽車的接口
package blog;
/**
* 汽車
*/
public interface Car {
void drive();
}
3.4 創(chuàng)建汽車的具體產(chǎn)品
package blog;
/**
* suv
*/
public class Suv implements Car{
@Override
public void drive() {
System.out.println("開著一輛SUV");
}
}
package blog;
/**
* mpv
*/
public class Mpv implements Car{
@Override
public void drive() {
System.out.println("開著一輛MPV");
}
}
3.5 創(chuàng)建抽象工廠
package blog;
/**
* 抽象工廠
*/
public interface AbstractFactory {
Car getCar(String type);
Motorcycle getMotorCycle(String type);
}
3.6 創(chuàng)建具體工廠
package blog;
/**
* 摩托車工廠
*/
public class MotorCycleFactory implements AbstractFactory {
@Override
public Car getCar(String type) {
return null;
}
@Override
public Motorcycle getMotorCycle(String type) {
try {
Class<?> aClass = Class.forName(type);
return (Motorcycle) aClass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
package blog;
/**
* 汽車工廠
*/
public class CarFactory implements AbstractFactory {
@Override
public Car getCar(String type) {
try {
Class<?> aClass = Class.forName(type);
return (Car)aClass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public Motorcycle getMotorCycle(String type) {
return null;
}
}
3.7 創(chuàng)建工廠生成器
package blog;
/**
* 工廠生成器
*/
public class FactoryProducer {
public static AbstractFactory getFactory(String type) {
if ("car".equals(type)) {
return new CarFactory();
}
if ("motorcycle".equals(type)) {
return new MotorCycleFactory();
}
return null;
}
}
3.8 使用工廠生成器獲取工廠,通過工廠獲取產(chǎn)品
package blog;
public class AbstractFactoryDemo {
public static void main(String[] args) {
// 獲取汽車工廠
AbstractFactory carFactory = FactoryProducer.getFactory("car");
// 獲取suv
Car suv = carFactory.getCar("blog.Suv");
suv.drive();
// 獲取mpv
Car mpv = carFactory.getCar("blog.Mpv");
mpv.drive();
// 獲取摩托車工廠
AbstractFactory motorcycleFactory = FactoryProducer.getFactory("motorcycle");
// 獲取scooter
Motorcycle scooter = motorcycleFactory.getMotorCycle("blog.Scooter");
scooter.by();
// 獲取UnderBone
Motorcycle underBone = motorcycleFactory.getMotorCycle("blog.UnderBone");
underBone.by();
}
}
到了這里,關(guān)于設(shè)計(jì)模式(三):抽象工廠模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!