samedi 28 février 2015

Design Patterns - One public class utilizing many hidden classes

I have gone through http://ift.tt/1tmzBcW in trying to find out the most efficient to create a design pattern in which "one visible class utilizes many hidden classes" to create a fluent API. Below is the code I currently have:



public class VisibleClass {
Private OrderClass order;
private ReceiptClass receipt;

public VisibleClass makeOrder() {
if (!(order instanceof OrderClass))
order = new OrderClass();

order.make();
return this;
}

public VisibleClass printReceipt() {
if (!(receipt instanceof ReceiptClass))
receipt = new ReceiptClass();

receipt.print();
return this;
}
}

class OrderClass implements IOrder {
public void make() {}
}

class ReceiptClass implements IReceipt {
public void print() {}
}

interface IOrder { void make(); }
interface IReceipt { void print(); }


Here is how I am currently using the API:



public static void main(String[] args) {
VisibleClass x = new VisibleClass();
x.makeOrder().printReceipt();
}


It this a good approach? Can a better approach be used for it?


*EDIT: Also, I should add that the VisibleClass will implement all methods of the hidden classes.


Aucun commentaire:

Enregistrer un commentaire