├── src └── main │ └── java │ └── com │ └── jourwon │ └── designpattern │ ├── creational │ ├── builder │ │ ├── Packing.java │ │ ├── Bottle.java │ │ ├── Item.java │ │ ├── Wrapper.java │ │ ├── ColdDrink.java │ │ ├── Coke.java │ │ ├── Pepsi.java │ │ ├── Burger.java │ │ ├── ChickenBurger.java │ │ ├── VegBurger.java │ │ ├── MealBuilder.java │ │ ├── BuilderPatternDemo.java │ │ └── Meal.java │ ├── factory │ │ ├── Shape.java │ │ ├── Circle.java │ │ ├── Square.java │ │ ├── Rectangle.java │ │ ├── ShapeFactory.java │ │ └── FactoryPatternDemo.java │ ├── abstractfactory │ │ ├── Color.java │ │ ├── Shape.java │ │ ├── Blue.java │ │ ├── Red.java │ │ ├── Square.java │ │ ├── Circle.java │ │ ├── Green.java │ │ ├── AbstractFactory.java │ │ ├── Rectangle.java │ │ ├── FactoryProducer.java │ │ ├── ColorFactory.java │ │ ├── ShapeFactory.java │ │ └── AbstractFactoryDemo.java │ ├── prototype │ │ ├── Circle.java │ │ ├── Square.java │ │ ├── Rectangle.java │ │ ├── PrototypePatternDemo.java │ │ ├── Shape.java │ │ └── ShapeCache.java │ └── singleton │ │ ├── SingleObject.java │ │ ├── Singleton6.java │ │ ├── Singleton1.java │ │ ├── SingletonPatternDemo.java │ │ ├── Singleton3.java │ │ ├── Singleton2.java │ │ ├── Singleton8.java │ │ ├── Singleton4.java │ │ ├── Singleton9.java │ │ ├── Singleton7.java │ │ └── Singleton5.java │ ├── structural │ ├── facade │ │ ├── Shape.java │ │ ├── Circle.java │ │ ├── Square.java │ │ ├── Rectangle.java │ │ ├── FacadePatternDemo.java │ │ └── ShapeMaker.java │ ├── proxy │ │ ├── Image.java │ │ ├── ProxyPatternDemo.java │ │ ├── ProxyImage.java │ │ └── RealImage.java │ ├── decorator │ │ ├── Shape.java │ │ ├── Circle.java │ │ ├── Rectangle.java │ │ ├── ShapeDecorator.java │ │ ├── RedShapeDecorator.java │ │ └── DecoratorPatternDemo.java │ ├── flyweight │ │ ├── Shape.java │ │ ├── ShapeFactory.java │ │ ├── Circle.java │ │ └── FlyweightPatternDemo.java │ ├── bridge │ │ ├── DrawApi.java │ │ ├── Shape.java │ │ ├── GreenCircle.java │ │ ├── RedCircle.java │ │ ├── BridgePatternDemo.java │ │ └── Circle.java │ ├── adapter │ │ ├── MediaPlayer.java │ │ ├── AdvancedMediaPlayer.java │ │ ├── Mp4Player.java │ │ ├── VlcPlayer.java │ │ ├── AdapterPatternDemo.java │ │ ├── MediaAdapter.java │ │ └── AudioPlayer.java │ ├── filter │ │ ├── Criteria.java │ │ ├── CriteriaFemale.java │ │ ├── CriteriaMale.java │ │ ├── CriteriaSingle.java │ │ ├── AndCriteria.java │ │ ├── Person.java │ │ ├── OrCriteria.java │ │ └── CriteriaPatternDemo.java │ └── composite │ │ ├── Employee.java │ │ └── CompositePatternDemo.java │ ├── behavioral │ ├── command │ │ ├── Order.java │ │ ├── SellStock.java │ │ ├── BuyStock.java │ │ ├── Stock.java │ │ ├── Broker.java │ │ └── CommandPatternDemo.java │ ├── iterator │ │ ├── Container.java │ │ ├── Iterator.java │ │ ├── IteratorPatternDemo.java │ │ └── NameRepository.java │ ├── state │ │ ├── State.java │ │ ├── Context.java │ │ ├── StopState.java │ │ ├── StartState.java │ │ └── StatePatternDemo.java │ ├── strategy │ │ ├── Strategy.java │ │ ├── OperationAdd.java │ │ ├── OperationMultiply.java │ │ ├── OperationSubstract.java │ │ ├── Context.java │ │ └── StrategyPatternDemo.java │ ├── interpreter │ │ ├── Expression.java │ │ ├── TerminalExpression.java │ │ ├── OrExpression.java │ │ ├── AndExpression.java │ │ └── InterpreterPatternDemo.java │ ├── visitor │ │ ├── ComputerPart.java │ │ ├── Monitor.java │ │ ├── Mouse.java │ │ ├── Keyboard.java │ │ ├── ComputerPartVisitor.java │ │ ├── VisitorPatternDemo.java │ │ ├── Computer.java │ │ └── ComputerPartDisplayVisitor.java │ ├── observer │ │ ├── Observer.java │ │ ├── OctalObserver.java │ │ ├── HexaObserver.java │ │ ├── BinaryObserver.java │ │ ├── ObserverPatternDemo.java │ │ └── Subject.java │ ├── nullobject │ │ ├── AbstractCustomer.java │ │ ├── NullCustomer.java │ │ ├── RealCustomer.java │ │ ├── CustomerFactory.java │ │ └── NullPatternDemo.java │ ├── memento │ │ ├── Memento.java │ │ ├── CareTaker.java │ │ ├── Originator.java │ │ └── MementoPatternDemo.java │ ├── mediator │ │ ├── ChatRoom.java │ │ ├── MediatorPatternDemo.java │ │ └── User.java │ ├── chain │ │ ├── FileLogger.java │ │ ├── ErrorLogger.java │ │ ├── ConsoleLogger.java │ │ ├── AbstractLogger.java │ │ └── ChainPatternDemo.java │ └── template │ │ ├── TemplatePatternDemo.java │ │ ├── Game.java │ │ ├── Cricket.java │ │ └── Football.java │ └── j2ee │ ├── interceptingfilter │ ├── Filter.java │ ├── Target.java │ ├── DebugFilter.java │ ├── AuthenticationFilter.java │ ├── Client.java │ ├── FilterManager.java │ ├── InterceptingFilterPatternDemo.java │ └── FilterChain.java │ ├── businessdelegate │ ├── BusinessService.java │ ├── JmsService.java │ ├── EjbService.java │ ├── Client.java │ ├── BusinessLookUp.java │ ├── BusinessDelegate.java │ └── BusinessDelegatePatternDemo.java │ ├── servicelocator │ ├── Service.java │ ├── Service2.java │ ├── Service1.java │ ├── InitialContext.java │ ├── ServiceLocator.java │ ├── ServiceLocatorPatternDemo.java │ └── Cache.java │ ├── frontcontroller │ ├── HomeView.java │ ├── StudentView.java │ ├── FrontControllerPatternDemo.java │ ├── Dispatcher.java │ └── FrontController.java │ ├── compositeentity │ ├── DependentObject2.java │ ├── DependentObject1.java │ ├── CompositeEntity.java │ ├── CompositeEntityPatternDemo.java │ ├── CoarseGrainedObject.java │ └── Client.java │ ├── dataaccessobject │ ├── StudentDao.java │ ├── Student.java │ ├── DataAccessObjectPatternDemo.java │ └── StudentDaoImpl.java │ ├── mvc │ ├── StudentView.java │ ├── Student.java │ ├── StudentController.java │ └── MvcPatternDemo.java │ └── transferobject │ ├── StudentVO.java │ ├── TransferObjectPatternDemo.java │ └── StudentBO.java ├── .gitignore └── pom.xml /src/main/java/com/jourwon/designpattern/creational/builder/Packing.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 9:21 8 | */ 9 | public interface Packing { 10 | 11 | String pack(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/factory/Shape.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.factory; 2 | 3 | /** 4 | * Description:创建一个接口 5 | * 6 | * @author JourWon 7 | * @date 2019/7/12 15:25 8 | */ 9 | public interface Shape { 10 | 11 | void draw(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/facade/Shape.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.facade; 2 | 3 | /** 4 | * Description:创建一个接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 10:03 8 | */ 9 | public interface Shape { 10 | 11 | void draw(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/proxy/Image.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.proxy; 2 | 3 | /** 4 | * Description:创建一个接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 13:45 8 | */ 9 | public interface Image { 10 | 11 | void display(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/command/Order.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.command; 2 | 3 | /** 4 | * Description:创建一个命令接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 9:36 8 | */ 9 | public interface Order { 10 | 11 | void execute(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/decorator/Shape.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.decorator; 2 | 3 | /** 4 | * Description:创建一个接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 9:27 8 | */ 9 | public interface Shape { 10 | 11 | void draw(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/flyweight/Shape.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.flyweight; 2 | 3 | /** 4 | * Description:创建一个接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 11:22 8 | */ 9 | public interface Shape { 10 | 11 | void draw(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/iterator/Container.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.iterator; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 11:55 8 | */ 9 | public interface Container { 10 | 11 | Iterator getIterator(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/state/State.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.state; 2 | 3 | /** 4 | * Description:创建一个接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 14:48 8 | */ 9 | public interface State { 10 | 11 | void doAction(Context context); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/Color.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description:为颜色创建一个接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 22:53 8 | */ 9 | public interface Color { 10 | 11 | void fill(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/Shape.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description:为形状创建一个接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 22:50 8 | */ 9 | public interface Shape { 10 | 11 | void draw(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/strategy/Strategy.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.strategy; 2 | 3 | /** 4 | * Description:创建一个接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:23 8 | */ 9 | public interface Strategy { 10 | 11 | int doOperation(int num1, int num2); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/bridge/DrawApi.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.bridge; 2 | 3 | /** 4 | * Description:创建桥接实现接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 13:53 8 | */ 9 | public interface DrawApi { 10 | 11 | void drawCircle(int radius, int x, int y); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/iterator/Iterator.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.iterator; 2 | 3 | /** 4 | * Description:创建接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 11:55 8 | */ 9 | public interface Iterator { 10 | 11 | boolean hasNext(); 12 | Object next(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/interceptingfilter/Filter.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.interceptingfilter; 2 | 3 | /** 4 | * Description:创建过滤器接口 Filter。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:10 8 | */ 9 | public interface Filter { 10 | 11 | void execute(String request); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/interpreter/Expression.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.interpreter; 2 | 3 | /** 4 | * Description:创建一个表达式接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 10:42 8 | */ 9 | public interface Expression { 10 | 11 | boolean interpret(String context); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/businessdelegate/BusinessService.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.businessdelegate; 2 | 3 | /** 4 | * Description:创建 BusinessService 接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:41 8 | */ 9 | public interface BusinessService { 10 | 11 | void doProcessing(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/servicelocator/Service.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.servicelocator; 2 | 3 | /** 4 | * Description:创建服务接口 Service。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:19 8 | */ 9 | public interface Service { 10 | 11 | String getName(); 12 | 13 | void execute(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/adapter/MediaPlayer.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.adapter; 2 | 3 | /** 4 | * Description:为媒体播放器和更高级的媒体播放器创建接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 10:57 8 | */ 9 | public interface MediaPlayer { 10 | 11 | void play(String audioType, String fileName); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/visitor/ComputerPart.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.visitor; 2 | 3 | /** 4 | * Description:定义一个表示元素的接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:04 8 | */ 9 | public interface ComputerPart { 10 | 11 | void accept(ComputerPartVisitor computerPartVisitor); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/builder/Bottle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 9:31 8 | */ 9 | public class Bottle implements Packing { 10 | 11 | @Override 12 | public String pack() { 13 | return "Bottle"; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/builder/Item.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | /** 4 | * Description:创建一个表示食物条目和食物包装的接口。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 9:20 8 | */ 9 | public interface Item { 10 | 11 | String name(); 12 | 13 | Packing packing(); 14 | 15 | float price(); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/frontcontroller/HomeView.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.frontcontroller; 2 | 3 | /** 4 | * Description:创建视图。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 17:57 8 | */ 9 | public class HomeView { 10 | 11 | public void show(){ 12 | System.out.println("Displaying Home Page"); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/observer/Observer.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.observer; 2 | 3 | /** 4 | * Description:创建 Observer 类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 14:14 8 | */ 9 | public abstract class Observer { 10 | 11 | protected Subject subject; 12 | 13 | public abstract void update(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/frontcontroller/StudentView.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.frontcontroller; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 17:57 8 | */ 9 | public class StudentView { 10 | 11 | public void show(){ 12 | System.out.println("Displaying Student Page"); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/adapter/AdvancedMediaPlayer.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.adapter; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 10:58 8 | */ 9 | public interface AdvancedMediaPlayer { 10 | 11 | void playVlc(String fileName); 12 | 13 | void playMp4(String fileName); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/filter/Criteria.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.filter; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Description:为标准(Criteria)创建一个接口。 7 | * 8 | * @author JourWon 9 | * @date 2019/7/15 17:30 10 | */ 11 | public interface Criteria { 12 | 13 | List meetCriteria(List persons); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/decorator/Circle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.decorator; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 9:28 8 | */ 9 | public class Circle implements Shape { 10 | 11 | @Override 12 | public void draw() { 13 | System.out.println("Shape: Circle"); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/facade/Circle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.facade; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 10:04 8 | */ 9 | public class Circle implements Shape { 10 | 11 | @Override 12 | public void draw() { 13 | System.out.println("Circle::draw()"); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/facade/Square.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.facade; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 10:04 8 | */ 9 | public class Square implements Shape { 10 | 11 | @Override 12 | public void draw() { 13 | System.out.println("Square::draw()"); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/builder/Wrapper.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | /** 4 | * Description:创建实现 Packing 接口的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 9:22 8 | */ 9 | public class Wrapper implements Packing { 10 | 11 | @Override 12 | public String pack() { 13 | return "Wrapper"; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/factory/Circle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.factory; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/12 15:36 8 | */ 9 | public class Circle implements Shape { 10 | 11 | @Override 12 | public void draw() { 13 | System.out.println("Inside Circle::draw() method."); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/interceptingfilter/Target.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.interceptingfilter; 2 | 3 | /** 4 | * Description:创建 Target。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:16 8 | */ 9 | public class Target { 10 | 11 | public void execute(String request){ 12 | System.out.println("Executing request: " + request); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/decorator/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.decorator; 2 | 3 | /** 4 | * Description:创建实现接口的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 9:28 8 | */ 9 | public class Rectangle implements Shape { 10 | 11 | @Override 12 | public void draw() { 13 | System.out.println("Shape: Rectangle"); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/facade/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.facade; 2 | 3 | /** 4 | * Description:创建实现接口的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 10:03 8 | */ 9 | public class Rectangle implements Shape { 10 | 11 | @Override 12 | public void draw() { 13 | System.out.println("Rectangle::draw()"); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/Blue.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 22:54 8 | */ 9 | public class Blue implements Color { 10 | 11 | @Override 12 | public void fill() { 13 | System.out.println("Inside Blue::fille() method."); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/Red.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 22:53 8 | */ 9 | public class Red implements Color { 10 | 11 | @Override 12 | public void fill() { 13 | System.out.println("Inside Red::fille() method."); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/Square.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 22:52 8 | */ 9 | public class Square implements Shape { 10 | 11 | @Override 12 | public void draw() { 13 | System.out.println("Inside Square::draw() method."); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/Circle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 22:52 8 | */ 9 | public class Circle implements Shape { 10 | 11 | @Override 12 | public void draw() { 13 | System.out.println("Inside Circle::draw() method."); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/Green.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 22:55 8 | */ 9 | public class Green implements Color { 10 | 11 | @Override 12 | public void fill() { 13 | System.out.println("Inside Green::fille() method."); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/factory/Square.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.factory; 2 | 3 | /** 4 | * Description:创建实现接口的正方形实体类 5 | * 6 | * @author JourWon 7 | * @date 2019/7/12 15:34 8 | */ 9 | public class Square implements Shape { 10 | 11 | @Override 12 | public void draw() { 13 | System.out.println("Inside Square::draw() method."); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/strategy/OperationAdd.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.strategy; 2 | 3 | /** 4 | * Description:创建实现接口的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:26 8 | */ 9 | public class OperationAdd implements Strategy { 10 | 11 | @Override 12 | public int doOperation(int num1, int num2) { 13 | return num1 + num2; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/strategy/OperationMultiply.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.strategy; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:27 8 | */ 9 | public class OperationMultiply implements Strategy { 10 | 11 | @Override 12 | public int doOperation(int num1, int num2) { 13 | return num1 * num2; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/strategy/OperationSubstract.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.strategy; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:26 8 | */ 9 | public class OperationSubstract implements Strategy { 10 | 11 | @Override 12 | public int doOperation(int num1, int num2) { 13 | return num1 - num2; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/visitor/Monitor.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.visitor; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:07 8 | */ 9 | public class Monitor implements ComputerPart { 10 | @Override 11 | public void accept(ComputerPartVisitor computerPartVisitor) { 12 | computerPartVisitor.visit(this); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/factory/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.factory; 2 | 3 | /** 4 | * Description:创建实现接口的长方形实体类 5 | * 6 | * @author JourWon 7 | * @date 2019/7/12 15:27 8 | */ 9 | public class Rectangle implements Shape { 10 | 11 | @Override 12 | public void draw() { 13 | System.out.println("Inside Rectangle::draw() method."); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/visitor/Mouse.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.visitor; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:08 8 | */ 9 | public class Mouse implements ComputerPart { 10 | 11 | @Override 12 | public void accept(ComputerPartVisitor computerPartVisitor) { 13 | computerPartVisitor.visit(this); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/nullobject/AbstractCustomer.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.nullobject; 2 | 3 | /** 4 | * Description:创建一个抽象类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:13 8 | */ 9 | public abstract class AbstractCustomer { 10 | 11 | protected String name; 12 | 13 | public abstract boolean isNil(); 14 | 15 | public abstract String getName(); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/AbstractFactory.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description:为 Color 和 Shape 对象创建抽象类来获取工厂。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 22:56 8 | */ 9 | public abstract class AbstractFactory { 10 | 11 | abstract Shape getShape(String shape); 12 | 13 | abstract Color getColor(String color); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/businessdelegate/JmsService.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.businessdelegate; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:43 8 | */ 9 | public class JmsService implements BusinessService { 10 | @Override 11 | public void doProcessing() { 12 | System.out.println("Processing task by invoking JMS Service"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/interceptingfilter/DebugFilter.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.interceptingfilter; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:13 8 | */ 9 | public class DebugFilter implements Filter { 10 | 11 | @Override 12 | public void execute(String request){ 13 | System.out.println("request log: " + request); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description:创建实现接口的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 22:51 8 | */ 9 | public class Rectangle implements Shape { 10 | 11 | @Override 12 | public void draw() { 13 | System.out.println("Inside Rectangle::draw() method."); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/businessdelegate/EjbService.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.businessdelegate; 2 | 3 | /** 4 | * Description:创建实体服务类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:42 8 | */ 9 | public class EjbService implements BusinessService { 10 | @Override 11 | public void doProcessing() { 12 | System.out.println("Processing task by invoking EJB Service"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Eclipse 5 | .project 6 | .classpath 7 | .settings/ 8 | 9 | # Intellij 10 | *.ipr 11 | *.iml 12 | *.iws 13 | .idea/ 14 | 15 | # Maven 16 | target/ 17 | 18 | # Gradle 19 | build 20 | .gradle 21 | 22 | # Log file 23 | *.log 24 | log/ 25 | 26 | # out 27 | **/out/ 28 | 29 | # Mac 30 | .DS_Store 31 | 32 | # others 33 | *.jar 34 | *.war 35 | *.zip 36 | *.tar 37 | *.tar.gz 38 | *.pid 39 | *.orig 40 | temp/ -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/builder/ColdDrink.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 9:38 8 | */ 9 | public abstract class ColdDrink implements Item { 10 | 11 | @Override 12 | public Packing packing() { 13 | return new Bottle(); 14 | } 15 | 16 | @Override 17 | public abstract float price(); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/visitor/Keyboard.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.visitor; 2 | 3 | /** 4 | * Description:创建扩展了 ComputerPart 的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:06 8 | */ 9 | public class Keyboard implements ComputerPart { 10 | 11 | @Override 12 | public void accept(ComputerPartVisitor computerPartVisitor) { 13 | computerPartVisitor.visit(this); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/builder/Coke.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 9:42 8 | */ 9 | public class Coke extends ColdDrink { 10 | 11 | @Override 12 | public String name() { 13 | return "Coke"; 14 | } 15 | 16 | @Override 17 | public float price() { 18 | return 30.0f; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/builder/Pepsi.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 9:42 8 | */ 9 | public class Pepsi extends ColdDrink { 10 | 11 | @Override 12 | public String name() { 13 | return "Pepsi"; 14 | } 15 | 16 | @Override 17 | public float price() { 18 | return 35.0f; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/bridge/Shape.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.bridge; 2 | 3 | /** 4 | * Description:使用 DrawApi 接口创建抽象类 Shape。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 13:56 8 | */ 9 | public abstract class Shape { 10 | 11 | protected DrawApi drawApi; 12 | 13 | protected Shape(DrawApi drawApi) { 14 | this.drawApi = drawApi; 15 | } 16 | 17 | public abstract void draw(); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/memento/Memento.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.memento; 2 | 3 | /** 4 | * Description:创建 Memento 类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/19 10:07 8 | */ 9 | public class Memento { 10 | 11 | private String state; 12 | 13 | public Memento(String state){ 14 | this.state = state; 15 | } 16 | 17 | public String getState(){ 18 | return state; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/interceptingfilter/AuthenticationFilter.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.interceptingfilter; 2 | 3 | /** 4 | * Description:创建实体过滤器。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:12 8 | */ 9 | public class AuthenticationFilter implements Filter { 10 | 11 | @Override 12 | public void execute(String request){ 13 | System.out.println("Authenticating request: " + request); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.jourwon.pattern 8 | design-pattern 9 | 1.0-SNAPSHOT 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/visitor/ComputerPartVisitor.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.visitor; 2 | 3 | 4 | /** 5 | * Description:定义一个表示访问者的接口。 6 | * 7 | * @author JourWon 8 | * @date 2019/7/17 16:06 9 | */ 10 | public interface ComputerPartVisitor { 11 | 12 | void visit(Computer computer); 13 | 14 | void visit(Mouse mouse); 15 | 16 | void visit(Keyboard keyboard); 17 | 18 | void visit(Monitor monitor); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/builder/Burger.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | /** 4 | * Description:创建实现 Item 接口的抽象类,该类提供了默认的功能。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 9:37 8 | */ 9 | public abstract class Burger implements Item { 10 | 11 | @Override 12 | public Packing packing() { 13 | return new Wrapper(); 14 | } 15 | 16 | @Override 17 | public abstract float price(); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/prototype/Circle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.prototype; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 10:20 8 | */ 9 | public class Circle extends Shape { 10 | 11 | public Circle(){ 12 | type = "Circle"; 13 | } 14 | 15 | @Override 16 | public void draw() { 17 | System.out.println("Inside Circle::draw() method."); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/prototype/Square.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.prototype; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 10:19 8 | */ 9 | public class Square extends Shape { 10 | 11 | public Square(){ 12 | type = "Square"; 13 | } 14 | 15 | @Override 16 | public void draw() { 17 | System.out.println("Inside Square::draw() method."); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/builder/ChickenBurger.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 9:41 8 | */ 9 | public class ChickenBurger extends Burger { 10 | 11 | @Override 12 | public String name() { 13 | return "Chicken Burger"; 14 | } 15 | 16 | @Override 17 | public float price() { 18 | return 50.5f; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/compositeentity/DependentObject2.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.compositeentity; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 17:18 8 | */ 9 | public class DependentObject2 { 10 | 11 | private String data; 12 | 13 | public void setData(String data){ 14 | this.data = data; 15 | } 16 | 17 | public String getData(){ 18 | return data; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/compositeentity/DependentObject1.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.compositeentity; 2 | 3 | /** 4 | * Description:创建依赖对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 17:17 8 | */ 9 | public class DependentObject1 { 10 | 11 | private String data; 12 | 13 | public void setData(String data){ 14 | this.data = data; 15 | } 16 | 17 | public String getData(){ 18 | return data; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/servicelocator/Service2.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.servicelocator; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:20 8 | */ 9 | public class Service2 implements Service { 10 | 11 | @Override 12 | public void execute(){ 13 | System.out.println("Executing Service2"); 14 | } 15 | 16 | @Override 17 | public String getName() { 18 | return "Service2"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/mediator/ChatRoom.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.mediator; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * Description:创建中介类。 7 | * 8 | * @author JourWon 9 | * @date 2019/7/17 14:07 10 | */ 11 | public class ChatRoom { 12 | 13 | public static void showMessage(User user, String message){ 14 | System.out.println(new Date().toString() 15 | + " [" + user.getName() +"] : " + message); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/builder/VegBurger.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | /** 4 | * Description:创建扩展了 Burger 和 ColdDrink 的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 9:40 8 | */ 9 | public class VegBurger extends Burger { 10 | 11 | @Override 12 | public String name() { 13 | return "Veg Burger"; 14 | } 15 | 16 | @Override 17 | public float price() { 18 | return 25.0f; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/bridge/GreenCircle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.bridge; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 13:55 8 | */ 9 | public class GreenCircle implements DrawApi { 10 | @Override 11 | public void drawCircle(int radius, int x, int y) { 12 | System.out.println("Drawing Circle[ color: green, radius: " 13 | + radius + ", x: " + x + ", y: " + y + "]"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/prototype/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.prototype; 2 | 3 | /** 4 | * Description:创建扩展了上面抽象类的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 10:19 8 | */ 9 | public class Rectangle extends Shape { 10 | 11 | public Rectangle(){ 12 | type = "Rectangle"; 13 | } 14 | 15 | @Override 16 | public void draw() { 17 | System.out.println("Inside Rectangle::draw() method."); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/chain/FileLogger.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.chain; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 14:26 8 | */ 9 | public class FileLogger extends AbstractLogger { 10 | 11 | public FileLogger(int level){ 12 | this.level = level; 13 | } 14 | 15 | @Override 16 | protected void write(String message) { 17 | System.out.println("File::Logger: " + message); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/command/SellStock.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.command; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 9:48 8 | */ 9 | public class SellStock implements Order { 10 | 11 | private Stock abcStock; 12 | 13 | public SellStock(Stock abcStock) { 14 | this.abcStock = abcStock; 15 | } 16 | 17 | @Override 18 | public void execute() { 19 | abcStock.sell(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/visitor/VisitorPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.visitor; 2 | 3 | /** 4 | * Description:使用 ComputerPartDisplayVisitor 来显示 Computer 的组成部分。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:15 8 | */ 9 | public class VisitorPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | ComputerPart computer = new Computer(); 13 | computer.accept(new ComputerPartDisplayVisitor()); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/dataaccessobject/StudentDao.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.dataaccessobject; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Description:创建数据访问对象接口。 7 | * 8 | * @author JourWon 9 | * @date 2019/7/17 17:50 10 | */ 11 | public interface StudentDao { 12 | 13 | List getAllStudents(); 14 | 15 | Student getStudent(int rollNo); 16 | 17 | void updateStudent(Student student); 18 | 19 | void deleteStudent(Student student); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/servicelocator/Service1.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.servicelocator; 2 | 3 | /** 4 | * Description:创建实体服务。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:20 8 | */ 9 | public class Service1 implements Service { 10 | 11 | @Override 12 | public void execute(){ 13 | System.out.println("Executing Service1"); 14 | } 15 | 16 | @Override 17 | public String getName() { 18 | return "Service1"; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/mvc/StudentView.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.mvc; 2 | 3 | /** 4 | * Description:创建视图。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:33 8 | */ 9 | public class StudentView { 10 | 11 | public void printStudentDetails(String studentName, String studentRollNo) { 12 | System.out.println("Student: "); 13 | System.out.println("Name: " + studentName); 14 | System.out.println("Roll No: " + studentRollNo); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/nullobject/NullCustomer.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.nullobject; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:15 8 | */ 9 | public class NullCustomer extends AbstractCustomer { 10 | 11 | @Override 12 | public String getName() { 13 | return "Not Available in Customer Database"; 14 | } 15 | 16 | @Override 17 | public boolean isNil() { 18 | return true; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/bridge/RedCircle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.bridge; 2 | 3 | /** 4 | * Description:创建实现了 DrawApi 接口的实体桥接实现类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 13:54 8 | */ 9 | public class RedCircle implements DrawApi { 10 | 11 | @Override 12 | public void drawCircle(int radius, int x, int y) { 13 | System.out.println("Drawing Circle[ color: red, radius: " 14 | + radius +", x: " +x+", y: "+ y +"]"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/chain/ErrorLogger.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.chain; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 14:25 8 | */ 9 | public class ErrorLogger extends AbstractLogger { 10 | 11 | public ErrorLogger(int level) { 12 | this.level = level; 13 | } 14 | 15 | @Override 16 | protected void write(String message) { 17 | System.out.println("Error Console::Logger: " + message); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/command/BuyStock.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.command; 2 | 3 | /** 4 | * Description:创建实现了 Order 接口的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 9:47 8 | */ 9 | public class BuyStock implements Order { 10 | 11 | private Stock abcStock; 12 | 13 | public BuyStock(Stock abcStock) { 14 | this.abcStock = abcStock; 15 | } 16 | 17 | 18 | @Override 19 | public void execute() { 20 | abcStock.buy(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/businessdelegate/Client.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.businessdelegate; 2 | 3 | /** 4 | * Description:创建客户端。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:48 8 | */ 9 | public class Client { 10 | 11 | BusinessDelegate businessService; 12 | 13 | public Client(BusinessDelegate businessService){ 14 | this.businessService = businessService; 15 | } 16 | 17 | public void doTask(){ 18 | businessService.doTask(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/facade/FacadePatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.facade; 2 | 3 | /** 4 | * Description:使用该外观类画出各种类型的形状。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 10:07 8 | */ 9 | public class FacadePatternDemo { 10 | 11 | public static void main(String[] args) { 12 | ShapeMaker shapeMaker = new ShapeMaker(); 13 | 14 | shapeMaker.drawCircle(); 15 | shapeMaker.drawRectangle(); 16 | shapeMaker.drawSquare(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/state/Context.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.state; 2 | 3 | /** 4 | * Description:创建 Context 类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 14:49 8 | */ 9 | public class Context { 10 | 11 | private State state; 12 | 13 | public Context() { 14 | state = null; 15 | } 16 | 17 | public State getState() { 18 | return state; 19 | } 20 | 21 | public void setState(State state) { 22 | this.state = state; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/strategy/Context.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.strategy; 2 | 3 | /** 4 | * Description:创建 Context 类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:28 8 | */ 9 | public class Context { 10 | 11 | private Strategy strategy; 12 | 13 | public Context(Strategy strategy){ 14 | this.strategy = strategy; 15 | } 16 | 17 | public int executeStrategy(int num1, int num2){ 18 | return strategy.doOperation(num1, num2); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/businessdelegate/BusinessLookUp.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.businessdelegate; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:44 8 | */ 9 | public class BusinessLookUp { 10 | 11 | public BusinessService getBusinessService(String serviceType) { 12 | if (serviceType.equalsIgnoreCase("EJB")) { 13 | return new EjbService(); 14 | } else { 15 | return new JmsService(); 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/template/TemplatePatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.template; 2 | 3 | /** 4 | * Description:使用 Game 的模板方法 play() 来演示游戏的定义方式。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:59 8 | */ 9 | public class TemplatePatternDemo { 10 | 11 | public static void main(String[] args) { 12 | Game game = new Cricket(); 13 | game.play(); 14 | System.out.println(); 15 | 16 | game = new Football(); 17 | game.play(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/chain/ConsoleLogger.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.chain; 2 | 3 | /** 4 | * Description:创建扩展了该记录器类的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 14:22 8 | */ 9 | public class ConsoleLogger extends AbstractLogger { 10 | 11 | public ConsoleLogger(int level) { 12 | this.level = level; 13 | } 14 | 15 | @Override 16 | protected void write(String message) { 17 | System.out.println("Standard Console::Logger: " + message); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/adapter/Mp4Player.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.adapter; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 11:00 8 | */ 9 | public class Mp4Player implements AdvancedMediaPlayer { 10 | 11 | @Override 12 | public void playVlc(String fileName) { 13 | //什么也不做 14 | } 15 | 16 | @Override 17 | public void playMp4(String fileName) { 18 | System.out.println("Playing mp4 file. Name: " + fileName); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/state/StopState.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.state; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 14:52 8 | */ 9 | public class StopState implements State { 10 | 11 | @Override 12 | public void doAction(Context context) { 13 | System.out.println("Player is in stop state"); 14 | context.setState(this); 15 | } 16 | 17 | @Override 18 | public String toString(){ 19 | return "Stop State"; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/mediator/MediatorPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.mediator; 2 | 3 | /** 4 | * Description:使用 User 对象来显示他们之间的通信。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 14:08 8 | */ 9 | public class MediatorPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | User robert = new User("Robert"); 13 | User john = new User("John"); 14 | 15 | robert.sendMessage("Hi! John!"); 16 | john.sendMessage("Hello! Robert!"); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/compositeentity/CompositeEntity.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.compositeentity; 2 | 3 | /** 4 | * Description:创建组合实体。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 17:28 8 | */ 9 | public class CompositeEntity { 10 | 11 | private CoarseGrainedObject cgo = new CoarseGrainedObject(); 12 | 13 | public void setData(String data1, String data2){ 14 | cgo.setData(data1, data2); 15 | } 16 | 17 | public String[] getData(){ 18 | return cgo.getData(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/interceptingfilter/Client.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.interceptingfilter; 2 | 3 | /** 4 | * Description:创建客户端 Client。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:18 8 | */ 9 | public class Client { 10 | 11 | FilterManager filterManager; 12 | 13 | public void setFilterManager(FilterManager filterManager){ 14 | this.filterManager = filterManager; 15 | } 16 | 17 | public void sendRequest(String request){ 18 | filterManager.filterRequest(request); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/state/StartState.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.state; 2 | 3 | /** 4 | * Description:创建实现接口的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 14:50 8 | */ 9 | public class StartState implements State { 10 | 11 | @Override 12 | public void doAction(Context context) { 13 | System.out.println("Player is in start state"); 14 | context.setState(this); 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return "Start State"; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/adapter/VlcPlayer.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.adapter; 2 | 3 | /** 4 | * Description:创建实现了 AdvancedMediaPlayer 接口的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 10:59 8 | */ 9 | public class VlcPlayer implements AdvancedMediaPlayer { 10 | 11 | @Override 12 | public void playVlc(String fileName) { 13 | System.out.println("Playing vlc file. Name: " + fileName); 14 | } 15 | 16 | @Override 17 | public void playMp4(String fileName) { 18 | // 什么也不做 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/decorator/ShapeDecorator.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.decorator; 2 | 3 | /** 4 | * Description:创建实现了 Shape 接口的抽象装饰类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 9:29 8 | */ 9 | public abstract class ShapeDecorator implements Shape { 10 | 11 | protected Shape decoratorShape; 12 | 13 | public ShapeDecorator(Shape decoratorShape) { 14 | this.decoratorShape = decoratorShape; 15 | } 16 | 17 | @Override 18 | public void draw() { 19 | decoratorShape.draw(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/proxy/ProxyPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.proxy; 2 | 3 | /** 4 | * Description:当被请求时,使用 ProxyImage 来获取 RealImage 类的对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 14:05 8 | */ 9 | public class ProxyPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | Image image = new ProxyImage("test.png"); 13 | 14 | //图像将从磁盘加载 15 | image.display(); 16 | System.out.println(""); 17 | 18 | //图像将无法从磁盘加载 19 | image.display(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/bridge/BridgePatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.bridge; 2 | 3 | /** 4 | * Description:使用 Shape 和 DrawApi 类画出不同颜色的圆。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 16:39 8 | */ 9 | public class BridgePatternDemo { 10 | 11 | public static void main(String[] args) { 12 | Shape redCircle = new Circle(100, 100, 10, new RedCircle()); 13 | Shape greenCircle = new Circle(100, 100, 10, new GreenCircle()); 14 | 15 | redCircle.draw(); 16 | greenCircle.draw(); 17 | } 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/memento/CareTaker.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.memento; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description:创建 CareTaker 类。 8 | * 9 | * @author JourWon 10 | * @date 2019/7/19 10:08 11 | */ 12 | public class CareTaker { 13 | 14 | private List mementoList = new ArrayList(); 15 | 16 | public void add(Memento state){ 17 | mementoList.add(state); 18 | } 19 | 20 | public Memento get(int index){ 21 | return mementoList.get(index); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/nullobject/RealCustomer.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.nullobject; 2 | 3 | /** 4 | * Description:创建扩展了 AbstractCustomer 的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:13 8 | */ 9 | public class RealCustomer extends AbstractCustomer { 10 | 11 | public RealCustomer(String name) { 12 | this.name = name; 13 | } 14 | 15 | @Override 16 | public boolean isNil() { 17 | return false; 18 | } 19 | 20 | @Override 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/observer/OctalObserver.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.observer; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 14:18 8 | */ 9 | public class OctalObserver extends Observer { 10 | 11 | public OctalObserver(Subject subject){ 12 | this.subject = subject; 13 | this.subject.attach(this); 14 | } 15 | 16 | @Override 17 | public void update() { 18 | System.out.println( "Octal String: " 19 | + Integer.toOctalString( subject.getState() ) ); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/frontcontroller/FrontControllerPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.frontcontroller; 2 | 3 | /** 4 | * Description:使用 FrontController 来演示前端控制器设计模式。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:05 8 | */ 9 | public class FrontControllerPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | FrontController frontController = new FrontController(); 13 | frontController.dispatchRequest("HOME"); 14 | System.out.println(); 15 | 16 | frontController.dispatchRequest("STUDENT"); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/observer/HexaObserver.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.observer; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 14:19 8 | */ 9 | public class HexaObserver extends Observer { 10 | 11 | public HexaObserver(Subject subject){ 12 | this.subject = subject; 13 | this.subject.attach(this); 14 | } 15 | 16 | @Override 17 | public void update() { 18 | System.out.println( "Hex String: " 19 | + Integer.toHexString( subject.getState() ).toUpperCase() ); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/command/Stock.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.command; 2 | 3 | /** 4 | * Description:创建一个请求类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 9:43 8 | */ 9 | public class Stock { 10 | 11 | private String name = "ABC"; 12 | private int quantity = 10; 13 | 14 | public void buy() { 15 | System.out.println("Stock [ Name: " + name + ", Quantity:" + quantity + " ]bought "); 16 | } 17 | 18 | public void sell() { 19 | System.out.println("Stock [ Name: " + name + ", Quantity:" + quantity + " ]sold "); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/observer/BinaryObserver.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.observer; 2 | 3 | /** 4 | * Description:创建实体观察者类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 14:17 8 | */ 9 | public class BinaryObserver extends Observer { 10 | 11 | public BinaryObserver(Subject subject) { 12 | this.subject = subject; 13 | this.subject.attach(this); 14 | } 15 | 16 | @Override 17 | public void update() { 18 | System.out.println("Binary String: " 19 | + Integer.toBinaryString(subject.getState())); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/template/Game.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.template; 2 | 3 | /** 4 | * Description:创建一个抽象类,它的模板方法被设置为 final。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:56 8 | */ 9 | public abstract class Game { 10 | 11 | abstract void initialize(); 12 | 13 | abstract void startPlay(); 14 | 15 | abstract void endPlay(); 16 | 17 | //模板 18 | public final void play() { 19 | //初始化游戏 20 | initialize(); 21 | 22 | //开始游戏 23 | startPlay(); 24 | 25 | //结束游戏 26 | endPlay(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/bridge/Circle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.bridge; 2 | 3 | /** 4 | * Description:创建实现了 Shape 接口的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 13:57 8 | */ 9 | public class Circle extends Shape { 10 | 11 | int x, y, radius; 12 | 13 | protected Circle(int x, int y, int radius, DrawApi drawApi) { 14 | super(drawApi); 15 | this.x = x; 16 | this.y = y; 17 | this.radius = radius; 18 | } 19 | 20 | @Override 21 | public void draw() { 22 | drawApi.drawCircle(radius, x, y); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/compositeentity/CompositeEntityPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.compositeentity; 2 | 3 | /** 4 | * Description:使用 Client 来演示组合实体设计模式的用法。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 17:30 8 | */ 9 | public class CompositeEntityPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | Client client = new Client(); 13 | client.setData("Test", "Data"); 14 | client.printData(); 15 | System.out.println(); 16 | 17 | client.setData("Second Test", "Data1"); 18 | client.printData(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/interpreter/TerminalExpression.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.interpreter; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 10:43 8 | */ 9 | public class TerminalExpression implements Expression { 10 | 11 | private String data; 12 | 13 | public TerminalExpression(String data) { 14 | this.data = data; 15 | } 16 | 17 | @Override 18 | public boolean interpret(String context) { 19 | if (context.contains(data)) { 20 | return true; 21 | } 22 | 23 | return false; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/iterator/IteratorPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.iterator; 2 | 3 | /** 4 | * Description:使用 NameRepository 来获取迭代器,并打印名字。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 14:02 8 | */ 9 | public class IteratorPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | NameRepository namesRepository = new NameRepository(); 13 | 14 | for (Iterator iter = namesRepository.getIterator(); iter.hasNext(); ) { 15 | String name = (String) iter.next(); 16 | System.out.println("Name : " + name); 17 | } 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/mvc/Student.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.mvc; 2 | 3 | /** 4 | * Description:创建模型。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:32 8 | */ 9 | public class Student { 10 | 11 | private String rollNo; 12 | private String name; 13 | 14 | public String getRollNo() { 15 | return rollNo; 16 | } 17 | 18 | public void setRollNo(String rollNo) { 19 | this.rollNo = rollNo; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/adapter/AdapterPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.adapter; 2 | 3 | /** 4 | * Description:使用 AudioPlayer 来播放不同类型的音频格式。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 11:13 8 | */ 9 | public class AdapterPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | AudioPlayer audioPlayer = new AudioPlayer(); 13 | 14 | audioPlayer.play("mp3", "beyond the horizon.mp3"); 15 | audioPlayer.play("mp4", "alone.mp4"); 16 | audioPlayer.play("vlc", "far far away.vlc"); 17 | audioPlayer.play("avi", "mind me.avi"); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/proxy/ProxyImage.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.proxy; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 14:02 8 | */ 9 | public class ProxyImage implements Image { 10 | 11 | private RealImage realImage; 12 | 13 | private String fileName; 14 | 15 | public ProxyImage(String fileName) { 16 | this.fileName = fileName; 17 | } 18 | 19 | @Override 20 | public void display() { 21 | if (realImage == null) { 22 | realImage = new RealImage(fileName); 23 | } 24 | realImage.display(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/mediator/User.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.mediator; 2 | 3 | /** 4 | * Description:创建 user 类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 14:07 8 | */ 9 | public class User { 10 | 11 | private String name; 12 | 13 | public String getName() { 14 | return name; 15 | } 16 | 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | 21 | public User(String name){ 22 | this.name = name; 23 | } 24 | 25 | public void sendMessage(String message){ 26 | ChatRoom.showMessage(this,message); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/command/Broker.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.command; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description:创建命令调用类。 8 | * 9 | * @author JourWon 10 | * @date 2019/7/17 10:20 11 | */ 12 | public class Broker { 13 | 14 | private List orderList = new ArrayList<>(); 15 | 16 | public void takeOrder(Order order) { 17 | orderList.add(order); 18 | } 19 | 20 | public void placeOrders() { 21 | for (Order order : orderList) { 22 | order.execute(); 23 | } 24 | orderList.clear(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/template/Cricket.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.template; 2 | 3 | /** 4 | * Description:创建扩展了上述类的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:58 8 | */ 9 | public class Cricket extends Game { 10 | 11 | @Override 12 | void endPlay() { 13 | System.out.println("Cricket Game Finished!"); 14 | } 15 | 16 | @Override 17 | void initialize() { 18 | System.out.println("Cricket Game Initialized! Start playing."); 19 | } 20 | 21 | @Override 22 | void startPlay() { 23 | System.out.println("Cricket Game Started. Enjoy the game!"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/template/Football.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.template; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:59 8 | */ 9 | public class Football extends Game { 10 | 11 | @Override 12 | void endPlay() { 13 | System.out.println("Football Game Finished!"); 14 | } 15 | 16 | @Override 17 | void initialize() { 18 | System.out.println("Football Game Initialized! Start playing."); 19 | } 20 | 21 | @Override 22 | void startPlay() { 23 | System.out.println("Football Game Started. Enjoy the game!"); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/compositeentity/CoarseGrainedObject.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.compositeentity; 2 | 3 | /** 4 | * Description:创建粗粒度对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 17:18 8 | */ 9 | public class CoarseGrainedObject { 10 | 11 | DependentObject1 do1 = new DependentObject1(); 12 | 13 | DependentObject2 do2 = new DependentObject2(); 14 | 15 | public void setData(String data1, String data2) { 16 | do1.setData(data1); 17 | do2.setData(data2); 18 | } 19 | 20 | public String[] getData() { 21 | return new String[]{do1.getData(), do2.getData()}; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/interceptingfilter/FilterManager.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.interceptingfilter; 2 | 3 | /** 4 | * Description:创建过滤管理器。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:18 8 | */ 9 | public class FilterManager { 10 | 11 | FilterChain filterChain; 12 | 13 | public FilterManager(Target target){ 14 | filterChain = new FilterChain(); 15 | filterChain.setTarget(target); 16 | } 17 | public void setFilter(Filter filter){ 18 | filterChain.addFilter(filter); 19 | } 20 | 21 | public void filterRequest(String request){ 22 | filterChain.execute(request); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/interpreter/OrExpression.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.interpreter; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 11:03 8 | */ 9 | public class OrExpression implements Expression { 10 | 11 | private Expression expr1 = null; 12 | private Expression expr2 = null; 13 | 14 | public OrExpression(Expression expr1, Expression expr2) { 15 | this.expr1 = expr1; 16 | this.expr2 = expr2; 17 | } 18 | 19 | @Override 20 | public boolean interpret(String context) { 21 | return expr1.interpret(context) || expr2.interpret(context); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/nullobject/CustomerFactory.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.nullobject; 2 | 3 | /** 4 | * Description:创建 CustomerFactory 类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:15 8 | */ 9 | public class CustomerFactory { 10 | 11 | public static final String[] names = {"Rob", "Joe", "Julie"}; 12 | 13 | public static AbstractCustomer getCustomer(String name) { 14 | for (int i = 0; i < names.length; i++) { 15 | if (names[i].equalsIgnoreCase(name)) { 16 | return new RealCustomer(name); 17 | } 18 | } 19 | return new NullCustomer(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/singleton/SingleObject.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.singleton; 2 | 3 | /** 4 | * Description:创建一个 Singleton 类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 23:35 8 | */ 9 | public class SingleObject { 10 | 11 | //创建 SingleObject 的一个对象 12 | private static SingleObject instance = new SingleObject(); 13 | 14 | //让构造函数为 private,这样该类就不会被实例化 15 | private SingleObject() {} 16 | 17 | //获取唯一可用的对象 18 | public static SingleObject getInstance() { 19 | return instance; 20 | } 21 | 22 | public void showMessage() { 23 | System.out.println("Hello Singleton."); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/compositeentity/Client.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.compositeentity; 2 | 3 | /** 4 | * Description:创建使用组合实体的客户端类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 17:30 8 | */ 9 | public class Client { 10 | 11 | private CompositeEntity compositeEntity = new CompositeEntity(); 12 | 13 | public void printData() { 14 | for (int i = 0; i < compositeEntity.getData().length; i++) { 15 | System.out.println("Data: " + compositeEntity.getData()[i]); 16 | } 17 | } 18 | 19 | public void setData(String data1, String data2) { 20 | compositeEntity.setData(data1, data2); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/command/CommandPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.command; 2 | 3 | /** 4 | * Description:使用 Broker 类来接受并执行命令。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 10:28 8 | */ 9 | public class CommandPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | Stock stock = new Stock(); 13 | 14 | BuyStock buyStockOrder = new BuyStock(stock); 15 | SellStock sellStock = new SellStock(stock); 16 | 17 | Broker broker = new Broker(); 18 | broker.takeOrder(buyStockOrder); 19 | broker.takeOrder(sellStock); 20 | 21 | broker.placeOrders(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/interpreter/AndExpression.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.interpreter; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 11:01 8 | */ 9 | public class AndExpression implements Expression { 10 | 11 | private Expression expr1 = null; 12 | private Expression expr2 = null; 13 | 14 | public AndExpression(Expression expr1, Expression expr2) { 15 | this.expr1 = expr1; 16 | this.expr2 = expr2; 17 | } 18 | 19 | @Override 20 | public boolean interpret(String context) { 21 | return expr1.interpret(context) && expr2.interpret(context); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/memento/Originator.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.memento; 2 | 3 | /** 4 | * Description:创建 Originator 类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/19 10:07 8 | */ 9 | public class Originator { 10 | 11 | private String state; 12 | 13 | public void setState(String state){ 14 | this.state = state; 15 | } 16 | 17 | public String getState(){ 18 | return state; 19 | } 20 | 21 | public Memento saveStateToMemento(){ 22 | return new Memento(state); 23 | } 24 | 25 | public void getStateFromMemento(Memento Memento){ 26 | state = Memento.getState(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/singleton/Singleton6.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.singleton; 2 | 3 | /** 4 | * Description:枚举 5 | * JDK 版本:JDK1.5 起 6 | * 是否 Lazy 初始化:否 7 | * 是否多线程安全:是 8 | * 实现难度:易 9 | * 10 | * 描述:这种实现方式还没有被广泛采用,但这是实现单例模式的最佳方法。它更简洁,自动支持序列化机制,绝对防止多次实例化。 11 | * 这种方式是 Effective Java 作者 Josh Bloch 提倡的方式,它不仅能避免多线程同步问题,而且还自动支持序列化机制,防止反序列化重新创建新的对象,绝对防止多次实例化。不过,由于 JDK1.5 之后才加入 enum 特性,用这种方式写不免让人感觉生疏,在实际工作中,也很少用。 12 | * 不能通过 reflection attack 来调用私有构造方法。 13 | * 14 | * @author JourWon 15 | * @date 2019/7/14 0:02 16 | */ 17 | public enum Singleton6 { 18 | 19 | INSTANCE; 20 | 21 | public void whateverMethod() { 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/frontcontroller/Dispatcher.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.frontcontroller; 2 | 3 | /** 4 | * Description:创建调度器 Dispatcher。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 17:58 8 | */ 9 | public class Dispatcher { 10 | 11 | private StudentView studentView; 12 | private HomeView homeView; 13 | public Dispatcher(){ 14 | studentView = new StudentView(); 15 | homeView = new HomeView(); 16 | } 17 | 18 | public void dispatch(String request){ 19 | if(request.equalsIgnoreCase("STUDENT")){ 20 | studentView.show(); 21 | }else{ 22 | homeView.show(); 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/decorator/RedShapeDecorator.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.decorator; 2 | 3 | /** 4 | * Description:创建扩展了 ShapeDecorator 类的实体装饰类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 9:31 8 | */ 9 | public class RedShapeDecorator extends ShapeDecorator { 10 | 11 | public RedShapeDecorator(Shape decoratorShape) { 12 | super(decoratorShape); 13 | } 14 | 15 | @Override 16 | public void draw() { 17 | decoratorShape.draw(); 18 | setRedBorder(decoratorShape); 19 | } 20 | 21 | private void setRedBorder(Shape decoratorShape) { 22 | System.out.println("Border Color: Red"); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/proxy/RealImage.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.proxy; 2 | 3 | /** 4 | * Description:创建实现接口的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 13:45 8 | */ 9 | public class RealImage implements Image { 10 | 11 | private String fileName; 12 | 13 | public RealImage(String fileName) { 14 | this.fileName = fileName; 15 | loadFromDisk(fileName); 16 | } 17 | 18 | @Override 19 | public void display() { 20 | System.out.println("Displaying " + fileName); 21 | } 22 | 23 | private void loadFromDisk(String fileName) { 24 | System.out.println("Loading " + fileName); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/visitor/Computer.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.visitor; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:11 8 | */ 9 | public class Computer implements ComputerPart { 10 | 11 | ComputerPart[] parts; 12 | 13 | public Computer() { 14 | parts = new ComputerPart[] {new Mouse(), new Keyboard(), new Monitor()}; 15 | } 16 | 17 | @Override 18 | public void accept(ComputerPartVisitor computerPartVisitor) { 19 | for (int i = 0; i < parts.length; i++) { 20 | parts[i].accept(computerPartVisitor); 21 | } 22 | computerPartVisitor.visit(this); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/builder/MealBuilder.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | /** 4 | * Description:创建一个 MealBuilder 类,实际的 builder 类负责创建 Meal 对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 9:52 8 | */ 9 | public class MealBuilder { 10 | 11 | public Meal prepareVegMeal() { 12 | Meal meal = new Meal(); 13 | meal.addItem(new VegBurger()); 14 | meal.addItem(new Coke()); 15 | return meal; 16 | } 17 | 18 | public Meal prepareNonVegMeal() { 19 | Meal meal = new Meal(); 20 | meal.addItem(new ChickenBurger()); 21 | meal.addItem(new Pepsi()); 22 | return meal; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/businessdelegate/BusinessDelegate.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.businessdelegate; 2 | 3 | /** 4 | * Description:创建业务代表。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:45 8 | */ 9 | public class BusinessDelegate { 10 | 11 | private BusinessLookUp lookupService = new BusinessLookUp(); 12 | private BusinessService businessService; 13 | private String serviceType; 14 | 15 | public void setServiceType(String serviceType) { 16 | this.serviceType = serviceType; 17 | } 18 | 19 | public void doTask() { 20 | businessService = lookupService.getBusinessService(serviceType); 21 | businessService.doProcessing(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/interceptingfilter/InterceptingFilterPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.interceptingfilter; 2 | 3 | /** 4 | * Description:使用 Client 来演示拦截过滤器设计模式。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:18 8 | */ 9 | public class InterceptingFilterPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | FilterManager filterManager = new FilterManager(new Target()); 13 | filterManager.setFilter(new AuthenticationFilter()); 14 | filterManager.setFilter(new DebugFilter()); 15 | 16 | Client client = new Client(); 17 | client.setFilterManager(filterManager); 18 | client.sendRequest("HOME"); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/filter/CriteriaFemale.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.filter; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description: 8 | * 9 | * @author JourWon 10 | * @date 2019/7/15 17:38 11 | */ 12 | public class CriteriaFemale implements Criteria { 13 | 14 | @Override 15 | public List meetCriteria(List persons) { 16 | List femalePersons = new ArrayList(); 17 | for (Person person : persons) { 18 | if(person.getGender().equalsIgnoreCase("FEMALE")){ 19 | femalePersons.add(person); 20 | } 21 | } 22 | return femalePersons; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/filter/CriteriaMale.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.filter; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description: 8 | * 9 | * @author JourWon 10 | * @date 2019/7/15 17:36 11 | */ 12 | public class CriteriaMale implements Criteria { 13 | 14 | @Override 15 | public List meetCriteria(List persons) { 16 | List malePersons = new ArrayList<>(); 17 | 18 | for (Person person : persons) { 19 | if (person.getGender().equalsIgnoreCase("Male")) { 20 | malePersons.add(person); 21 | } 22 | } 23 | 24 | return malePersons; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/singleton/Singleton1.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.singleton; 2 | 3 | /** 4 | * Description:懒汉式,线程不安全 5 | * 是否 Lazy 初始化:是 6 | * 是否多线程安全:否 7 | * 实现难度:易 8 | * 9 | * 描述:这种方式是最基本的实现方式,这种实现最大的问题就是不支持多线程。因为没有加锁 synchronized,所以严格意义上它并不算单例模式。 10 | * 这种方式 lazy loading 很明显,不要求线程安全,在多线程不能正常工作。 11 | * 12 | * @author JourWon 13 | * @date 2019/7/13 23:44 14 | */ 15 | public class Singleton1 { 16 | 17 | private static Singleton1 instance; 18 | 19 | private Singleton1() {} 20 | 21 | public static Singleton1 getInstance() { 22 | if (instance == null) { 23 | instance = new Singleton1(); 24 | } 25 | 26 | return instance; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/singleton/SingletonPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.singleton; 2 | 3 | /** 4 | * Description:从 singleton 类获取唯一的对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 23:38 8 | */ 9 | public class SingletonPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | //不合法的构造函数 13 | //编译时错误:构造函数 SingleObject() 是不可见的 14 | //SingleObject object = new SingleObject(); 15 | 16 | //获取唯一可用的对象 17 | SingleObject object1 = SingleObject.getInstance(); 18 | SingleObject object2 = SingleObject.getInstance(); 19 | 20 | System.out.println(object1); 21 | System.out.println(object2); 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/businessdelegate/BusinessDelegatePatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.businessdelegate; 2 | 3 | /** 4 | * Description:使用 BusinessDelegate 和 Client 类来演示业务代表模式。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:49 8 | */ 9 | public class BusinessDelegatePatternDemo { 10 | 11 | public static void main(String[] args) { 12 | 13 | BusinessDelegate businessDelegate = new BusinessDelegate(); 14 | businessDelegate.setServiceType("EJB"); 15 | 16 | Client client = new Client(businessDelegate); 17 | client.doTask(); 18 | System.out.println(); 19 | 20 | businessDelegate.setServiceType("JMS"); 21 | client.doTask(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/FactoryProducer.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description:创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 23:05 8 | */ 9 | public class FactoryProducer { 10 | 11 | public static AbstractFactory getFactory(String choice) { 12 | if (choice == null) { 13 | return null; 14 | } 15 | 16 | if ("shape".equalsIgnoreCase(choice)) { 17 | return new ShapeFactory(); 18 | } else if ("color".equalsIgnoreCase(choice)) { 19 | return new ColorFactory(); 20 | } else { 21 | return null; 22 | } 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/filter/CriteriaSingle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.filter; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description: 8 | * 9 | * @author JourWon 10 | * @date 2019/7/15 17:39 11 | */ 12 | public class CriteriaSingle implements Criteria { 13 | 14 | @Override 15 | public List meetCriteria(List persons) { 16 | List singlePersons = new ArrayList(); 17 | for (Person person : persons) { 18 | if(person.getMaritalStatus().equalsIgnoreCase("SINGLE")){ 19 | singlePersons.add(person); 20 | } 21 | } 22 | return singlePersons; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/observer/ObserverPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.observer; 2 | 3 | /** 4 | * Description:使用 Subject 和实体观察者对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 14:19 8 | */ 9 | public class ObserverPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | Subject subject = new Subject(); 13 | 14 | new BinaryObserver(subject); 15 | new HexaObserver(subject); 16 | new OctalObserver(subject); 17 | 18 | System.out.println("First state change: 15"); 19 | subject.setState(15); 20 | System.out.println(); 21 | 22 | System.out.println("Second state change: 10"); 23 | subject.setState(10); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/servicelocator/InitialContext.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.servicelocator; 2 | 3 | /** 4 | * Description:为 JNDI 查询创建 InitialContext。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:21 8 | */ 9 | public class InitialContext { 10 | 11 | public Object lookup(String jndiName){ 12 | if(jndiName.equalsIgnoreCase("SERVICE1")){ 13 | System.out.println("Looking up and creating a new Service1 object"); 14 | return new Service1(); 15 | }else if (jndiName.equalsIgnoreCase("SERVICE2")){ 16 | System.out.println("Looking up and creating a new Service2 object"); 17 | return new Service2(); 18 | } 19 | return null; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/facade/ShapeMaker.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.facade; 2 | 3 | /** 4 | * Description:创建一个外观类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 10:05 8 | */ 9 | public class ShapeMaker { 10 | 11 | private Shape circle; 12 | private Shape square; 13 | private Shape rectangle; 14 | 15 | public ShapeMaker() { 16 | circle = new Circle(); 17 | square = new Square(); 18 | rectangle = new Rectangle(); 19 | } 20 | 21 | public void drawCircle() { 22 | circle.draw(); 23 | } 24 | 25 | public void drawSquare() { 26 | square.draw(); 27 | } 28 | 29 | public void drawRectangle() { 30 | rectangle.draw(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/state/StatePatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.state; 2 | 3 | /** 4 | * Description:使用 Context 来查看当状态 State 改变时的行为变化。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:06 8 | */ 9 | public class StatePatternDemo { 10 | 11 | public static void main(String[] args) { 12 | Context context = new Context(); 13 | 14 | StartState startState = new StartState(); 15 | startState.doAction(context); 16 | 17 | System.out.println(context.getState().toString()); 18 | System.out.println(); 19 | 20 | StopState stopState = new StopState(); 21 | stopState.doAction(context); 22 | 23 | System.out.println(context.getState().toString()); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/filter/AndCriteria.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.filter; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Description: 7 | * 8 | * @author JourWon 9 | * @date 2019/7/15 17:40 10 | */ 11 | public class AndCriteria implements Criteria { 12 | 13 | private Criteria criteria; 14 | 15 | private Criteria otherCriteria; 16 | 17 | public AndCriteria(Criteria criteria, Criteria otherCriteria) { 18 | this.criteria = criteria; 19 | this.otherCriteria = otherCriteria; 20 | } 21 | 22 | @Override 23 | public List meetCriteria(List persons) { 24 | List list = criteria.meetCriteria(persons); 25 | return otherCriteria.meetCriteria(list); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/dataaccessobject/Student.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.dataaccessobject; 2 | 3 | /** 4 | * Description:创建数值对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 17:50 8 | */ 9 | public class Student { 10 | 11 | private String name; 12 | private int rollNo; 13 | 14 | Student(String name, int rollNo){ 15 | this.name = name; 16 | this.rollNo = rollNo; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | public int getRollNo() { 28 | return rollNo; 29 | } 30 | 31 | public void setRollNo(int rollNo) { 32 | this.rollNo = rollNo; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/transferobject/StudentVO.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.transferobject; 2 | 3 | /** 4 | * Description:创建传输对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/19 13:39 8 | */ 9 | public class StudentVO { 10 | 11 | private String name; 12 | private int rollNo; 13 | 14 | StudentVO(String name, int rollNo){ 15 | this.name = name; 16 | this.rollNo = rollNo; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | public int getRollNo() { 28 | return rollNo; 29 | } 30 | 31 | public void setRollNo(int rollNo) { 32 | this.rollNo = rollNo; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/strategy/StrategyPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.strategy; 2 | 3 | /** 4 | * Description:使用 Context 来查看当它改变策略 Strategy 时的行为变化。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:29 8 | */ 9 | public class StrategyPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | Context context = new Context(new OperationAdd()); 13 | System.out.println("10 + 5 = " + context.executeStrategy(10, 5)); 14 | 15 | context = new Context(new OperationSubstract()); 16 | System.out.println("10 - 5 = " + context.executeStrategy(10, 5)); 17 | 18 | context = new Context(new OperationMultiply()); 19 | System.out.println("10 * 5 = " + context.executeStrategy(10, 5)); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/singleton/Singleton3.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.singleton; 2 | 3 | /** 4 | * Description:饿汉式 5 | * 是否 Lazy 初始化:否 6 | * 是否多线程安全:是 7 | * 实现难度:易 8 | * 9 | * 描述:这种方式比较常用,但容易产生垃圾对象。 10 | * 优点:没有加锁,执行效率会提高。 11 | * 缺点:类加载时就初始化,浪费内存。 12 | * 它基于 classloder 机制避免了多线程的同步问题,不过,instance 在类装载时就实例化, 13 | * 虽然导致类装载的原因有很多种,在单例模式中大多数都是调用 getInstance 方法, 14 | * 但是也不能确定有其他的方式(或者其他的静态方法)导致类装载, 15 | * 这时候初始化 instance 显然没有达到 lazy loading 的效果。 16 | * 17 | * @author JourWon 18 | * @date 2019/7/13 23:49 19 | */ 20 | public class Singleton3 { 21 | 22 | private static Singleton3 instance = new Singleton3(); 23 | 24 | private Singleton3() {} 25 | 26 | public static Singleton3 getInstance() { 27 | return instance; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/flyweight/ShapeFactory.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.flyweight; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * Description:创建一个工厂,生成基于给定信息的实体类的对象。 8 | * 9 | * @author JourWon 10 | * @date 2019/7/16 11:29 11 | */ 12 | public class ShapeFactory { 13 | 14 | private static final Map circleMap = new HashMap<>(); 15 | 16 | public static Shape getCircle(String color) { 17 | Circle circle = (Circle) circleMap.get(color); 18 | if (circle == null) { 19 | circle = new Circle(color); 20 | circleMap.put(color, circle); 21 | System.out.println("Creating circle of color : " + color); 22 | } 23 | return circle; 24 | } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/servicelocator/ServiceLocator.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.servicelocator; 2 | 3 | /** 4 | * Description:创建服务定位器。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:23 8 | */ 9 | public class ServiceLocator { 10 | 11 | private static Cache cache; 12 | 13 | static { 14 | cache = new Cache(); 15 | } 16 | 17 | public static Service getService(String jndiName) { 18 | 19 | Service service = cache.getService(jndiName); 20 | 21 | if (service != null) { 22 | return service; 23 | } 24 | 25 | InitialContext context = new InitialContext(); 26 | Service service1 = (Service) context.lookup(jndiName); 27 | cache.addService(service1); 28 | return service1; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/singleton/Singleton2.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.singleton; 2 | 3 | /** 4 | * Description:懒汉式,线程安全 5 | * 是否 Lazy 初始化:是 6 | * 是否多线程安全:是 7 | * 实现难度:易 8 | * 9 | * 描述:这种方式具备很好的 lazy loading,能够在多线程中很好的工作,但是,效率很低,99% 情况下不需要同步。 10 | * 优点:第一次调用才初始化,避免内存浪费。 11 | * 缺点:必须加锁 synchronized 才能保证单例,但加锁会影响效率。 12 | * getInstance() 的性能对应用程序不是很关键(该方法使用不太频繁)。 13 | * 14 | * @author JourWon 15 | * @date 2019/7/13 23:47 16 | */ 17 | public class Singleton2 { 18 | 19 | private static Singleton2 instance; 20 | 21 | private Singleton2() {} 22 | 23 | public static synchronized Singleton2 getInstance() { 24 | if (instance == null) { 25 | instance = new Singleton2(); 26 | } 27 | 28 | return instance; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/interceptingfilter/FilterChain.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.interceptingfilter; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description:创建过滤器链。 8 | * 9 | * @author JourWon 10 | * @date 2019/7/17 18:17 11 | */ 12 | public class FilterChain { 13 | 14 | private List filters = new ArrayList(); 15 | private Target target; 16 | 17 | public void addFilter(Filter filter){ 18 | filters.add(filter); 19 | } 20 | 21 | public void execute(String request){ 22 | for (Filter filter : filters) { 23 | filter.execute(request); 24 | } 25 | target.execute(request); 26 | } 27 | 28 | public void setTarget(Target target){ 29 | this.target = target; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/filter/Person.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.filter; 2 | 3 | /** 4 | * Description:创建一个类,在该类上应用标准。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 17:28 8 | */ 9 | public class Person { 10 | 11 | private String name; 12 | 13 | private String gender; 14 | 15 | private String maritalStatus; 16 | 17 | public Person(String name, String gender, String maritalStatus) { 18 | this.name = name; 19 | this.gender = gender; 20 | this.maritalStatus = maritalStatus; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public String getGender() { 28 | return gender; 29 | } 30 | 31 | public String getMaritalStatus() { 32 | return maritalStatus; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/singleton/Singleton8.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.singleton; 2 | 3 | /** 4 | * Description:使用ThreadLocal实现单例模式(线程安全) 5 | ThreadLocal会为每一个线程提供一个独立的变量副本,从而隔离了多个线程对数据的访问冲突。 6 | 对于多线程资源共享的问题,同步机制采用了“以时间换空间”的方式, 7 | 而ThreadLocal采用了“以空间换时间”的方式。前者仅提供一份变量, 8 | 让不同的线程排队访问,而后者为每一个线程都提供了一份变量,因此可以同时访问而互不影响。 9 | * 10 | * @author JourWon 11 | * @date 2019/7/14 0:08 12 | */ 13 | public class Singleton8 { 14 | 15 | private static final ThreadLocal tlSingleton = new ThreadLocal() { 16 | @Override 17 | protected Singleton8 initialValue() { 18 | return new Singleton8(); 19 | } 20 | }; 21 | 22 | private Singleton8() {} 23 | 24 | public static Singleton8 getInstance() { 25 | return tlSingleton.get(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/decorator/DecoratorPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.decorator; 2 | 3 | /** 4 | * Description:使用 RedShapeDecorator 来装饰 Shape 对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 9:33 8 | */ 9 | public class DecoratorPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | Shape circle = new Circle(); 13 | Shape redCircle = new RedShapeDecorator(new Circle()); 14 | Shape redRectangle = new RedShapeDecorator(new Rectangle()); 15 | 16 | System.out.println("Circle with normal border"); 17 | circle.draw(); 18 | 19 | System.out.println("\nCircle of red border"); 20 | redCircle.draw(); 21 | 22 | System.out.println("\nRectangle of red border"); 23 | redRectangle.draw(); 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/factory/ShapeFactory.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.factory; 2 | 3 | /** 4 | * Description:创建一个工厂,生成基于给定信息的实体类的对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/12 15:37 8 | */ 9 | public class ShapeFactory { 10 | 11 | //使用 getShape 方法获取形状类型的对象 12 | public Shape getShape(String shapeType) { 13 | if (shapeType == null) { 14 | return null; 15 | } 16 | shapeType = shapeType.toLowerCase(); 17 | 18 | switch (shapeType) { 19 | case "circle": 20 | return new Circle(); 21 | case "rectangle": 22 | return new Rectangle(); 23 | case "square": 24 | return new Square(); 25 | default: 26 | return null; 27 | } 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/prototype/PrototypePatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.prototype; 2 | 3 | /** 4 | * Description:PrototypePatternDemo 使用 ShapeCache 类来获取存储在 Hashtable 中的形状的克隆。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 10:36 8 | */ 9 | public class PrototypePatternDemo { 10 | 11 | public static void main(String[] args) { 12 | ShapeCache.loadCache(); 13 | 14 | Shape clonedShape = (Shape) ShapeCache.getShape("1"); 15 | System.out.println("Shape : " + clonedShape.getType()); 16 | 17 | Shape clonedShape2 = (Shape) ShapeCache.getShape("2"); 18 | System.out.println("Shape : " + clonedShape2.getType()); 19 | 20 | Shape clonedShape3 = (Shape) ShapeCache.getShape("3"); 21 | System.out.println("Shape : " + clonedShape3.getType()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/servicelocator/ServiceLocatorPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.servicelocator; 2 | 3 | /** 4 | * Description:使用 ServiceLocator 来演示服务定位器设计模式。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 18:23 8 | */ 9 | public class ServiceLocatorPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | Service service = ServiceLocator.getService("Service1"); 13 | service.execute(); 14 | System.out.println(); 15 | 16 | service = ServiceLocator.getService("Service2"); 17 | service.execute(); 18 | System.out.println(); 19 | 20 | service = ServiceLocator.getService("Service1"); 21 | service.execute(); 22 | 23 | System.out.println(); 24 | service = ServiceLocator.getService("Service2"); 25 | service.execute(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/builder/BuilderPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | /** 4 | * Description:BuiderPatternDemo 使用 MealBuider 来演示建造者模式(Builder Pattern)。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 9:57 8 | */ 9 | public class BuilderPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | MealBuilder mealBuilder = new MealBuilder(); 13 | 14 | Meal vegMeal = mealBuilder.prepareVegMeal(); 15 | System.out.println("Veg Meal"); 16 | vegMeal.showItems(); 17 | System.out.println("Total Cost: " + vegMeal.getCost()); 18 | 19 | Meal nonVegMeal = mealBuilder.prepareNonVegMeal(); 20 | System.out.println("\n\nNon-Veg Meal"); 21 | nonVegMeal.showItems(); 22 | System.out.println("Total Cost: " + nonVegMeal.getCost()); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/visitor/ComputerPartDisplayVisitor.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.visitor; 2 | 3 | /** 4 | * Description:创建实现了 ComputerPartVisitor 的实体访问者。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:14 8 | */ 9 | public class ComputerPartDisplayVisitor implements ComputerPartVisitor { 10 | 11 | @Override 12 | public void visit(Computer computer) { 13 | System.out.println("Displaying Computer."); 14 | } 15 | 16 | @Override 17 | public void visit(Mouse mouse) { 18 | System.out.println("Displaying Mouse."); 19 | } 20 | 21 | @Override 22 | public void visit(Keyboard keyboard) { 23 | System.out.println("Displaying Keyboard."); 24 | } 25 | 26 | @Override 27 | public void visit(Monitor monitor) { 28 | System.out.println("Displaying Monitor."); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/singleton/Singleton4.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.singleton; 2 | 3 | /** 4 | * Description:双检锁/双重校验锁(DCL,即 double-checked locking) 5 | * JDK 版本:JDK1.5 起 6 | * 是否 Lazy 初始化:是 7 | * 是否多线程安全:是 8 | * 实现难度:较复杂 9 | * 10 | * 描述:这种方式采用双锁机制,安全且在多线程情况下能保持高性能。 11 | * getInstance() 的性能对应用程序很关键。 12 | * 13 | * @author JourWon 14 | * @date 2019/7/13 23:51 15 | */ 16 | public class Singleton4 { 17 | 18 | private static Singleton4 instance; 19 | 20 | private Singleton4() {} 21 | 22 | public static Singleton4 getInstance() { 23 | if (instance == null) { 24 | synchronized (Singleton4.class) { 25 | if (instance == null) { 26 | instance = new Singleton4(); 27 | } 28 | } 29 | } 30 | 31 | return instance; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/observer/Subject.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.observer; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description:创建 Subject 类。 8 | * 9 | * @author JourWon 10 | * @date 2019/7/17 14:13 11 | */ 12 | public class Subject { 13 | 14 | private List observers = new ArrayList<>(); 15 | 16 | private int state; 17 | 18 | public int getState() { 19 | return state; 20 | } 21 | 22 | public void setState(int state) { 23 | this.state = state; 24 | notifyAllObservers(); 25 | } 26 | 27 | public void attach(Observer observer) { 28 | observers.add(observer); 29 | } 30 | 31 | public void notifyAllObservers() { 32 | for (Observer observer : observers) { 33 | observer.update(); 34 | } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/ColorFactory.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description: 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 23:00 8 | */ 9 | public class ColorFactory extends AbstractFactory { 10 | 11 | @Override 12 | Shape getShape(String shape) { 13 | return null; 14 | } 15 | 16 | @Override 17 | Color getColor(String color) { 18 | if (color == null) { 19 | return null; 20 | } 21 | 22 | color = color.toLowerCase(); 23 | switch (color) { 24 | case "red": 25 | return new Red(); 26 | case "blue": 27 | return new Blue(); 28 | case "green": 29 | return new Green(); 30 | default: 31 | return null; 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/singleton/Singleton9.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.singleton; 2 | 3 | import java.util.concurrent.atomic.AtomicReference; 4 | 5 | /** 6 | * Description:使用CAS锁实现(线程安全) 7 | * 8 | * @author JourWon 9 | * @date 2019/7/14 0:11 10 | */ 11 | public class Singleton9 { 12 | 13 | private static final AtomicReference INSTANCE = new AtomicReference(); 14 | 15 | private Singleton9() { 16 | } 17 | 18 | public static Singleton9 getInstance() { 19 | for (; ; ) { 20 | Singleton9 current = INSTANCE.get(); 21 | 22 | if (current != null) { 23 | return current; 24 | } 25 | 26 | current = new Singleton9(); 27 | 28 | if (INSTANCE.compareAndSet(null, current)) { 29 | return current; 30 | } 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/prototype/Shape.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.prototype; 2 | 3 | /** 4 | * Description:创建一个实现了 Clonable 接口的抽象类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 10:16 8 | */ 9 | public abstract class Shape implements Cloneable { 10 | 11 | private String id; 12 | 13 | protected String type; 14 | 15 | abstract void draw(); 16 | 17 | public String getId() { 18 | return id; 19 | } 20 | 21 | public void setId(String id) { 22 | this.id = id; 23 | } 24 | 25 | public String getType() { 26 | return type; 27 | } 28 | 29 | @Override 30 | protected Object clone() { 31 | Object clone = null; 32 | try { 33 | clone = super.clone(); 34 | } catch (CloneNotSupportedException e) { 35 | e.printStackTrace(); 36 | } 37 | return clone; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/factory/FactoryPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.factory; 2 | 3 | /** 4 | * Description:使用该工厂,通过传递类型信息来获取实体类的对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/12 16:45 8 | */ 9 | public class FactoryPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | ShapeFactory shapeFactory = new ShapeFactory(); 13 | 14 | //获取 Circle 的对象,并调用它的 draw 方法 15 | Shape shape1 = shapeFactory.getShape("CIRCLE"); 16 | //调用 Circle 的 draw 方法 17 | shape1.draw(); 18 | 19 | //获取 Rectangle 的对象,并调用它的 draw 方法 20 | Shape shape2 = shapeFactory.getShape("RECTANGLE"); 21 | //调用 Rectangle 的 draw 方法 22 | shape2.draw(); 23 | 24 | //获取 Square 的对象,并调用它的 draw 方法 25 | Shape shape3 = shapeFactory.getShape("SQUARE"); 26 | //调用 Square 的 draw 方法 27 | shape3.draw(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/frontcontroller/FrontController.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.frontcontroller; 2 | 3 | /** 4 | * Description:创建前端控制器 FrontController。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 17:58 8 | */ 9 | public class FrontController { 10 | 11 | private Dispatcher dispatcher; 12 | 13 | public FrontController(){ 14 | dispatcher = new Dispatcher(); 15 | } 16 | 17 | private boolean isAuthenticUser(){ 18 | System.out.println("User is authenticated successfully."); 19 | return true; 20 | } 21 | 22 | private void trackRequest(String request){ 23 | System.out.println("Page requested: " + request); 24 | } 25 | 26 | public void dispatchRequest(String request){ 27 | //记录每一个请求 28 | trackRequest(request); 29 | //对用户进行身份验证 30 | if(isAuthenticUser()){ 31 | dispatcher.dispatch(request); 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/nullobject/NullPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.nullobject; 2 | 3 | /** 4 | * Description:使用 CustomerFactory,基于客户传递的名字,来获取 RealCustomer 或 NullCustomer 对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 15:16 8 | */ 9 | public class NullPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | AbstractCustomer customer1 = CustomerFactory.getCustomer("Rob"); 13 | AbstractCustomer customer2 = CustomerFactory.getCustomer("Bob"); 14 | AbstractCustomer customer3 = CustomerFactory.getCustomer("Julie"); 15 | AbstractCustomer customer4 = CustomerFactory.getCustomer("Laura"); 16 | 17 | System.out.println("Customers"); 18 | System.out.println(customer1.getName()); 19 | System.out.println(customer2.getName()); 20 | System.out.println(customer3.getName()); 21 | System.out.println(customer4.getName()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/ShapeFactory.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description:创建扩展了 CustomerFactory 的工厂类,基于给定的信息生成实体类的对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 22:58 8 | */ 9 | public class ShapeFactory extends AbstractFactory { 10 | 11 | @Override 12 | Shape getShape(String shape) { 13 | if (shape == null) { 14 | return null; 15 | } 16 | 17 | shape = shape.toLowerCase(); 18 | 19 | switch (shape) { 20 | case "circle": 21 | return new Circle(); 22 | case "rectangle": 23 | return new Rectangle(); 24 | case "square": 25 | return new Square(); 26 | default: 27 | return null; 28 | } 29 | 30 | } 31 | 32 | @Override 33 | Color getColor(String color) { 34 | return null; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/chain/AbstractLogger.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.chain; 2 | 3 | /** 4 | * Description:创建抽象的记录器类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 14:17 8 | */ 9 | public abstract class AbstractLogger { 10 | 11 | public static final int INFO = 1; 12 | public static final int DEBUG = 2; 13 | public static final int ERROR = 3; 14 | 15 | protected int level; 16 | 17 | /** 18 | * 责任链中的下一个元素 19 | */ 20 | protected AbstractLogger nextLogger; 21 | 22 | public void setNextLogger(AbstractLogger nextLogger) { 23 | this.nextLogger = nextLogger; 24 | } 25 | 26 | public void logMessage(int level, String message) { 27 | if (this.level <= level) { 28 | write(message); 29 | } 30 | if (nextLogger != null) { 31 | nextLogger.logMessage(level, message); 32 | } 33 | } 34 | 35 | abstract protected void write(String message); 36 | 37 | } -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/builder/Meal.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.builder; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description:创建一个 Meal 类,带有上面定义的 Item 对象。 8 | * 9 | * @author JourWon 10 | * @date 2019/7/15 9:47 11 | */ 12 | public class Meal { 13 | 14 | private List items = new ArrayList<>(); 15 | 16 | public void addItem(Item item) { 17 | items.add(item); 18 | } 19 | 20 | public float getCost() { 21 | float cost = 0.0f; 22 | 23 | for (Item item : items) { 24 | cost += item.price(); 25 | } 26 | 27 | return cost; 28 | } 29 | 30 | public void showItems() { 31 | for (Item item : items) { 32 | System.out.print("Item : " + item.name()); 33 | System.out.print(", Packing : " + item.packing().pack()); 34 | System.out.println(", Price : " + item.price()); 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/adapter/MediaAdapter.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.adapter; 2 | 3 | /** 4 | * Description:创建实现了 MediaPlayer 接口的适配器类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 11:04 8 | */ 9 | public class MediaAdapter implements MediaPlayer { 10 | 11 | AdvancedMediaPlayer advancedMusicPlayer; 12 | 13 | public MediaAdapter(String audioType) { 14 | if (audioType.equalsIgnoreCase("vlc")) { 15 | advancedMusicPlayer = new VlcPlayer(); 16 | } else if (audioType.equalsIgnoreCase("mp4")) { 17 | advancedMusicPlayer = new Mp4Player(); 18 | } 19 | } 20 | 21 | @Override 22 | public void play(String audioType, String fileName) { 23 | if (audioType.equalsIgnoreCase("vlc")) { 24 | advancedMusicPlayer.playVlc(fileName); 25 | } else if (audioType.equalsIgnoreCase("mp4")) { 26 | advancedMusicPlayer.playMp4(fileName); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/mvc/StudentController.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.mvc; 2 | 3 | /** 4 | * Description:创建控制器。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:35 8 | */ 9 | public class StudentController { 10 | 11 | private Student model; 12 | private StudentView view; 13 | 14 | public StudentController(Student model, StudentView view) { 15 | this.model = model; 16 | this.view = view; 17 | } 18 | 19 | public void setStudentName(String name) { 20 | model.setName(name); 21 | } 22 | 23 | public String getStudentName() { 24 | return model.getName(); 25 | } 26 | 27 | public void setStudentRollNo(String rollNo) { 28 | model.setRollNo(rollNo); 29 | } 30 | 31 | public String getStudentRollNo() { 32 | return model.getRollNo(); 33 | } 34 | 35 | public void updateView() { 36 | view.printStudentDetails(model.getName(), model.getRollNo()); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/filter/OrCriteria.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.filter; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Description: 7 | * 8 | * @author JourWon 9 | * @date 2019/7/15 17:53 10 | */ 11 | public class OrCriteria implements Criteria { 12 | 13 | private Criteria criteria; 14 | 15 | private Criteria otherCriteria; 16 | 17 | public OrCriteria(Criteria criteria, Criteria otherCriteria) { 18 | this.criteria = criteria; 19 | this.otherCriteria = otherCriteria; 20 | } 21 | 22 | @Override 23 | public List meetCriteria(List persons) { 24 | List first = criteria.meetCriteria(persons); 25 | List other = otherCriteria.meetCriteria(persons); 26 | 27 | for (Person person : other) { 28 | if (!first.contains(person)) { 29 | first.add(person); 30 | } 31 | } 32 | 33 | return first; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/adapter/AudioPlayer.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.adapter; 2 | 3 | /** 4 | * Description:创建实现了 MediaPlayer 接口的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/15 11:09 8 | */ 9 | public class AudioPlayer implements MediaPlayer { 10 | 11 | MediaAdapter mediaAdapter; 12 | 13 | @Override 14 | public void play(String audioType, String fileName) { 15 | // 播放 mp3 音乐文件的内置支持 16 | if (audioType.equalsIgnoreCase("mp3")) { 17 | System.out.println("Playing mp3 file. Name: " + fileName); 18 | } else if (audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")) { 19 | //mediaAdapter 提供了播放其他文件格式的支持 20 | mediaAdapter = new MediaAdapter(audioType); 21 | mediaAdapter.play(audioType, fileName); 22 | } else { 23 | System.out.println("Invalid media. " + 24 | audioType + " format not supported"); 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/flyweight/Circle.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.flyweight; 2 | 3 | /** 4 | * Description:创建实现接口的实体类。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 11:22 8 | */ 9 | public class Circle implements Shape { 10 | 11 | private String color; 12 | private int x; 13 | private int y; 14 | private int radius; 15 | 16 | public Circle(String color) { 17 | this.color = color; 18 | } 19 | 20 | public void setColor(String color) { 21 | this.color = color; 22 | } 23 | 24 | public void setX(int x) { 25 | this.x = x; 26 | } 27 | 28 | public void setY(int y) { 29 | this.y = y; 30 | } 31 | 32 | public void setRadius(int radius) { 33 | this.radius = radius; 34 | } 35 | 36 | @Override 37 | public void draw() { 38 | System.out.println("Circle: Draw() [Color : " + color 39 | + ", x : " + x + ", y : " + y + ", radius : " + radius); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/mvc/MvcPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.mvc; 2 | 3 | /** 4 | * Description:使用 StudentController 方法来演示 MVC 设计模式的用法。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 16:36 8 | */ 9 | public class MvcPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | 13 | //从数据可获取学生记录 14 | Student model = retriveStudentFromDatabase(); 15 | 16 | //创建一个视图:把学生详细信息输出到控制台 17 | StudentView view = new StudentView(); 18 | 19 | StudentController controller = new StudentController(model, view); 20 | 21 | controller.updateView(); 22 | System.out.println(); 23 | 24 | //更新模型数据 25 | controller.setStudentName("John"); 26 | 27 | controller.updateView(); 28 | } 29 | 30 | private static Student retriveStudentFromDatabase() { 31 | Student student = new Student(); 32 | student.setName("Robert"); 33 | student.setRollNo("10"); 34 | return student; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/iterator/NameRepository.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.iterator; 2 | 3 | /** 4 | * Description:创建实现了 Container 接口的实体类。该类有实现了 Iterator 接口的内部类 NameIterator。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 13:58 8 | */ 9 | public class NameRepository implements Container { 10 | 11 | public String names[] = {"Robert", "John", "Julie", "Lora"}; 12 | 13 | @Override 14 | public Iterator getIterator() { 15 | return new NameIterator(); 16 | } 17 | 18 | private class NameIterator implements Iterator { 19 | 20 | int index; 21 | 22 | @Override 23 | public boolean hasNext() { 24 | if (index < names.length) { 25 | return true; 26 | } 27 | return false; 28 | } 29 | 30 | @Override 31 | public Object next() { 32 | if (this.hasNext()) { 33 | return names[index++]; 34 | } 35 | return null; 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/singleton/Singleton7.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.singleton; 2 | 3 | /** 4 | * Description:volatile关键字 5 | * 对于Double-Check这种可能出现的问题(当然这种概率已经非常小了,但毕竟还是有的嘛~), 6 | * 解决方案是:只需要给instance的声明加上volatile关键字即可 7 | * 8 | * volatile关键字的一个作用是禁止指令重排,把instance声明为volatile之后, 9 | * 对它的写操作就会有一个内存屏障(什么是内存屏障?),这样,在它的赋值完成之前,就不用会调用读操作。 10 | * 11 | * 注意:volatile阻止的不是singleton = newSingleton()这句话内部[1-2-3]的指令重排, 12 | * 而是保证了在一个写操作([1-2-3])完成之前,不会调用读操作(if (instance == null))。 13 | * 14 | * @author JourWon 15 | * @date 2019/7/14 0:24 16 | */ 17 | public class Singleton7 { 18 | 19 | private static volatile Singleton7 instance = null; 20 | 21 | private Singleton7() {} 22 | 23 | public static Singleton7 getInstance() { 24 | if (instance == null) { 25 | synchronized (Singleton7.class) { 26 | if (instance == null) { 27 | instance = new Singleton7(); 28 | } 29 | } 30 | } 31 | 32 | return instance; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/transferobject/TransferObjectPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.transferobject; 2 | 3 | /** 4 | * Description:使用 StudentBO 来演示传输对象设计模式。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/19 13:40 8 | */ 9 | public class TransferObjectPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | StudentBO studentBusinessObject = new StudentBO(); 13 | 14 | //输出所有的学生 15 | for (StudentVO student : studentBusinessObject.getAllStudents()) { 16 | System.out.println("Student: [RollNo : " 17 | +student.getRollNo()+", Name : "+student.getName()+" ]"); 18 | } 19 | 20 | //更新学生 21 | StudentVO student =studentBusinessObject.getAllStudents().get(0); 22 | student.setName("Michael"); 23 | studentBusinessObject.updateStudent(student); 24 | 25 | //获取学生 26 | studentBusinessObject.getStudent(0); 27 | System.out.println("Student: [RollNo : " 28 | +student.getRollNo()+", Name : "+student.getName()+" ]"); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/memento/MementoPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.memento; 2 | 3 | /** 4 | * Description:使用 CareTaker 和 Originator 对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/19 10:08 8 | */ 9 | public class MementoPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | Originator originator = new Originator(); 13 | CareTaker careTaker = new CareTaker(); 14 | originator.setState("State #1"); 15 | originator.setState("State #2"); 16 | careTaker.add(originator.saveStateToMemento()); 17 | originator.setState("State #3"); 18 | careTaker.add(originator.saveStateToMemento()); 19 | originator.setState("State #4"); 20 | 21 | System.out.println("Current State: " + originator.getState()); 22 | originator.getStateFromMemento(careTaker.get(0)); 23 | System.out.println("First saved State: " + originator.getState()); 24 | originator.getStateFromMemento(careTaker.get(1)); 25 | System.out.println("Second saved State: " + originator.getState()); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/prototype/ShapeCache.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.prototype; 2 | 3 | import java.util.Hashtable; 4 | 5 | /** 6 | * Description:创建一个类,从数据库获取实体类,并把它们存储在一个 Hashtable 中。 7 | * 8 | * @author JourWon 9 | * @date 2019/7/15 10:27 10 | */ 11 | public class ShapeCache { 12 | 13 | private static Hashtable shapeMap = new Hashtable<>(); 14 | 15 | public static Shape getShape(String shapeId) { 16 | Shape cachedShape = shapeMap.get(shapeId); 17 | return (Shape) cachedShape.clone(); 18 | } 19 | 20 | // 对每种形状都运行数据库查询,并创建该形状 21 | // shapeMap.put(shapeKey, shape); 22 | // 例如,我们要添加三种形状 23 | public static void loadCache() { 24 | Circle circle = new Circle(); 25 | circle.setId("1"); 26 | shapeMap.put(circle.getId(), circle); 27 | 28 | Square square = new Square(); 29 | square.setId("2"); 30 | shapeMap.put(square.getId(), square); 31 | 32 | Rectangle rectangle = new Rectangle(); 33 | rectangle.setId("3"); 34 | shapeMap.put(rectangle.getId(), rectangle); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/dataaccessobject/DataAccessObjectPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.dataaccessobject; 2 | 3 | /** 4 | * Description:使用 StudentDao 来演示数据访问对象模式的用法。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 17:52 8 | */ 9 | public class DataAccessObjectPatternDemo { 10 | 11 | public static void main(String[] args) { 12 | StudentDao studentDao = new StudentDaoImpl(); 13 | 14 | //输出所有的学生 15 | for (Student student : studentDao.getAllStudents()) { 16 | System.out.println("Student: [RollNo : " 17 | + student.getRollNo() + ", Name : " + student.getName() + " ]"); 18 | } 19 | System.out.println(); 20 | 21 | //更新学生 22 | Student student = studentDao.getAllStudents().get(0); 23 | student.setName("Michael"); 24 | studentDao.updateStudent(student); 25 | System.out.println(); 26 | 27 | //获取学生 28 | studentDao.getStudent(0); 29 | System.out.println("Student: [RollNo : " 30 | + student.getRollNo() + ", Name : " + student.getName() + " ]"); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/flyweight/FlyweightPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.flyweight; 2 | 3 | /** 4 | * Description:使用该工厂,通过传递颜色信息来获取实体类的对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 11:33 8 | */ 9 | public class FlyweightPatternDemo { 10 | 11 | private static final String[] colors = 12 | {"Red", "Green", "Blue", "White", "Black"}; 13 | 14 | public static void main(String[] args) { 15 | 16 | for (int i = 0; i < 20; ++i) { 17 | Circle circle = (Circle) ShapeFactory.getCircle(getRandomColor()); 18 | circle.setX(getRandomX()); 19 | circle.setY(getRandomY()); 20 | circle.setRadius(100); 21 | circle.draw(); 22 | System.out.println(); 23 | } 24 | } 25 | 26 | private static String getRandomColor() { 27 | return colors[(int) (Math.random() * colors.length)]; 28 | } 29 | 30 | private static int getRandomX() { 31 | return (int) (Math.random() * 100); 32 | } 33 | 34 | private static int getRandomY() { 35 | return (int) (Math.random() * 100); 36 | } 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/servicelocator/Cache.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.servicelocator; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description:创建缓存 Cache。 8 | * 9 | * @author JourWon 10 | * @date 2019/7/17 18:22 11 | */ 12 | public class Cache { 13 | 14 | private List services; 15 | 16 | public Cache() { 17 | services = new ArrayList(); 18 | } 19 | 20 | public Service getService(String serviceName) { 21 | for (Service service : services) { 22 | if (service.getName().equalsIgnoreCase(serviceName)) { 23 | System.out.println("Returning cached " + serviceName + " object"); 24 | return service; 25 | } 26 | } 27 | return null; 28 | } 29 | 30 | public void addService(Service newService) { 31 | boolean exists = false; 32 | for (Service service : services) { 33 | if (service.getName().equalsIgnoreCase(newService.getName())) { 34 | exists = true; 35 | } 36 | } 37 | if (!exists) { 38 | services.add(newService); 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/singleton/Singleton5.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.singleton; 2 | 3 | /** 4 | * Description:登记式/静态内部类 5 | * 是否 Lazy 初始化:是 6 | * 是否多线程安全:是 7 | * 实现难度:一般 8 | * 9 | * 描述:这种方式能达到双检锁方式一样的功效,但实现更简单。对静态域使用延迟初始化,应使用这种方式而不是双检锁方式。 10 | * 这种方式只适用于静态域的情况,双检锁方式可在实例域需要延迟初始化时使用。 11 | * 这种方式同样利用了 classloder 机制来保证初始化 instance 时只有一个线程, 12 | * 它跟第 3 种方式不同的是:第 3 种方式只要 Singleton 类被装载了, 13 | * 那么 instance 就会被实例化(没有达到 lazy loading 效果), 14 | * 而这种方式是 Singleton 类被装载了,instance 不一定被初始化。 15 | * 因为 SingletonHolder 类没有被主动使用,只有显示通过调用 getInstance 方法时, 16 | * 才会显示装载 SingletonHolder 类,从而实例化 instance。 17 | * 想象一下,如果实例化 instance 很消耗资源,所以想让它延迟加载, 18 | * 另外一方面,又不希望在 Singleton 类加载时就实例化, 19 | * 因为不能确保 Singleton 类还可能在其他的地方被主动使用从而被加载, 20 | * 那么这个时候实例化 instance 显然是不合适的。这个时候,这种方式相比第 3 种方式就显得很合理。 21 | * 22 | * @author JourWon 23 | * @date 2019/7/13 23:54 24 | */ 25 | public class Singleton5 { 26 | 27 | private static class SingletonHolder { 28 | private static final Singleton5 INSTANCE = new Singleton5(); 29 | } 30 | 31 | private Singleton5() {} 32 | 33 | public static Singleton5 getInstance() { 34 | return SingletonHolder.INSTANCE; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/composite/Employee.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.composite; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description:创建 Employee 类,该类带有 Employee 对象的列表。 8 | * 9 | * @author JourWon 10 | * @date 2019/7/16 9:13 11 | */ 12 | public class Employee { 13 | 14 | private String name; 15 | private String dept; 16 | private int salary; 17 | private List subordinates; 18 | 19 | public Employee(String name, String dept, int salary) { 20 | this.name = name; 21 | this.dept = dept; 22 | this.salary = salary; 23 | this.subordinates = new ArrayList(); 24 | } 25 | 26 | public void add(Employee e) { 27 | subordinates.add(e); 28 | } 29 | 30 | public void remove(Employee e) { 31 | subordinates.remove(e); 32 | } 33 | 34 | public List getSubordinates() { 35 | return subordinates; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return ("Employee :[ Name : " + name 41 | + ", dept : " + dept + ", salary :" 42 | + salary + " ]"); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/interpreter/InterpreterPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.interpreter; 2 | 3 | /** 4 | * Description:InterpreterPatternDemo 使用 Expression 类来创建规则,并解析它们。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/17 11:04 8 | */ 9 | public class InterpreterPatternDemo { 10 | 11 | //规则:Robert 和 John 是男性 12 | public static Expression getMaleExpression(){ 13 | Expression robert = new TerminalExpression("Robert"); 14 | Expression john = new TerminalExpression("John"); 15 | return new OrExpression(robert, john); 16 | } 17 | 18 | //规则:Julie 是一个已婚的女性 19 | public static Expression getMarriedWomanExpression(){ 20 | Expression julie = new TerminalExpression("Julie"); 21 | Expression married = new TerminalExpression("Married"); 22 | return new AndExpression(julie, married); 23 | } 24 | 25 | public static void main(String[] args) { 26 | Expression isMale = getMaleExpression(); 27 | Expression isMarriedWoman = getMarriedWomanExpression(); 28 | 29 | System.out.println("John is male? " + isMale.interpret("John")); 30 | System.out.println("Julie is a married women? " 31 | + isMarriedWoman.interpret("Married Julie")); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/behavioral/chain/ChainPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.behavioral.chain; 2 | 3 | /** 4 | * Description:创建不同类型的记录器。赋予它们不同的错误级别,并在每个记录器中设置下一个记录器。 5 | * 每个记录器中的下一个记录器代表的是链的一部分。 6 | * 7 | * @author JourWon 8 | * @date 2019/7/16 14:32 9 | */ 10 | public class ChainPatternDemo { 11 | 12 | private static AbstractLogger getChainOfLoggers(){ 13 | 14 | AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR); 15 | AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG); 16 | AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO); 17 | 18 | errorLogger.setNextLogger(fileLogger); 19 | fileLogger.setNextLogger(consoleLogger); 20 | 21 | return errorLogger; 22 | } 23 | 24 | public static void main(String[] args) { 25 | AbstractLogger loggerChain = getChainOfLoggers(); 26 | 27 | loggerChain.logMessage(AbstractLogger.INFO, 28 | "This is an information."); 29 | System.out.println(); 30 | 31 | loggerChain.logMessage(AbstractLogger.DEBUG, 32 | "This is an debug level information."); 33 | System.out.println(); 34 | 35 | loggerChain.logMessage(AbstractLogger.ERROR, 36 | "This is an error information."); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/transferobject/StudentBO.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.transferobject; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description:创建业务对象。 8 | * 9 | * @author JourWon 10 | * @date 2019/7/19 13:39 11 | */ 12 | public class StudentBO { 13 | 14 | //列表是当作一个数据库 15 | List students; 16 | 17 | public StudentBO() { 18 | students = new ArrayList(); 19 | StudentVO student1 = new StudentVO("Robert", 0); 20 | StudentVO student2 = new StudentVO("John", 1); 21 | students.add(student1); 22 | students.add(student2); 23 | } 24 | 25 | public void deleteStudent(StudentVO student) { 26 | students.remove(student.getRollNo()); 27 | System.out.println("Student: Roll No " 28 | + student.getRollNo() + ", deleted from database"); 29 | } 30 | 31 | //从数据库中检索学生名单 32 | public List getAllStudents() { 33 | return students; 34 | } 35 | 36 | public StudentVO getStudent(int rollNo) { 37 | return students.get(rollNo); 38 | } 39 | 40 | public void updateStudent(StudentVO student) { 41 | students.get(student.getRollNo()).setName(student.getName()); 42 | System.out.println("Student: Roll No " 43 | + student.getRollNo() + ", updated in the database"); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/composite/CompositePatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.composite; 2 | 3 | /** 4 | * Description:使用 Employee 类来创建和打印员工的层次结构。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/16 9:19 8 | */ 9 | public class CompositePatternDemo { 10 | 11 | public static void main(String[] args) { 12 | Employee CEO = new Employee("John", "CEO", 30000); 13 | 14 | Employee headSales = new Employee("Robert", "Head Sales", 20000); 15 | 16 | Employee headMarketing = new Employee("Michel", "Head Marketing", 20000); 17 | 18 | Employee clerk1 = new Employee("Laura", "Marketing", 10000); 19 | Employee clerk2 = new Employee("Bob", "Marketing", 10000); 20 | 21 | Employee salesExecutive1 = new Employee("Richard", "Sales", 10000); 22 | Employee salesExecutive2 = new Employee("Rob", "Sales", 10000); 23 | 24 | CEO.add(headSales); 25 | CEO.add(headMarketing); 26 | 27 | headSales.add(salesExecutive1); 28 | headSales.add(salesExecutive2); 29 | 30 | headMarketing.add(clerk1); 31 | headMarketing.add(clerk2); 32 | 33 | //打印该组织的所有员工 34 | System.out.println(CEO); 35 | for (Employee headEmployee : CEO.getSubordinates()) { 36 | System.out.println(headEmployee); 37 | for (Employee employee : headEmployee.getSubordinates()) { 38 | System.out.println(employee); 39 | } 40 | } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/creational/abstractfactory/AbstractFactoryDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.creational.abstractfactory; 2 | 3 | /** 4 | * Description:使用 FactoryProducer 来获取 CustomerFactory,通过传递类型信息来获取实体类的对象。 5 | * 6 | * @author JourWon 7 | * @date 2019/7/13 23:08 8 | */ 9 | public class AbstractFactoryDemo { 10 | 11 | public static void main(String[] args) { 12 | //获取形状工厂 13 | AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE"); 14 | 15 | //获取形状为 Circle 的对象 16 | Shape shape1 = shapeFactory.getShape("CIRCLE"); 17 | //调用 Circle 的 draw 方法 18 | shape1.draw(); 19 | 20 | //获取形状为 Rectangle 的对象 21 | Shape shape2 = shapeFactory.getShape("RECTANGLE"); 22 | //调用 Rectangle 的 draw 方法 23 | shape2.draw(); 24 | 25 | //获取形状为 Square 的对象 26 | Shape shape3 = shapeFactory.getShape("SQUARE"); 27 | shape3.draw(); 28 | 29 | //获取颜色工厂 30 | AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR"); 31 | 32 | //获取颜色为 Red 的对象 33 | Color color1 = colorFactory.getColor("RED"); 34 | //调用 Red 的 fill 方法 35 | color1.fill(); 36 | 37 | //获取颜色为 Green 的对象 38 | Color color2 = colorFactory.getColor("Green"); 39 | //调用 Green 的 fill 方法 40 | color2.fill(); 41 | 42 | //获取颜色为 Blue 的对象 43 | Color color3 = colorFactory.getColor("BLUE"); 44 | //调用 Blue 的 fill 方法 45 | color3.fill(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/j2ee/dataaccessobject/StudentDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.j2ee.dataaccessobject; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description:创建实现了 StudentDao 接口的实体类。 8 | * 9 | * @author JourWon 10 | * @date 2019/7/17 17:51 11 | */ 12 | public class StudentDaoImpl implements StudentDao { 13 | 14 | //列表是当作一个数据库 15 | List students; 16 | 17 | public StudentDaoImpl() { 18 | students = new ArrayList(); 19 | Student student1 = new Student("Robert", 0); 20 | Student student2 = new Student("John", 1); 21 | students.add(student1); 22 | students.add(student2); 23 | } 24 | 25 | @Override 26 | public void deleteStudent(Student student) { 27 | students.remove(student.getRollNo()); 28 | System.out.println("Student: Roll No " + student.getRollNo() 29 | + ", deleted from database"); 30 | } 31 | 32 | //从数据库中检索学生名单 33 | @Override 34 | public List getAllStudents() { 35 | return students; 36 | } 37 | 38 | @Override 39 | public Student getStudent(int rollNo) { 40 | return students.get(rollNo); 41 | } 42 | 43 | @Override 44 | public void updateStudent(Student student) { 45 | students.get(student.getRollNo()).setName(student.getName()); 46 | System.out.println("Student: Roll No " + student.getRollNo() 47 | + ", updated in the database"); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/jourwon/designpattern/structural/filter/CriteriaPatternDemo.java: -------------------------------------------------------------------------------- 1 | package com.jourwon.designpattern.structural.filter; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Description:使用不同的标准(Criteria)和它们的结合来过滤 Person 对象的列表。 8 | * 9 | * @author JourWon 10 | * @date 2019/7/15 18:26 11 | */ 12 | public class CriteriaPatternDemo { 13 | 14 | public static void main(String[] args) { 15 | List persons = new ArrayList(); 16 | 17 | persons.add(new Person("Robert","Male", "Single")); 18 | persons.add(new Person("John","Male", "Married")); 19 | persons.add(new Person("Laura","Female", "Married")); 20 | persons.add(new Person("Diana","Female", "Single")); 21 | persons.add(new Person("Mike","Male", "Single")); 22 | persons.add(new Person("Bobby","Male", "Single")); 23 | 24 | Criteria male = new CriteriaMale(); 25 | Criteria female = new CriteriaFemale(); 26 | Criteria single = new CriteriaSingle(); 27 | Criteria singleMale = new AndCriteria(single, male); 28 | Criteria singleOrFemale = new OrCriteria(single, female); 29 | 30 | System.out.println("Males: "); 31 | printPersons(male.meetCriteria(persons)); 32 | 33 | System.out.println("\nFemales: "); 34 | printPersons(female.meetCriteria(persons)); 35 | 36 | System.out.println("\nSingle Males: "); 37 | printPersons(singleMale.meetCriteria(persons)); 38 | 39 | System.out.println("\nSingle Or Females: "); 40 | printPersons(singleOrFemale.meetCriteria(persons)); 41 | } 42 | 43 | public static void printPersons(List persons){ 44 | for (Person person : persons) { 45 | System.out.println("Person : [ Name : " + person.getName() 46 | +", Gender : " + person.getGender() 47 | +", Marital Status : " + person.getMaritalStatus() 48 | +" ]"); 49 | } 50 | } 51 | 52 | } 53 | --------------------------------------------------------------------------------