欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java设计模式六(抽象工厂模式)

程序员文章站 2024-01-21 18:40:04
...

java设计模式—抽象工厂模式

一.概述

抽象工厂模式是在工厂模式的基础上,加上了一个管理工厂的超级工厂。也就是说,工厂的作用是管理各种有相似功能的类,而超级工厂的作用是管理这些工厂。

二.模式示例:

在上一节里,我们以调用打印机为例,讲解了工厂模式的具体实现过程,但是,如果除了调用打印机,还有扫描仪或者复印机,这种情况下单纯的工厂模式就难以对付,下面我们就用抽象工厂模式来实现系统除了惠普和联想打印机,还有方正和佳能扫描仪的情况。
1.创建打印机接口

public interface Printer {
    public void print();
}

2.创建实现具体打印功能的惠普打印机类

public class HpPrinter implements Printer{

    @Override
    public void print() {
        System.out.println("This is HP printer");
    }
}

3.创建实现具体打印功能的联想打印机类

public class LenovoPrinter  implements Printer{

    @Override
    public void print() {
        System.out.println("This is Lenovo printer");
    }
}

4.创建创建扫描仪接口

public interface Scanner {
    public void scan();
}

5.创建实现具体扫描功能的佳能扫描仪类

public class CanonScanner implements Scanner{

    @Override
    public void scan() {
        System.out.println("this is CanonScanner");
    }
}

6.创建实现具体扫描功能的方正扫描仪类

public class FounderScanner implements Scanner{

    @Override
    public void scan() {
        System.out.println("this is FounderScanner");
    }
}

7.创建具有打印功能和扫描功能的抽象工厂类

public abstract class DeviceFactory {
    public abstract Printer getPrinter(String printName);
    public abstract Scanner getScanner(String scannerName);
}

8.创建打印机工厂

public class PrinterFactory extends DeviceFactory{
    public Printer getPrinter(String printerName) {
        if ("hp".equalsIgnoreCase(printerName)) {
            return new HpPrinter();
        } else if ("lenovo".equalsIgnoreCase(printerName)) {
            return new LenovoPrinter(); 
        } else {
            return null;
        }
    }

    @Override
    public Scanner getScanner(String scannerName) {     
        return null;
    }   
}

9.创建扫描仪工厂

public class ScannerFactory extends DeviceFactory{

    @Override
    public Printer getPrinter(String printName) {       
        return null;
    }

    @Override
    public Scanner getScanner(String scannerName) {
        if ("Founder".equalsIgnoreCase(scannerName)) {
            return new FounderScanner();
        } else if ("Canon".equalsIgnoreCase(scannerName)) {
            return new CanonScanner(); 
        } else {
            return null;
        }
    }
}

10.创建获取打印机和扫描仪的工厂类

public class DispatchFactory {
    public static DeviceFactory getFactory(String device){
          if(device.equalsIgnoreCase("Printer")){
             return new PrinterFactory();
          } else if(device.equalsIgnoreCase("Scanner")){
             return new ScannerFactory();
          }
          return null;
       }
}

11.模式调用:实现的过程有些复杂,但是调用却变得非常简单。

public class Test {
    public static void main(String args[]){
        DeviceFactory printerFactory = DispatchFactory.getFactory("Printer");
        Printer hpPrinter = printerFactory.getPrinter("hp");
        Printer lenovoPrinter = printerFactory.getPrinter("lenovo");
        hpPrinter.print();
        lenovoPrinter.print();

        DeviceFactory scannerFactory = DispatchFactory.getFactory("Scanner");
        Scanner founderScanner = scannerFactory.getScanner("founder");
        Scanner canonScanner = scannerFactory.getScanner("canon");
        founderScanner.scan();
        canonScanner.scan();
    }
}