Abstract Factory Pattern and Printing

fuzzdelay :

Working on learning more design patterns - specifically here I'm working on the abstract factory pattern. I'm trying to output "Receipt 1 Header 1 Footer 1" all in one line, but I can only seem to output via the displayName method one at a time. What is the best way to concatenate them? A toString method in the abstract classes? Any pointers would be greatly appreciated. Here are my classes:

public abstract class AbstractSalesReceiptFactory {
    public abstract AbstractReceipt createReceipt();
    public abstract AbstractHeader createHeader();
    public abstract AbstractFooter createFooter();


}

public abstract class AbstractReceipt {
    public abstract void displayName(AbstractReceipt a);

}

public abstract class AbstractHeader {
    public abstract void displayName(AbstractHeader a);

}

public abstract class AbstractFooter {
    public abstract void displayName(AbstractFooter a);

}

public class ConcreteReceipt1 extends AbstractSalesReceiptFactory{

    public AbstractReceipt createReceipt() {
        return new Receipt1();
    }
    public AbstractHeader createHeader() {
        return new Header1();
    }


    public AbstractFooter createFooter() {
        return new Footer1();
    }

    private ConcreteReceipt1() {}

    public String displayName() {
        return "Receipt1";
    }

    private static ConcreteReceipt1 instance = null;

    public static ConcreteReceipt1 getInstance() {
        instance = new ConcreteReceipt1();
        return instance;
    }

}

public class ConcreteReceipt2 extends AbstractSalesReceiptFactory{

    public AbstractReceipt createReceipt() {
        return new Receipt1();
    }

    public AbstractHeader createHeader() {
        return new Header1();
    }


    public AbstractFooter createFooter() {
        return new Footer1();
    }

    private ConcreteReceipt2() {
    }

    public String displayName() {
        return "Receipt2";
    }
    private static ConcreteReceipt2 instance = null;

    public static ConcreteReceipt2 getInstance() {
        instance = new ConcreteReceipt2();
        return instance;
    }


}

public class Footer1 extends AbstractFooter{
    public void displayName(AbstractFooter a) {
        System.out.println("Footer 1");
    }
}

public class Footer2 extends AbstractFooter{
    public void displayName(AbstractFooter a) {
        System.out.println("Footer 2");
    }
}

public class Header1 extends AbstractHeader{
    public void displayName(AbstractHeader a) {
        System.out.println("Header 1");
    }

}

public class Header2 extends AbstractHeader{
    public void displayName(AbstractHeader a) {
        System.out.println("Header 2");
    }

}

public class Receipt1 extends AbstractReceipt{
    public void displayName(AbstractReceipt a) {
        System.out.println("Receipt 1");
    }

}

public class Receipt2 extends AbstractReceipt{
    public void displayName(AbstractReceipt a) {
        System.out.println("Receipt 2");
    }

}

my main class:

public class Client {

    private static AbstractReceipt ar;
    private static AbstractHeader ah;
    private static AbstractFooter af;

    public Client(AbstractSalesReceiptFactory factory) {
        ar = factory.createReceipt();
        ah = factory.createHeader();
        af = factory.createFooter();
    }

    public void run() {
        ar.displayName(ar);
        ah.displayName(ah);
        af.displayName(af);
    }

    public static void main (String [] args) {
        AbstractSalesReceiptFactory receipt1 = ConcreteReceipt1.getInstance();

        Client c1 = new Client(receipt1);
        c1.run();
    }
}
Philip Wrage :
  1. The absolute easiest thing you could do would be to change System.out.println to System.out.print in your existing displayName method implementations.

  2. You could also override the toString method, but you may want to leave toString as it is and use displayName.

  3. Another simple way to get the output you desire would be to first update the signature of displayName in your abstract classes so it returns String instead of void (and takes no arguments). For example,

    public abstract class AbstractReceipt {
        public abstract String displayName();
    }

Then update the implementations in all of your concrete classes to return the desired display name instead of printing directly.

    public class Receipt1 extends AbstractReceipt{
        public String displayName() {
            return "Receipt 1";
        }
    }

Finally, update run method to perform the System.out.println.

    public void run() {
        System.out.println(
            String.format( "%s %s %s", ar.displayName(), ah.displayName(),  af.displayName() )
        );
    }

However, your request is more related to Java I/O and not design patterns. You are simply using the console output to provide dead-simple verification of your pattern implementation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=317264&siteId=1