Thursday, December 5, 2013

Hierarchical software design example

application model Problem: Print customer names from a given file. Solution: Step 1: Create logical classes(data + tasks + services) to perform the pieces of the problem solution. --logical implementation of functional requirements. public class CustomerData { //process data private LinkedList<Customer> customerList; public CustomerData() { customerList = new LinkedList<Customer>(); } public final LinkedList<Customer> getCustomerList() { return customerList; } public final void addCustomer(Customer c) { getCustomerList().add(c); } } public class ReadCustomers { //service public readCustomers() { FileReader fr = new FileReader(getFilename()); BufferedReader br = new BufferedReader(fr); String name = br.readLine(); while (name != null) { Customer customer = new Customer(name); addCustomer(customer); name = br.readLine(); } } } public class PrintCustomers { //controller task public printCustomers() { for (Customer customer : getCustomerList()) { printCustomer(customer); } } } public class PrintCustomer { //terminal task public printCustomer(Customer customer) { System.out.println(customer.getName()); } } public class CustomersTask { //controller task public printCustomersTask() { readCustomers(); printCustomers(); } public static void main(String args[]) { String filename = args[0]; ........ CustomersTask pct = new CustomersTask(....); pct.printCustomersTask(); } } customersTask main printCustomers readCustomers filename injection printCustomer customerData Object dependency diagram To make the application complete, do the following 2.Connect these classes to make an extensible structure 3.Create object instances 4.write linking code from caller to callee Step 2. Create a structure by creating a set of connector (context) classes and then attatch the logical classes to these nodes, to create an extensible hierarchical structure. CustomersTask CustomersTaskContext ReadCustomers PrintCustomers PrintCustomersContext PrintCustomer class structure diagram public class PrintCustomersContext { //Connection example private CustomersTaskContext customersTaskContext; public PrintCustomersContext(CustomersTaskContext ctc) { this.customersTaskContext = ctc; } public final CustomersTaskContext getCustomersTaskContext() { return customersTaskContext; } } Step 3. Add object composition code to the classes. main CustomersTask CustomersTaskContext CustomerData ReadCustomers PrintCustomers PrintCustomersContext PrintCustomer class structure with object composition diagram public class CustomersTask { //object composition example private CustomersTaskContext customersTaskContext; private PrintCustomersContext printCustomersContext; private ReadCustomers readCustomers; private PrintCustomers printCustomers; public CustomersTask(CustomersTaskContext ctc) { this.customersTaskContext = ctc; printCustomersContext = new PrintCustomersContext(ctc); readCustomers = new ReadCustomers(ctc); printCustomers = new PrintCustomers(ctc); } } Step 4. Add the linking code. Here is the complete code.
public class CustomersTask {

    private CustomersTaskContext customersTaskContext;
    private PrintCustomersContext printCustomersContext;
    private ReadCustomers readCustomers;
    private PrintCustomers printCustomers;

    public CustomersTask(CustomersTaskContext customersTaskContext) {
        this.customersTaskContext = customersTaskContext;
        customersTaskInit();
    }

    public final void customersTaskInit() {
        printCustomersContext 
                = new PrintCustomersContext(getCustomersTaskContext());
        readCustomers = new ReadCustomers(getCustomersTaskContext());
        printCustomers = new PrintCustomers(getPrintCustomersContext());
    }

    public final void printCustomersTask() throws IOException {
        readCustomers();
        printCustomers();
    }

    public static void main(String args[]) {
        try {
            String filename = args[0];
            CustomersTaskContext ctc = new CustomersTaskContext();
            ctc.init(filename);
            CustomersTask ct = new CustomersTask(ctc);
            ct.printCustomersTask();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private CustomersTaskContext getCustomersTaskContext() {
        return customersTaskContext;
    }

    private PrintCustomersContext getPrintCustomersContext() {
        return printCustomersContext;
    }

    private PrintCustomers getPrintCustomers() {
        return printCustomers;
    }

    private void printCustomers() {
        getPrintCustomers().printCustomers();
    }

    private ReadCustomers getReadCustomers() {
        return readCustomers;
    }

    private void readCustomers() throws IOException {
        getReadCustomers().readCustomers();
    }
}
public class PrintCustomers {

    private PrintCustomersContext printCustomersContext;
    private PrintCustomer printCustomer;

    public PrintCustomers(PrintCustomersContext printCustomersContext) {
        this.printCustomersContext = printCustomersContext;
        printCustomersInit();
    }

    public final void printCustomersInit() {
        printCustomer = new PrintCustomer(getPrintCustomersContext());
    }

    public final void printCustomers() {
        for (Customer customer : getCustomerList()) {
            printCustomer(customer);
        }
    }

    private PrintCustomersContext getPrintCustomersContext() {
        return printCustomersContext;
    }

    private PrintCustomer getPrintCustomer() {
        return printCustomer;
    }

    private CustomersTaskContext getCustomersTaskContext() {
        return getPrintCustomersContext().getCustomersTaskContext();
    }

    private void printCustomer(Customer customer) {
        getPrintCustomer().printCustomer(customer);
    }

    private CustomerData getCustomerData() {
        return getCustomersTaskContext().getCustomerData();
    }

    private LinkedList getCustomerList() {
        return getCustomerData().getCustomerList();
    }
}
© Ravindra Mahajan
public class CustomersTaskContext {

    private String filename;
    private CustomerData customerData;

    public CustomersTaskContext() {
    }

    public void init(String filename) {
        this.filename = filename;
        this.customerData = new CustomerData();
    }

    public final String getFilename() {
        return filename;
    }

    public final CustomerData getCustomerData() {
        return customerData;
    }
}
public class ReadCustomers {

    private CustomersTaskContext customersTaskContext;

    public ReadCustomers(CustomersTaskContext customersTaskContext) {
        this.customersTaskContext = customersTaskContext;
    }

    public final void readCustomers() throws IOException {
        FileReader fr = new FileReader(getFilename());
        BufferedReader br = new BufferedReader(fr);
        String name = br.readLine();
        while (name != null) {
            Customer customer = new Customer(name);
            addCustomer(customer);
            name = br.readLine();
        }
        br.close();
    }

    private CustomersTaskContext getCustomersTaskContext() {
        return customersTaskContext;
    }

    private String getFilename() {
        return getCustomersTaskContext().getFilename();
    }

    private CustomerData getCustomerData() {
        return getCustomersTaskContext().getCustomerData();
    }

    private boolean addCustomer(Customer customer) {
        return getCustomerData().addCustomer(customer);
    }
}
public class PrintCustomersContext {

    private CustomersTaskContext customersTaskContext;

    public PrintCustomersContext(CustomersTaskContext ctc) {
        this.customersTaskContext = ctc;
    }

    public final CustomersTaskContext getCustomersTaskContext() {
        return customersTaskContext;
    }
}
public class PrintCustomer {

    private PrintCustomersContext printCustomersContext;

    public PrintCustomer(PrintCustomersContext pcc) {
        this.printCustomersContext = pcc;
    }

    public final void printCustomer(Customer customer) {
        System.out.println(customer.getName());
    }
}
public class CustomerData {

    private LinkedList customerList;

    public CustomerData() {
        customerList = new LinkedList();
    }

    public boolean addCustomer(Customer c) {
        return getCustomerList().add(c);
    }

    public LinkedList getCustomerList() {
        return customerList;
    }
}
public class Customer {

    private String name;

    public Customer(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

No comments:

Post a Comment