应用SRP和OCP

问题描述 投票:0回答:1

我一直在尝试将SRP和OCP应用于此代码。该代码应允许银行员工预订或不预订硬编码约会。我无法弄清楚要创建哪些类来将这两个原理应用于代码。我想知道我需要哪些类以及应该应用的继承。


        package gradedGroupProject.nonPrincipledDesign.v1;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;

    public class ORIGINAL {

        public String name = "X";

        public List<BankClient> bankClientsWithAppointments;
        public List<Date> appointmentDates;

        public static void main(String[] args) throws ParseException {

            BankEmployee bankEmployee = new BankEmployee();

            bankEmployee.bankClientsWithAppointments = new ArrayList<BankClient>();
            bankEmployee.appointmentDates = new ArrayList<Date>();

            BankClient bankClient1 = new BankClient("u1", "p1");
            bankClient1.name = "A";
            bankEmployee.bankClientsWithAppointments.add(bankClient1);
            bankEmployee.appointmentDates.add(new SimpleDateFormat("dd/MM/yyyy").parse("20/12/2000"));

            BankClient bankClient2 = new BankClient("u2", "p2");
            bankClient2.name = "B";
            bankEmployee.bankClientsWithAppointments.add(bankClient2);
            bankEmployee.appointmentDates.add(new SimpleDateFormat("dd/MM/yyyy").parse("22/12/2000"));

            print(bankEmployee.bankClientsWithAppointments);



            for (int i = 0; i < bankEmployee.bankClientsWithAppointments.size(); ++i) {

                Date date = bankEmployee.appointmentDates.get(i);
                BankClient bankClient = bankEmployee.bankClientsWithAppointments.get(i);

                System.out.println("\nCandidate date = " + date + " with client = " + bankClient.name);

                System.out.println("1. Verify it");
                System.out.println("2. Do not book it");
                String choice = read("choice");

                if (choice.equals("1"))
                    bankClient.bookAppointment(date, bankEmployee.name); // we assume that we answer with this call to the
                                                                            // client.
                System.out.println("Appointment Booked");
            }
        }

        public static String read(String label) {

            System.out.println("\nProvide your " + label + ":");

            System.out.println(">");

            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

            String value = null;

            try {

                value = input.readLine();
            }

            catch (IOException ex) {
                ex.printStackTrace();
            }

            return value;
        }

        public static void print(List<BankClient> bankClients) {

            for (int i = 0; bankClients != null && i < bankClients.size(); ++i)
                bankClients.get(i).toPrint();
        }
    }

java eclipse single-responsibility-principle appointment open-closed-principle
1个回答
0
投票

首先,您应该从数据实体中删除generating方法,并将它们移至其他一些类(以维护SR Principle)。新的类将成为您的“迭代者”(知道如何执行某些任务)。

对于每个迭代器角色,您都会生成一个接口,该接口指定该角色的“合同”,然后您可以对该接口有许多不同的实现。如果需要为该角色添加新的某种不同的实现,则可以创建该接口的新实现,而不必修改当前代码(OCP Principle

© www.soinside.com 2019 - 2024. All rights reserved.