Archetype pattern
The archetype pattern is a software design pattern that separates logic from implementation. The separation is accomplished through the creation of two abstract classes: a decorator (for logic), and a delegate (for implementation). The Factory handles the mapping of decorator and delegate classes and returns the pair associated with a parameter or parameters passed. The interface is the contract between a decorator, a delegate, and the calling class, creating an Inversion of Responsibility.[1] This example use two branches; however, you can have 'N' branches as required. The pattern means that one branch from the interface does not have to worry about how another branch operators as long it implements the interface. SectionsDecorator Delegate UMLJava examplepublic interface Request {
public void sendRequest();
}
public class RequestFactory {
public static Request getRequest(String a, String b){
DecoratorRequest dcr = null;
DelegateRequest dlr = null;
if (a.equals("A"))
dcr = new ADecoratorRequest();
if (a.equals("B"))
dcr = new BDecoratorRequest();
if (b.equals("Y"))
dlr = new YDelegateRequest();
if (b.equals("Z"))
dlr = new ZDelegateRequest();
dcr.setDelegate(dlr);
return dcr;
}
}
public class App {
public static void main(String[] args) {
Request cr = null;
cr = RequestFactory.getRequest("A", "Y");
cr.sendRequest();
cr = RequestFactory.getRequest("A", "Z");
cr.sendRequest();
cr = RequestFactory.getRequest("B", "Y");
cr.sendRequest();
cr = RequestFactory.getRequest("B", "Z");
cr.sendRequest();
}
}
public abstract class DecoratorRequest implements Request {
protected DelegateRequest delegate;
public DecoratorRequest() {
}
public void setDelegate(DelegateRequest delegate) {
this.delegate = delegate;
}
}
public abstract class DelegateRequest implements Request {
public DelegateRequest () {
}
}
public class ADecoratorRequest extends DecoratorRequest {
@Override
public void sendRequest() {
System.out.print("A-");
delegate.sendRequest();
}
}
public class BDecoratorRequest extends DecoratorRequest {
@Override
public void sendRequest() {
System.out.print("B-");
delegate.sendRequest();
}
}
public class YDelegateRequest extends DelegateRequest {
@Override
public void sendRequest() {
System.out.println("-Y");
}
}
public class ZDelegateRequest extends DelegateRequest {
@Override
public void sendRequest() {
System.out.println("-Z");
}
}
ParticipantsDelegation pattern - calls the specific implementation Decorator pattern - performs the generalised logic Factory method pattern - creates the archetype combination References
See also
|