美文网首页
Abstract Factory Design Pattern

Abstract Factory Design Pattern

作者: zheting | 来源:发表于2018-03-22 07:27 被阅读5次
    package com.sheting.design.pattern.demo2;
    
    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();
        }
    
    }
    
    
    package com.sheting.design.pattern.demo2;
    
    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;
        }
    
    }
    
    
    package com.sheting.design.pattern.demo2;
    
    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;
        }
    
    }
    
    
    package com.sheting.design.pattern.demo2;
    
    public interface ComputerAbstractFactory {
    
        Computer createComputer();
    }
    
    
    package com.sheting.design.pattern.demo2;
    
    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);
        }
    
    }
    
    
    package com.sheting.design.pattern.demo2;
    
    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);
        }
    
    }
    
    
    package com.sheting.design.pattern.demo2;
    
    public class ComputerFactory {
    
        public static Computer getComputer(ComputerAbstractFactory factory) {
            return factory.createComputer();
        }
    
    }
    
    
    package com.sheting.design.pattern.demo2;
    
    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);
        }
    
    }
    
    

    抽象工厂设计模式在JDK中使用例子

    • javax.xml.parsers.DocumentBuilderFactory#newInstance()
    • javax.xml.transform.TransformerFactory#newInstance()
    • javax.xml.xpath.XPathFactory#newInstance()

    相关文章

      网友评论

          本文标题:Abstract Factory Design Pattern

          本文链接:https://www.haomeiwen.com/subject/zteoqftx.html