设计模式-抽象工厂模式实例-01

设计模式-抽象工厂模式实例-01

要点:

需要一个工厂生产多个产品对象。

优点:

隔离了具体类的生成,使客户端不知道什么被创建;

保证客户端始终只使用一个产品族的对象;

增加新的产品族很方便,符合开闭原则。

缺点:

增加新的产品等级结构很复杂,不符合开闭原则。

适用环境:

一个系统不应当依赖于产品类的实例如何被创建、组合和表达的细节;

系统中有多个产品族但每次只使用其中某一产品族;

属于同一个产品族的产品将在一起被使用;

产品等级结构稳定。

静态工厂模式、工厂方法模式、抽象工厂模式对比:

 

 

 

实例1:海尔工厂和TCL工厂生产电视和空调。

类图

 

 

代码实现:

代码结构:

 

 

factory:

EFactory.java:

import abstractTVFactory.airConditioner.AirConditioner;
import abstractTVFactory.television.Television;

//抽象工厂类
public interface EFactory {
    public Television produceTelevision();
    public AirConditioner produceAirConditioner();
}

 

HaierFactory.java:

import abstractTVFactory.airConditioner.AirConditioner;
import abstractTVFactory.airConditioner.HaierAirConditioner;
import abstractTVFactory.television.HaierTelevision;
import abstractTVFactory.television.Television;

//具体工厂类HaierFactory
public class HaierFactory implements EFactory {
    public Television produceTelevision(){
        return new HaierTelevision();
    }
    public AirConditioner produceAirConditioner(){
        return new HaierAirConditioner();
    }
}

 

TCLFactory.java:

import abstractTVFactory.airConditioner.AirConditioner;
import abstractTVFactory.airConditioner.TCLAirConditioner;
import abstractTVFactory.television.TCLTelevision;
import abstractTVFactory.television.Television;

//具体工厂类TCLFactory
public class TCLFactory implements EFactory {
    public Television produceTelevision(){
        return new TCLTelevision();
    }
    public AirConditioner produceAirConditioner(){
        return new TCLAirConditioner();
    }
}

 

 

television:

Television.java:

 

//抽象产品类
public interface Television {
    public void play();
}

 

HaierTelevision.java:

 

//具体产品类HaierTV
public class HaierTelevision implements Television {
    public void play(){
        System.out.println("Haier TV is playing");
    }
}

 

TCLTelevision.java:

 

//具体产品类TCLTV
public class TCLTelevision implements Television {
    public void play(){
        System.out.println("TCL TV is playing");
    }
}

 

 

 

airConditioner:

AirConditioner.java:

 

//抽象工厂类AirConditioner
public interface AirConditioner {
    public void use();
}

 

HaierAirConditioner.java:

 

//具体产品类Haier空调
public class HaierAirConditioner implements AirConditioner {
    public void use(){
        System.out.println("Haier AirConditioner is using");
    }
}

 

TCLAirConditioner.java:

 

//具体产品类TCL空调
public class TCLAirConditioner implements AirConditioner {
    public void use(){
        System.out.println("TCL AirConditioner is using");
    }
}

 

 

 

utils:

XMLUtil.java:

 

import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.SAXException;
import java.io.*;

public class XMLUtil {
    public static Object getBean(){
        try {
            //创建文档对象
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc;
            doc = builder.parse(new File("src/abstractTVFactory/utils/config.xml"));
            //获取包含支付方式名称的文本节点
            NodeList nl = doc.getElementsByTagName("className");
            Node classNode=nl.item(0).getFirstChild();
            String cName=classNode.getNodeValue();
            //通过类名生成实例对象并将其返回
            Class c=Class.forName(cName);
            Object obj=c.newInstance();
            return obj;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}

 

config.xml:

 

<?xml version="1.0"  ?>
<config>
    <className>abstractTVFactory.factory.TCLFactory</className>
</config>

 

Client:

Client.java:

 

import abstractTVFactory.airConditioner.AirConditioner;
import abstractTVFactory.factory.EFactory;
import abstractTVFactory.television.Television;
import abstractTVFactory.utils.XMLUtil;

public class Client {
    public static void main(String[] args) {
        try{
            EFactory factory;
            Television tv;
            AirConditioner airConditioner;
            factory = (EFactory) XMLUtil.getBean();
            tv = factory.produceTelevision();
            tv.play();

            airConditioner = factory.produceAirConditioner();
            airConditioner.use();

        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}

 

 运行结果:

 

 



 
 
 
 
posted @ 2022-03-09 19:39  临易  阅读(92)  评论(0编辑  收藏  举报