- 模式理解:该模式跟工厂模式的区别就是,工厂模式对应着一个工厂,而抽象工厂模式对应多个工厂,就是说将多个工厂抽象出一个类,先通过这个类来创建对应工厂,然后再通过工厂实现工厂中相应的方法
- 运用场景:两个串联开关的时候使用,无所谓多个工厂类型是否相同
- 代码示例:
//先设计两个接口:
public interface IStore {
void sale();
}
public interface ICompany {
void work();
}
//再创建对应接口的实现类:
public class DianQiStore implements IStore {
@Override
public void sale() {
System.out.println("销售电器");
}
}
public class FuZhuangStore implements IStore {
@Override
public void sale() {
System.out.println("销售服装");
}
}
public class GeLiCompany implements ICompany{
@Override
public void work() {
System.out.println("制造格力空调");
}
}
public class XiaNaiErCompany implements ICompany {
@Override
public void work() {
System.out.println("进行服装设计");
}
}
//抽象出工厂:
public abstract class AbstractFactory {
abstract IStore getStore(String type);
abstract ICompany getCompany(String type);
}
//创建商场工厂和公司工厂,并继承抽象工厂:
public class StoreFactory extends AbstractFactory{
@Override
IStore getStore(String type) {
if (type.equalsIgnoreCase("FZ")) {
return new FuZhuangStore();
}
if (type.equalsIgnoreCase("DQ")) {
return new DianQiStore();
}
return null;
}
@Override
ICompany getCompany(String type) {
return null;
}
}
public class CompanyFactory extends AbstractFactory{
@Override
IStore getStore(String type) {
return null;
}
@Override
ICompany getCompany(String type) {
if (type.equalsIgnoreCase("GL")) {
return new GeLiCompany();
}
if (type.equalsIgnoreCase("XNE")) {
return new XiaNaiErCompany();
}
return null;
}
}
//再设计一个类用于选择工厂类型:
public class SelectFactory {
public AbstractFactory getFactory(String type){
if (type.equalsIgnoreCase("STORE")) {
return new StoreFactory();
}
if (type.equalsIgnoreCase("COMPANY")) {
return new CompanyFactory();
}
return null;
}
}
//最后写个测试类:
public class Main {
public static void main(String[] args) {
SelectFactory selectFactory=new SelectFactory();
selectFactory.getFactory("store").getStore("fz").sale();
selectFactory.getFactory("store").getStore("dq").sale();
selectFactory.getFactory("company").getCompany("xne").work();
selectFactory.getFactory("company").getCompany("gl").work();
}
}
//先设计两个接口:
public interface IStore {
void sale();
}
public interface ICompany {
void work();
}
//再创建对应接口的实现类:
public class DianQiStore implements IStore {
@Override
public void sale() {
System.out.println("销售电器");
}
}
public class FuZhuangStore implements IStore {
@Override
public void sale() {
System.out.println("销售服装");
}
}
public class GeLiCompany implements ICompany{
@Override
public void work() {
System.out.println("制造格力空调");
}
}
public class XiaNaiErCompany implements ICompany {
@Override
public void work() {
System.out.println("进行服装设计");
}
}
//抽象出工厂:
public abstract class AbstractFactory {
abstract IStore getStore(String type);
abstract ICompany getCompany(String type);
}
//创建商场工厂和公司工厂,并继承抽象工厂:
public class StoreFactory extends AbstractFactory{
@Override
IStore getStore(String type) {
if (type.equalsIgnoreCase("FZ")) {
return new FuZhuangStore();
}
if (type.equalsIgnoreCase("DQ")) {
return new DianQiStore();
}
return null;
}
@Override
ICompany getCompany(String type) {
return null;
}
}
public class CompanyFactory extends AbstractFactory{
@Override
IStore getStore(String type) {
return null;
}
@Override
ICompany getCompany(String type) {
if (type.equalsIgnoreCase("GL")) {
return new GeLiCompany();
}
if (type.equalsIgnoreCase("XNE")) {
return new XiaNaiErCompany();
}
return null;
}
}
//再设计一个类用于选择工厂类型:
public class SelectFactory {
public AbstractFactory getFactory(String type){
if (type.equalsIgnoreCase("STORE")) {
return new StoreFactory();
}
if (type.equalsIgnoreCase("COMPANY")) {
return new CompanyFactory();
}
return null;
}
}
//最后写个测试类:
public class Main {
public static void main(String[] args) {
SelectFactory selectFactory=new SelectFactory();
selectFactory.getFactory("store").getStore("fz").sale();
selectFactory.getFactory("store").getStore("dq").sale();
selectFactory.getFactory("company").getCompany("xne").work();
selectFactory.getFactory("company").getCompany("gl").work();
}
}