工厂模式和抽象工厂模式

1 工厂模式

关于工厂模式可以参考我的一篇博文 https://blog.csdn.net/GoSaint/article/details/100118814 。

工厂模式其实应该叫做工厂方法模式。指的是用工厂方法来创建对象。而不是使用构造器!该工厂类根据提供的输入返回不同的子类,并且工厂类使用if-else或switch语句来实现此目的!一般普遍的实现方式要点如下:

1 创建接口

2 实现接口的不同子类

3 创建工厂以及工厂方法,根据传递的参数或者类型返回具体的接口对象。

2 抽象工厂

在Abstract Factory模式中,我们摆脱了if-else块,并为每个子类都有一个工厂类。然后是一个抽象工厂类,它将基于输入工厂类返回子类。

2.1抽象工厂设计模式超类和子类

public abstract class Computer {
     
    public abstract String getRAM();
    public abstract String getHDD();
    public abstract String getCPU();
     
    @Override
    public String toString(){
        return "RAM= "+this.getRAM()+", HDD="+this.getHDD()+", CPU="+this.getCPU();
    }
}

 

public class PC extends Computer {
 
    private String ram;
    private String hdd;
    private String cpu;
     
    public PC(String ram, String hdd, String cpu){
        this.ram=ram;
        this.hdd=hdd;
        this.cpu=cpu;
    }
    @Override
    public String getRAM() {
        return this.ram;
    }
 
    @Override
    public String getHDD() {
        return this.hdd;
    }
 
    @Override
    public String getCPU() {
        return this.cpu;
    }
 
}
public class Server extends Computer {
 
    private String ram;
    private String hdd;
    private String cpu;
     
    public Server(String ram, String hdd, String cpu){
        this.ram=ram;
        this.hdd=hdd;
        this.cpu=cpu;
    }
    @Override
    public String getRAM() {
        return this.ram;
    }
 
    @Override
    public String getHDD() {
        return this.hdd;
    }
 
    @Override
    public String getCPU() {
        return this.cpu;
    }
 
}

2.2每个子类的工厂类

首先,我们需要创建一个Abstract Factory接口或抽象类

public interface ComputerAbstractFactory {

	public Computer createComputer();

}

注意,该createComputer()方法正在返回超类的实例Computer。现在,我们的工厂类将实现此接口并返回其各自的子类。

public class PCFactory implements ComputerAbstractFactory {

	private String ram;
	private String hdd;
	private String cpu;
	
	public PCFactory(String ram, String hdd, String cpu){
		this.ram=ram;
		this.hdd=hdd;
		this.cpu=cpu;
	}
	@Override
	public Computer createComputer() {
		return new PC(ram,hdd,cpu);
	}

}

同样,我们将为Server子类提供一个工厂类

public class ServerFactory implements ComputerAbstractFactory {

	private String ram;
	private String hdd;
	private String cpu;
	
	public ServerFactory(String ram, String hdd, String cpu){
		this.ram=ram;
		this.hdd=hdd;
		this.cpu=cpu;
	}
	
	@Override
	public Computer createComputer() {
		return new Server(ram,hdd,cpu);
	}

}

现在,我们将创建一个消费者类,该类将为客户端类创建子类提供入口

public class ComputerFactory {

	public static Computer getComputer(ComputerAbstractFactory factory){
		return factory.createComputer();
	}
}

测试:

public class TestDesignPatterns {

	public static void main(String[] args) {
		testAbstractFactory();
	}

	private static void testAbstractFactory() {
		Computer pc = ComputerFactory.getComputer(new PCFactory("2 GB","500 GB","2.4 GHz"));
		Computer server = ComputerFactory.getComputer(new ServerFactory("16 GB","1 TB","2.9 GHz"));
		System.out.println("AbstractFactory PC Config::"+pc);
		System.out.println("AbstractFactory Server Config::"+server);
	}
}

3 抽象工厂和工厂的区别

  1. 工厂模式是基于方法的抽象。而抽象工厂是基于接口的抽象
  2. 普通工厂产出是一个产品(实例),抽象工厂产出是一个抽象(接口)。区别在于,若添加一个新的产品,前者是修改工厂,后者是创建新工厂(符合“闭合原则”)。

4 JDK中的工厂模式可抽象工厂模式

 4.1工厂方法:java.util.Calendar ;UML图如下:

                                    

public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
}

1 跟踪getInstance()方法;

 public static Calendar getInstance(){
        return createCalendar(TimeZone.getDefault(),     
                              Locale.getDefault(Locale.Category.FORMAT));
}

2 跟踪createCalendar()方法;

根据不同的时区或者地区创建对应的Calender对象。

这里和标准的工厂方法设计模式稍微有点区别,没有工厂类。但是Calender这个抽象类的createCalendar()方法进行了不同子类的实现。

4.2  抽象工厂 javax.xml.parsers.DocumentBuilderFactory

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值