• 抽象工厂与工厂方法的区别


    抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

    举一个电脑产品的例子吧:

    IBMDell都是著名的计算机生产厂家,他们采用的主板、硬盘及CPU是不相同的,但是配件间、主板与CPU一定要相互兼容,例如下面例子中的微星MSIK7N2G配AMD的CPU;微星MSI865PE配Intel的CPU。

    如图中所示,ComputerFactory是抽象工厂,DellIBM是生产产品的工厂;CPU、HardDisk、MainBoard是抽象产品,CPU的型号又分许多种。具体实现见代码:

    //首先,定义CPU的接口:
    
    public interface CPU{
    
        public String getCPU();
    
    }
    
    //定义AMD类,实现CPU接口:
    
    public class AMD implements CPU{
    
        public String getCPU(){
    
            return “Athlon XP 2008+”;
    
        }
    
    //定义Intel类,实现CPU接口:
    
    public class Intel implements CPU{
    
        public String getCPU(){
    
            return “奔腾4 3.2C”;
    
        }
    
    }
    
    //定义硬盘接口:
    
    public interface HardDisk{
    
        public String getSize();
    
    }
    
    //定义Maxtor类,实现硬盘接口:
    
    public class Maxtor implements HardDisk{
    
        public String getSize(){
    
            return “MaXLine Plus II 200G”;
    
        }
    
    }
    
    //定义WestDigit,实现硬盘接口:
    
    public class WestDigit implements HardDisk{
    
        public String getSize(){
    
            return “WD2500JD 250G”;
    
        }
    
    }
    
    //定义主板的接口,包含参数为CPU的公共方法Attach():
    
    public interface MainBoard{
    
        public void Attach(CPU cpu) throws Exception;
    
    }
    
    //主板微星MSI865PE,支持Intel的CPU:
    
    public class MSI865PE implements MainBoard{
    
        public void Attach(CPU cpu) throws Exception{
    
            if(cpu.getClass ().toString ().endsWith(“Intel”)){
    
                System.out.println(“MSI865PE”);
    
            }
    
             else{
    
                throw new Exception(“主板MSI865PE只能配Intel的                                        CPU”);
    
            }
    
        }
    
    }
    
    //主板微星MSIK7N2G,支持AMD的CPU:
    
    public class MSIK7N2G implements MainBoard{
    
        public void Attach(CPU cpu) throws Exception{
    
            if(cpu.getClass ().toString ().endsWith(“AMD”)){
    
                System.out.println(“MSIK7N2G”);
    
            }
    
            else{
    
                throw new Exception(“主板MSIK7N2G只能配AMD的CPU”);
    
            }
    
        }
    
    }
    
    //定义抽象电脑工厂类:
    
    public abstract class ComputerFactory{
    
        CPU cpu;
    
        HardDisk hd;
    
        MainBoard mb;
    
    public void show(){
    
            try{
    
                System.out.println(this.getClass().getName() .toString () + (“生产的电脑配置”);
    
                System.out.println (“CPU:” + cpu.getCPU ());
    
                System.out.println (“HardDisk:” + hd.getSize ());
    
                System.out.print (“MainBoard:”);
    
                mb.Attach.(cpu);
    
            }
    
    catch(Exception e){
    
                System.err.println(e.getMessage());
    
            }
    
        }
    
    }
    
    //抽象电脑工厂类派生类IBM,定义其返回的系列配件产品:
    
    public class IBM extends ComputerFactory{
    
        IBM(){
    
            cpu = new Intel();
    
            hd = new WestDigit();
    
            mb = new MSI865PE();
    
        }
    
    }
    
    //抽象电脑工厂类派生类Dell,定义其返回的系列配件产品:
    
    public class Dell extends ComputerFactory{
    
        Dell(){
    
            cpu = new AMD();
    
            hd = new Maxtor();
    
            mb = new MSIK7N2G();
    
        }
    
    }
    
    //客户程序调用:
    
    Public class Client{
    
        public static void main(String argv[]){
    
            IBM ibm = new IBM();
    
            ibm.show();
    
            Dell dell = new Dell();
    
            dell.show();
    
        }
    
    }

    输出结果为:

    Computerworld.IBM生产的电脑配置

    CPU:奔腾4 3.2C

    HardDisk:WD2500JD 250G

    MainBoard:MSI865PE

    Computerworld.Dell生产的电脑配置

    CPU:Athlon XP 2800+

    HardDisk:MaXLine Plus II 200G

    MainBoard:MSIK7N2G

    工厂方法模式:
    一个抽象产品类,可以派生出多个具体产品类。
    一个抽象工厂类,可以派生出多个具体工厂类。
    每个具体工厂类只能创建一个具体产品类的实例。

    抽象工厂模式:
    多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。
    一个抽象工厂类,可以派生出多个具体工厂类。
    每个具体工厂类可以创建多个具体产品类的实例。

    区别:
    工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。
    工厂方法模式的具体工厂类只能创建一个具体产品类的实例,而抽象工厂模式可以创建多个。
  • 相关阅读:
    一文快速入门分库分表(必修课)
    MySql分库分表与分区的区别和思考
    常用分库分表方案汇总
    分区分表分库
    MySQL分区和分表
    MySQL的聚集索引和非聚集索引
    PHP大文件上传支持断点上传组件
    PHP大文件上传支持断点上传工具
    Nginx大文件上传支持断点上传
    百度WebUploader大文件上传支持断点上传
  • 原文地址:https://www.cnblogs.com/cangqiongbingchen/p/4727999.html
Copyright © 2020-2023  润新知