抽象工廠模式(Abstract Factory)
工廠方法模式:
一個抽象產(chǎn)品類,可以派生出多個具體產(chǎn)品類。
一個抽象工廠類,可以派生出多個具體工廠類。
每個具體工廠類只能創(chuàng)建一個具體產(chǎn)品類的實(shí)例。
抽象工廠模式:
多個抽象產(chǎn)品類,每個抽象產(chǎn)品類可以派生出多個具體產(chǎn)品類。
一個抽象工廠類,可以派生出多個具體工廠類。
每個具體工廠類可以創(chuàng)建多個具體產(chǎn)品類的實(shí)例。
區(qū)別:
工廠方法模式只有一個抽象產(chǎn)品類,而抽象工廠模式有多個。
工廠方法模式的具體工廠類只能創(chuàng)建一個具體產(chǎn)品類的實(shí)例,而抽象工廠模式可以創(chuàng)建多個。
產(chǎn)品
- 父類
package com.ghg.data_structure.mode.abstractfactory;
/**
* 門口A
* @author Administrator
*
*/
public interface IProductA {
public void doWork();
}
package com.ghg.data_structure.mode.abstractfactory;
/**
* 產(chǎn)品B
* @author Administrator
*
*/
public interface IProductB {
public void doWork();
}
- A產(chǎn)品實(shí)現(xiàn)
package com.ghg.data_structure.mode.abstractfactory;
/**
* 產(chǎn)品子類A1
* @author Administrator
*
*/
public class ProductA1 implements IProductA {
public ProductA1() {
super();
System.out.println("ProductA1 ");
}
public void doWork() {
System.out.println("ProductA1 doWork");
}
}
package com.ghg.data_structure.mode.abstractfactory;
/**
* 產(chǎn)品子類A1
* @author Administrator
*
*/
public class ProductA2 implements IProductA {
public ProductA2() {
super();
System.out.println("ProductA2 ");
}
public void doWork() {
System.out.println("ProductA2 doWork");
}
}
- B產(chǎn)品實(shí)現(xiàn)
package com.ghg.data_structure.mode.abstractfactory;
/**
* 產(chǎn)品2
* @author Administrator
*
*/
public class ProductB1 implements IProductB {
public ProductB1() {
super();
System.out.println(" ProductB1 ");
}
public void doWork() {
System.out.println("ProductB1 doWork");
}
}
package com.ghg.data_structure.mode.abstractfactory;
/**
* 產(chǎn)品2
*
* @author Administrator
*
*/
public class ProductB2 implements IProductB {
public ProductB2() {
super();
System.out.println(" ProductB2 ");
}
public void doWork() {
System.out.println("ProductB2 doWork");
}
}
工廠
- 接口
package com.ghg.data_structure.mode.abstractfactory.factory;
import com.ghg.data_structure.mode.abstractfactory.IProductA;
import com.ghg.data_structure.mode.abstractfactory.IProductB;
/**
* 工廠一
* @author Administrator
*
*/
public interface Factory1 {
/**
* 生產(chǎn)產(chǎn)品1
* @return
*/
public IProductA getProductA1();
/**
* 生產(chǎn)產(chǎn)品2
* @return
*/
public IProductB getProductB1();
}
package com.ghg.data_structure.mode.abstractfactory.factory;
import com.ghg.data_structure.mode.abstractfactory.IProductA;
import com.ghg.data_structure.mode.abstractfactory.IProductB;
/**
* 工廠一
* @author Administrator
*
*/
public interface Factory2 {
/**
* 生產(chǎn)產(chǎn)品1
* @return
*/
public IProductA getProductA2();
/**
* 生產(chǎn)產(chǎn)品2
* @return
*/
public IProductB getProductB2();
}
- A接口工廠實(shí)現(xiàn)
package com.ghg.data_structure.mode.abstractfactory.factory;
import com.ghg.data_structure.mode.abstractfactory.IProductA;
import com.ghg.data_structure.mode.abstractfactory.IProductB;
import com.ghg.data_structure.mode.abstractfactory.ProductA1;
import com.ghg.data_structure.mode.abstractfactory.ProductB1;
public class ConcreteFactory1 implements Factory1 {
public IProductA getProductA1() {
/**
* 產(chǎn)品a1
*/
return new ProductA1();
}
public IProductB getProductB1() {
/**
* 產(chǎn)品B1
*/
return new ProductB1();
}
}
- B接口工廠實(shí)現(xiàn)
package com.ghg.data_structure.mode.abstractfactory.factory;
import com.ghg.data_structure.mode.abstractfactory.IProductA;
import com.ghg.data_structure.mode.abstractfactory.IProductB;
import com.ghg.data_structure.mode.abstractfactory.ProductA2;
import com.ghg.data_structure.mode.abstractfactory.ProductB2;
public class ConcreteFactory2 implements Factory2 {
public IProductA getProductA2() {
/**
* 產(chǎn)品a2
*/
return new ProductA2();
}
public IProductB getProductB2() {
/**
* 產(chǎn)品B2
*/
return new ProductB2();
}
}
測試
package com.ghg.data_structure.mode.abstractfactory;
import com.ghg.data_structure.mode.abstractfactory.factory.ConcreteFactory1;
import com.ghg.data_structure.mode.abstractfactory.factory.ConcreteFactory2;
import com.ghg.data_structure.mode.abstractfactory.factory.Factory1;
import com.ghg.data_structure.mode.abstractfactory.factory.Factory2;
public class Test1 {
public static void main(String[] args) {
Factory1 factory1 = new ConcreteFactory1();
IProductA productA1 = factory1.getProductA1();
IProductB productB1 = factory1.getProductB1();
productA1.doWork();
productB1.doWork();
Factory2 factory2 = new ConcreteFactory2();
IProductA productA2 = factory2.getProductA2();
IProductB productB2 = factory2.getProductB2();
productA2.doWork();
productB2.doWork();
}
}
結(jié)果
ProductA1
ProductB1
ProductA1 doWork
ProductB1 doWork
ProductA2
ProductB2
ProductA2 doWork
ProductB2 doWork