1. public abstract class Computer {
    2. public abstract String getRAM();
    3. public abstract String getHDD();
    4. public abstract String getCPU();
    5. @Override
    6. public String toString() {
    7. return "RAM= " + this.getRAM() + ", HDD=" + this.getHDD() + ", CPU=" + this.getCPU();
    8. }
    9. }
    1. public class PC extends Computer{
    2. private String ram;
    3. private String hdd;
    4. private String cpu;
    5. public PC() {
    6. }
    7. public PC(String ram, String hdd, String cpu) {
    8. this.ram = ram;
    9. this.hdd = hdd;
    10. this.cpu = cpu;
    11. }
    12. @Override
    13. public String getRAM() {
    14. return this.ram;
    15. }
    16. @Override
    17. public String getHDD() {
    18. return this.hdd;
    19. }
    20. @Override
    21. public String getCPU() {
    22. return this.cpu;
    23. }
    24. }
    1. public class Server extends Computer {
    2. private String ram;
    3. private String hdd;
    4. private String cpu;
    5. public Server() {
    6. }
    7. public Server(String ram, String hdd, String cpu) {
    8. this.ram = ram;
    9. this.hdd = hdd;
    10. this.cpu = cpu;
    11. }
    12. @Override
    13. public String getRAM() {
    14. return this.ram;
    15. }
    16. @Override
    17. public String getHDD() {
    18. return this.hdd;
    19. }
    20. @Override
    21. public String getCPU() {
    22. return this.cpu;
    23. }
    24. }
    1. public class ComputerFactory {
    2. /*使用 字符串 + 分支选择,来创建指定的子对象*/
    3. public static Computer getComputer(String type, String ram, String hdd, String cpu) {
    4. if ("PC".equalsIgnoreCase(type)) {
    5. return new PC(ram, hdd, cpu);
    6. } else if ("Server".equalsIgnoreCase(type)) {
    7. return new Server(ram, hdd, cpu);
    8. }
    9. return null;
    10. }
    11. /*使用反射调用无参构造函数创建对象*/
    12. public static Computer getComputer(Class<? extends Computer> classType) {
    13. System.out.println("classType.getName() = " + classType.getName());
    14. Computer obj = null;
    15. try {
    16. obj = (Computer) Class.forName(classType.getName()).getDeclaredConstructor().newInstance();
    17. } catch (InstantiationException | NoSuchMethodException | InvocationTargetException | ClassNotFoundException | IllegalAccessException e) {
    18. e.printStackTrace();
    19. }
    20. return obj;
    21. }
    22. /*使用反射条用有参构造函数创建对象*/
    23. public static Computer getComputer(Class<? extends Computer> classType, String ram, String hdd, String cpu) {
    24. System.out.println("classType.getName() = " + classType.getName());
    25. Computer obj = null;
    26. try {
    27. Constructor<?> constructor = Class.forName(classType.getName()).getConstructor(String.class, String.class, String.class);
    28. obj = (Computer) constructor.newInstance(ram, hdd, cpu);
    29. } catch (InstantiationException | NoSuchMethodException | InvocationTargetException | ClassNotFoundException | IllegalAccessException e) {
    30. e.printStackTrace();
    31. }
    32. return obj;
    33. }
    34. }
    1. public class Client {
    2. /*public static void main(String[] args) {
    3. Computer pc = ComputerFactory.getComputer("pc", "2 GB", "500 GB", "2.4 GHz");
    4. Computer server = ComputerFactory.getComputer("server", "16 GB", "1 TB", "2.9 GHz");
    5. System.out.println("Factory PC Config::" + pc);
    6. System.out.println("Factory Server Config::" + server);
    7. }*/
    8. /*public static void main(String[] args) {
    9. Computer computer = ComputerFactory.getComputer(PC.class);
    10. System.out.println("computer = " + computer);
    11. }*/
    12. public static void main(String[] args) {
    13. Computer pc = ComputerFactory.getComputer(PC.class, "2 GB", "500 GB", "2.4 GHz");
    14. Computer server = ComputerFactory.getComputer(Server.class, "16 GB", "1 TB", "2.9 GHz");
    15. System.out.println("Factory PC Config::" + pc);
    16. System.out.println("Factory Server Config::" + server);
    17. }
    18. }