Design Patterns Model Codes (Landscape Format)
/* =========================
CREATIONAL DESIGN PATTERNS
========================= */
// 1. Factory Method
abstract class Product { public abstract void show(); }
class ConcreteProductA extends Product { public void show() { [Link]("A"); } }
class ConcreteProductB extends Product { public void show() { [Link]("B"); } }
abstract class Creator { public abstract Product factoryMethod(); }
class ConcreteCreatorA extends Creator {
public Product factoryMethod() { return new ConcreteProductA(); }
}
class ConcreteCreatorB extends Creator {
public Product factoryMethod() { return new ConcreteProductB(); }
}
class FactoryClient {
public static void main(String[] args) {
Creator creator = new ConcreteCreatorA();
Product p = [Link]();
[Link]();
}
}
// 2. Abstract Factory
interface Button { void paint(); }
interface Checkbox { void check(); }
class WinButton implements Button { public void paint() { [Link]("Windows Button"); } }
class MacButton implements Button { public void paint() { [Link]("Mac Button"); } }
interface GUIFactory {
Button createButton();
Checkbox createCheckbox();
}
class WinFactory implements GUIFactory {
public Button createButton() { return new WinButton(); }
public Checkbox createCheckbox() { return new Checkbox() { public void check(){ [Link]("Win Checkbox"); } }; }
}
class MacFactory implements GUIFactory {
public Button createButton() { return new MacButton(); }
public Checkbox createCheckbox() { return new Checkbox() { public void check(){ [Link]("Mac Checkbox"); } }; }
}
class AbstractFactoryClient {
public static void main(String[] args) {
GUIFactory factory = new WinFactory();
Button b = [Link](); [Link]();
}
}
// 3. Builder
class ProductBuild { String partA, partB; public void show(){ [Link](partA + " " + partB); } }
abstract class Builder {
protected ProductBuild p = new ProductBuild();
public abstract void buildPartA();
public abstract void buildPartB();
public ProductBuild getResult(){ return p; }
}
class ConcreteBuilder extends Builder {
public void buildPartA(){ [Link] = "PartA"; }
public void buildPartB(){ [Link] = "PartB"; }
}
class Director {
public ProductBuild construct(Builder b){
[Link]();
[Link]();
return [Link]();
}
}
class BuilderClient {
public static void main(String[] args){
Builder b = new ConcreteBuilder();
ProductBuild p = new Director().construct(b);
[Link]();
}
}
// 4. Prototype
interface Prototype { Prototype clone(); }
class ConcretePrototype implements Prototype {
public Prototype clone() { return (Prototype)this; }
}
class PrototypeClient {
public static void main(String[] args){
ConcretePrototype obj1 = new ConcretePrototype();
ConcretePrototype obj2 = (ConcretePrototype)[Link]();
}
}
// 5. Singleton
class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance(){
if(instance == null) instance = new Singleton();
return instance;
}
}
class SingletonClient {
public static void main(String[] args){
Singleton s1 = [Link]();
Singleton s2 = [Link]();
[Link](s1 == s2);
}
}
/* =========================
STRUCTURAL DESIGN PATTERNS
========================= */
// 1. Adapter
interface Target { void request(); }
class Adaptee { void specificRequest(){ [Link]("Specific Request"); } }
class Adapter implements Target {
Adaptee adaptee = new Adaptee();
public void request(){ [Link](); }
}
class AdapterClient {
public static void main(String[] args){
Target t = new Adapter();
[Link]();
}
}
// 2. Bridge
interface Implementor { void operationImpl(); }
class ConcreteImplementorA implements Implementor { public void operationImpl(){ [Link]("Impl A"); } }
abstract class Abstraction {
protected Implementor impl;
public Abstraction(Implementor i){ impl = i; }
public abstract void operation();
}
class RefinedAbstraction extends Abstraction {
public RefinedAbstraction(Implementor i){ super(i); }
public void operation(){ [Link](); }
}
class BridgeClient {
public static void main(String[] args){
Abstraction ab = new RefinedAbstraction(new ConcreteImplementorA());
[Link]();
}
}
// 3. Composite
import [Link].*;
interface Component { void show(); }
class Leaf implements Component { public void show(){ [Link]("Leaf"); } }
class Composite implements Component {
List<Component> children = new ArrayList<>();
public void add(Component c){ [Link](c); }
public void show(){ for(Component c : children) [Link](); }
}
class CompositeClient {
public static void main(String[] args){
Composite root = new Composite();
[Link](new Leaf());
[Link](new Leaf());
[Link]();
}
}
// 4. Decorator
interface Comp { void operation(); }
class ConcreteComp implements Comp { public void operation(){ [Link]("Base Op"); } }
class Decorator implements Comp {
protected Comp comp;
public Decorator(Comp c){ comp = c; }
public void operation(){ [Link](); }
}
class ConcreteDecorator extends Decorator {
public ConcreteDecorator(Comp c){ super(c); }
public void operation(){ [Link](); [Link]("Extra Feature"); }
}
class DecoratorClient {
public static void main(String[] args){
Comp c = new ConcreteDecorator(new ConcreteComp());
[Link]();
}
}
// 5. Facade
class SubsystemA { void opA(){ [Link]("A"); } }
class SubsystemB { void opB(){ [Link]("B"); } }
class Facade {
private SubsystemA a = new SubsystemA();
private SubsystemB b = new SubsystemB();
void operation(){ [Link](); [Link](); }
}
class FacadeClient {
public static void main(String[] args){
new Facade().operation();
}
}
// 6. Proxy
interface Subject { void request(); }
class RealSubject implements Subject { public void request(){ [Link]("Real Request"); } }
class Proxy implements Subject {
private RealSubject real;
public void request(){
if(real == null) real = new RealSubject();
[Link]("Proxy access check");
[Link]();
}
}
class ProxyClient {
public static void main(String[] args){
Subject s = new Proxy();
[Link]();
}
}
/* =========================
BEHAVIORAL DESIGN PATTERNS
========================= */
// 1. Chain of Responsibility
abstract class Handler {
protected Handler next;
public void setNext(Handler n){ next = n; }
public abstract void handleRequest(int level);
}
class ConcreteHandler1 extends Handler {
public void handleRequest(int level){
if(level == 1) [Link]("Handled by 1");
else if(next != null) [Link](level);
}
}
class ConcreteHandler2 extends Handler {
public void handleRequest(int level){
if(level == 2) [Link]("Handled by 2");
else if(next != null) [Link](level);
}
}
class CORClient {
public static void main(String[] args){
Handler h1 = new ConcreteHandler1();
Handler h2 = new ConcreteHandler2();
[Link](h2);
[Link](2);
}
}
// 2. Command
interface Command { void execute(); }
class Receiver { void action(){ [Link]("Action done"); } }
class ConcreteCommand implements Command {
Receiver r;
public ConcreteCommand(Receiver r){ this.r = r; }
public void execute(){ [Link](); }
}
class Invoker {
Command cmd;
public void setCommand(Command c){ cmd = c; }
public void execute(){ [Link](); }
}
class CommandClient {
public static void main(String[] args){
Receiver r = new Receiver();
Command c = new ConcreteCommand(r);
Invoker i = new Invoker();
[Link](c);
[Link]();
}
}
// 3. Iterator
interface IteratorI { boolean hasNext(); Object next(); }
interface Aggregate { IteratorI createIterator(); }
class ConcreteAggregate implements Aggregate {
private String[] items = {"A", "B", "C"};
public IteratorI createIterator(){ return new ConcreteIterator(items); }
}
class ConcreteIterator implements IteratorI {
private String[] items; int pos = 0;
public ConcreteIterator(String[] i){ items = i; }
public boolean hasNext(){ return pos < [Link]; }
public Object next(){ return items[pos++]; }
}
class IteratorClient {
public static void main(String[] args){
Aggregate agg = new ConcreteAggregate();
IteratorI it = [Link]();
while([Link]()) [Link]([Link]());
}
}
// 4. Observer
interface Observer { void update(); }
interface SubjectObs { void add(Observer o); void notifyAllObservers(); }
class ConcreteSubject implements SubjectObs {
List<Observer> obs = new ArrayList<>();
public void add(Observer o){ [Link](o); }
public void notifyAllObservers(){ for(Observer o : obs) [Link](); }
}
class ConcreteObserver implements Observer {
public void update(){ [Link]("Updated!"); }
}
class ObserverClient {
public static void main(String[] args){
ConcreteSubject s = new ConcreteSubject();
[Link](new ConcreteObserver());
[Link]();
}
}
// 5. Strategy
interface Strategy { void execute(); }
class ConcreteStrategyA implements Strategy { public void execute(){ [Link]("A"); } }
class ConcreteStrategyB implements Strategy { public void execute(){ [Link]("B"); } }
class Context {
Strategy s;
public Context(Strategy s){ this.s = s; }
public void execute(){ [Link](); }
}
class StrategyClient {
public static void main(String[] args){
Context c = new Context(new ConcreteStrategyA());
[Link]();
}
}
// 6. Template Method
abstract class AbstractClass {
public final void template(){
step1(); step2(); step3();
}
abstract void step1();
abstract void step2();
void step3(){ [Link]("Common Step"); }
}
class ConcreteClass extends AbstractClass {
void step1(){ [Link]("Step 1"); }
void step2(){ [Link]("Step 2"); }
}
class TemplateClient {
public static void main(String[] args){
new ConcreteClass().template();
}
}