package com.github.code4craft.helloworld; import com.github.code4craft.helloworld.behavioral.state.HelloWorldStateContext; import com.github.code4craft.helloworld.creational.abstract_factory.SplitHelloWorldFactory; import com.github.code4craft.helloworld.creational.singleton.HelloWorldSingleton; import com.github.code4craft.helloworld.structural.adapter.HelloAdapterDesignPattern; import com.github.code4craft.helloworld.behavioral.chain_of_responsibility.HelloWorldObjectHandler; import com.github.code4craft.helloworld.behavioral.command.Command; import com.github.code4craft.helloworld.behavioral.command.HelloWorldPrintCommand; import com.github.code4craft.helloworld.behavioral.iterator.HelloWorldCharacterIterator; import com.github.code4craft.helloworld.behavioral.mediator.HelloWorldMediator; import com.github.code4craft.helloworld.behavioral.mediator.HelloWorldObject; import com.github.code4craft.helloworld.behavioral.memento.HelloWorldMementoOriginator; import com.github.code4craft.helloworld.behavioral.strategy.DesignPatternHelloWorldStrategy; import com.github.code4craft.helloworld.behavioral.strategy.HelloWorldStrategyContext; import com.github.code4craft.helloworld.behavioral.visitor.HelloWorldCharacterElements; import com.github.code4craft.helloworld.structural.adapter.HelloWorldAdapter; import com.github.code4craft.helloworld.structural.bridge.HelloWorldBridge; import com.github.code4craft.helloworld.structural.facade.HelloWorldFacade; import com.github.code4craft.helloworld.structural.proxy.HelloWorldProxy; import com.github.code4craft.helloworld.behavioral.chain_of_responsibility.HelloWorldInterjectionHandler; import com.github.code4craft.helloworld.behavioral.chain_of_responsibility.HelloWorldHandler; import com.github.code4craft.helloworld.behavioral.interpreter.HelloWorldInterpreter; import com.github.code4craft.helloworld.behavioral.mediator.HelloWorldInterjection; import com.github.code4craft.helloworld.behavioral.observer.HelloWorldObserver; import com.github.code4craft.helloworld.behavioral.observer.Subject; import com.github.code4craft.helloworld.behavioral.template_method.TemplateMethodHelloWorld; import com.github.code4craft.helloworld.behavioral.visitor.HelloWorldCharacterVisitor; import com.github.code4craft.helloworld.creational.abstract_factory.AbstractFactory; import com.github.code4craft.helloworld.creational.builder.HelloWorldBuilder; import com.github.code4craft.helloworld.creational.factory_method.FactoryMethodHelloWorldFactory; import com.github.code4craft.helloworld.creational.prototype.HelloWorldPrototype; import com.github.code4craft.helloworld.structural.bridge.DesignPatternWorldImpl; import com.github.code4craft.helloworld.structural.composite.CompositeHelloWorld; import com.github.code4craft.helloworld.structural.decorator.HelloWorldDecorator; import com.github.code4craft.helloworld.structural.flyweight.HelloWorldFlyWeightFactory; /** * @author yihua.huang@dianping.com */ public class Main { public static void main(String[] args) throws InstantiationException, IllegalAccessException { System.out.println("We are creational patterns!"); System.out.println("1. Abstract Factory: "); SplitHelloWorldFactory abstractFactory = AbstractFactory.select(AbstractFactory.Type.DesignPattern); HelloWorld abstractFactoryHelloWorld = new SplitHelloWorld(abstractFactory.createHelloWorldInterjection(), abstractFactory.createHelloWorldObject()); System.out.println(abstractFactoryHelloWorld.helloWorld()); System.out.println("2. Builder: "); HelloWorld builderHelloWorld = HelloWorldBuilder.builder() .interjection("Hello") .object("Builder").getHelloWorld(); System.out.println(builderHelloWorld.helloWorld()); System.out.println("3. Factory Method: "); /** * Difference between "abstract factory" and "factory method" is the abstracting point: * 1. Abstracting point of abstract factory is factory. * 2. Abstracting point of factory method is generated object. */ FactoryMethodHelloWorldFactory factoryMethodHelloWorldFactory = new FactoryMethodHelloWorldFactory(); HelloWorld factoryMethodHelloWorld = factoryMethodHelloWorldFactory.createHelloWorld(); System.out.println(factoryMethodHelloWorld.helloWorld()); System.out.println("4. Prototype: "); HelloWorld prototypeHelloWorld = HelloWorldPrototype.PROTOTYPE.clone(); System.out.println(prototypeHelloWorld.helloWorld()); System.out.println("5. Singleton: "); HelloWorld singletonHelloWorld = HelloWorldSingleton.instance(); System.out.println(singletonHelloWorld.helloWorld()); System.out.println("===================================="); System.out.println("We are structural patterns!"); System.out.println("6. Adapter: "); HelloWorld adapterHelloWorld = new HelloWorldAdapter(new HelloAdapterDesignPattern()); System.out.println(adapterHelloWorld.helloWorld()); System.out.println("7. Bridge: "); HelloWorld bridgeHelloWorld = new HelloWorldBridge(new DesignPatternWorldImpl()); //leave bridgeHelloWorld' output to compositeHelloWorld System.out.println("8. Composite: "); HelloWorld compositeHelloWorld = new CompositeHelloWorld(bridgeHelloWorld, new CompositeHelloWorld.DefaultHelloWorld()); System.out.println(compositeHelloWorld.helloWorld()); System.out.println("9. Decorator: "); HelloWorldDecorator decoratorHelloWorld = new HelloWorldDecorator(new HelloWorldDecorator.DefaultHelloWorld()); System.out.println(decoratorHelloWorld.helloDecorator()); System.out.println("10. Facade: "); HelloWorld facadeHelloWorld = HelloWorldFacade.instance().facadeHelloWorld(); System.out.println(facadeHelloWorld.helloWorld()); System.out.println("11. Flyweight: "); HelloWorld flyWeightHelloWorld = HelloWorldFlyWeightFactory.instance().createHelloWorld("Hello Flyweight!"); System.out.println(flyWeightHelloWorld.helloWorld()); System.out.println("12. Proxy: "); HelloWorld proxyHelloWorld = new HelloWorldProxy(new HelloWorldProxy.DefaultHelloWorld()); System.out.println(proxyHelloWorld.helloWorld()); System.out.println("===================================="); System.out.println("We are behavioral patterns!"); System.out.println("13. Chain of Responsibility: "); HelloWorldHandler helloWorldChainOfResponsibility = new HelloWorldInterjectionHandler().setNext(new HelloWorldObjectHandler()); System.out.println(helloWorldChainOfResponsibility.helloWorld()); System.out.println("14. Command: "); Command helloWorldCommand = new HelloWorldPrintCommand(); helloWorldCommand.execute(); System.out.println("15. Interpreter: "); HelloWorldInterpreter helloWorldInterpreter = new HelloWorldInterpreter(); helloWorldInterpreter.interpret("println('Hello Interpreter!')"); System.out.println("16. Iterator: "); HelloWorldCharacterIterator helloWorldCharacterIterator = new HelloWorldCharacterIterator("Hello Iterator!".toCharArray()); while (helloWorldCharacterIterator.hasNext()) { System.out.print(helloWorldCharacterIterator.next()); } System.out.println(); System.out.println("17. Mediator: "); HelloWorldInterjection helloWorldInterjection = new HelloWorldInterjection(); HelloWorldObject helloWorldObject = new HelloWorldObject(); HelloWorldMediator helloWorldMediator = new HelloWorldMediator(helloWorldInterjection, helloWorldObject); helloWorldInterjection.setHelloWorldMediator(helloWorldMediator); helloWorldObject.setHelloWorldMediator(helloWorldMediator); System.out.println(helloWorldObject.helloWorld()); System.out.println("18. Memento: "); HelloWorldMementoOriginator helloWorldMementoOriginator = new HelloWorldMementoOriginator(); HelloWorldMementoOriginator.Memento memento = helloWorldMementoOriginator.set("Hello Memento!").saveToMemento(); helloWorldMementoOriginator.set("Hello Whatever!"); helloWorldMementoOriginator.restoreFromMemento(memento); System.out.println(helloWorldMementoOriginator.helloWorld()); System.out.println("19. Observer: "); Subject subject = new Subject().attach(new HelloWorldObserver()); subject.notifyObservers(); System.out.println("20. State: "); HelloWorldStateContext helloWorldStateContext = new HelloWorldStateContext(); HelloWorld stateHelloWorld = helloWorldStateContext.appendWord("Hello").appendWord("State"); System.out.println(stateHelloWorld.helloWorld()); System.out.println("21. Strategy: "); HelloWorldStrategyContext helloWorldStrategyContext = new HelloWorldStrategyContext(new DesignPatternHelloWorldStrategy()); System.out.println(helloWorldStrategyContext.helloWorld()); System.out.println("22. Template Method: "); HelloWorld templateMethodHelloWorld = new TemplateMethodHelloWorld(); System.out.println(templateMethodHelloWorld.helloWorld()); System.out.println("23. Visitor: "); HelloWorldCharacterElements helloWorldCharacterElements = new HelloWorldCharacterElements("Hello Visitor!".toCharArray()); HelloWorldCharacterVisitor helloWorldCharacterVisitor = new HelloWorldCharacterVisitor(); helloWorldCharacterElements.accept(helloWorldCharacterVisitor); System.out.println(helloWorldCharacterVisitor.helloWorld()); } }