├── .gitignore ├── 001-OCP ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── Food.java │ ├── IGoods.java │ └── Test.java │ ├── v2 │ ├── Food.java │ ├── IGoods.java │ └── Test.java │ └── v3 │ ├── Food.java │ ├── FoodDiscount.java │ ├── IGoods.java │ └── Test.java ├── 002-DIP ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── Person.java │ └── Test.java │ ├── v2 │ ├── Chocolate.java │ ├── Coke.java │ ├── Crisps.java │ ├── IGoods.java │ ├── Person.java │ └── Test.java │ ├── v3 │ ├── Chocolate.java │ ├── Coke.java │ ├── Crisps.java │ ├── IGoods.java │ ├── Person.java │ └── Test.java │ └── v4 │ ├── Chocolate.java │ ├── Coke.java │ ├── Crisps.java │ ├── IGoods.java │ ├── Person.java │ └── Test.java ├── 003-SRP ├── 003-SRP.iml ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── Drink.java │ └── Test.java │ ├── v2 │ ├── Drink.java │ └── Test.java │ ├── v3 │ ├── DairyDrink.java │ ├── SodaDrink.java │ └── Test.java │ ├── v4 │ ├── GoodsImpl.java │ ├── IGoods.java │ ├── IGoodsInfo.java │ └── IGoodsManager.java │ └── v5 │ ├── Method.java │ └── OldMethod.java ├── 004-ISP ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── Bird.java │ ├── Dog.java │ └── IAnimal.java │ └── v2 │ ├── Bird.java │ ├── Dog.java │ ├── IEat.java │ ├── IFly.java │ └── IMakeSound.java ├── 005-LoD ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── Boss.java │ ├── Goods.java │ ├── Staff.java │ └── Test.java │ └── v2 │ ├── Boss.java │ ├── Goods.java │ ├── Staff.java │ └── Test.java ├── 006-LSP ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── Food.java │ ├── FoodDiscount.java │ ├── IGoods.java │ └── Test.java │ ├── v2 │ ├── Child.java │ ├── Father.java │ └── Test.java │ ├── v3 │ ├── Child.java │ ├── Father.java │ └── Test.java │ └── v4 │ ├── Child.java │ ├── Father.java │ └── Test.java ├── 007-CRP ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── DBConnection.java │ ├── Test.java │ └── UserDao.java │ └── v2 │ ├── DBConnection.java │ ├── MySQLConnection.java │ ├── OracleConnection.java │ ├── Test.java │ └── UserDao.java ├── 101-Simple-Factory ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── IPhone.java │ ├── MiPhone.java │ ├── Phone.java │ ├── PhoneFactory.java │ └── Test.java │ └── v2 │ ├── IPhone.java │ ├── MiPhone.java │ ├── Phone.java │ ├── PhoneFactory.java │ └── Test.java ├── 102-Factory-Method ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── IPhone.java │ ├── IPhoneFactory.java │ ├── MiPhone.java │ ├── MiPhoneFactory.java │ ├── Phone.java │ ├── PhoneFactory.java │ └── Test.java ├── 103-Abstract-Factory ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── Computer.java │ ├── ElectronicProductFactory.java │ ├── IComputer.java │ ├── IElectronicProductFactory.java │ ├── IPhone.java │ ├── MiComputer.java │ ├── MiElectronicProductFactory.java │ ├── MiPhone.java │ ├── Phone.java │ └── Test.java ├── 104-Builder ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── Computer.java │ ├── ComputerActualBuilder.java │ ├── ComputerBuilder.java │ ├── Enterprise.java │ └── Test.java │ └── v2 │ ├── Computer.java │ └── Test.java ├── 105-Singleton ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ └── LazySingleton.java │ ├── v10 │ └── EnumSingleton.java │ ├── v11 │ └── ContainerSingleton.java │ ├── v12 │ └── ThreadLocalSingleton.java │ ├── v2 │ └── LazySingleton.java │ ├── v3 │ └── LazySingleton.java │ ├── v4 │ └── LazySingleton.java │ ├── v5 │ └── LazySingleton.java │ ├── v6 │ └── StaticInnerClassSingleton.java │ ├── v7 │ └── HungrySingleton.java │ ├── v8 │ └── HungrySingleton.java │ └── v9 │ └── EnumSingleton.java ├── 106-Prototype ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── Resume.java │ └── Test.java │ ├── v2 │ ├── Resume.java │ └── Test.java │ └── v3 │ ├── Resume.java │ └── Test.java ├── 107-Facade ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── OutpatientService.java │ ├── Patient.java │ ├── RegisterService.java │ ├── SettleAccountService.java │ ├── TakeMedicineService.java │ └── Test.java │ └── v2 │ ├── OutpatientService.java │ ├── Patient.java │ ├── Receptionist.java │ ├── RegisterService.java │ ├── SettleAccountService.java │ ├── TakeMedicineService.java │ └── Test.java ├── 108-Decorator ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── AbstractDecorator.java │ ├── AbstractMilkyTea.java │ ├── MilkyTea.java │ ├── PearlDecorator.java │ ├── RedBeanDecorator.java │ ├── SugarDecorator.java │ └── Test.java ├── 109-Adapter ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── SDInterface.java │ ├── Test.java │ ├── TypeCDockStation.java │ └── TypeCInterface.java │ └── v2 │ ├── SDInterface.java │ ├── Test.java │ ├── TypeCDockStation.java │ └── TypeCInterface.java ├── 110-Flyweight ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── Clothes.java │ ├── ClothesFactory.java │ ├── Shirt.java │ └── Test.java ├── 111-Composite ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── MenuCatalog.java │ ├── MenuComponent.java │ ├── MenuItem.java │ └── Test.java │ └── v2 │ ├── MenuCatalog.java │ ├── MenuComponent.java │ ├── MenuItem.java │ └── Test.java ├── 112-Bridge ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── BallPen.java │ ├── CircleWithBallPen.java │ ├── CircleWithPencil.java │ ├── Pen.java │ ├── Pencil.java │ ├── SquareWithBallPen.java │ ├── SquareWithPencil.java │ └── Test.java │ └── v2 │ ├── BallPen.java │ ├── Circle.java │ ├── Pen.java │ ├── Pencil.java │ ├── Shape.java │ ├── Square.java │ └── Test.java ├── 113-Proxy ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── Station.java │ ├── Test.java │ └── Website12306.java │ ├── v2 │ ├── Station.java │ ├── Test.java │ ├── Website12306.java │ └── ZhuJiStation.java │ ├── v3 │ ├── Station.java │ ├── StationDynamicProxy.java │ ├── Test.java │ └── ZhuJiStation.java │ └── v4 │ ├── StationDynamicProxy.java │ ├── Test.java │ └── ZhuJiStation.java ├── 114-Template-Method ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── AbstractCook.java │ ├── GirlFriendCook.java │ ├── MeCook.java │ └── Test.java ├── 115-Iterator ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── Goods.java │ ├── GoodsAggregate.java │ ├── GoodsAggregateImpl.java │ ├── GoodsIterator.java │ ├── GoodsIteratorImpl.java │ └── Test.java ├── 116-Strategy ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ ├── v1 │ ├── DiamondMemberStrategy.java │ ├── DiscountActivity.java │ ├── GoldMemberStrategy.java │ ├── MemberStrategy.java │ ├── OrdinaryMemberStrategy.java │ ├── PlatinumMemberStrategy.java │ └── Test.java │ └── v2 │ ├── DiamondMemberStrategy.java │ ├── DiscountActivity.java │ ├── GoldMemberStrategy.java │ ├── MemberStrategy.java │ ├── MemberStrategyFactory.java │ ├── OrdinaryMemberStrategy.java │ ├── PlatinumMemberStrategy.java │ └── Test.java ├── 117-Interpreter ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── AddExpression.java │ ├── Expression.java │ ├── ExpressionContext.java │ ├── NumberExpression.java │ ├── SubExpression.java │ └── Test.java ├── 118-Observer ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── Customer.java │ ├── GauzeMask.java │ ├── Pharmacy.java │ └── Test.java ├── 119-Memento ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── CodeFile.java │ ├── CodeFileMemento.java │ ├── CodeFileMementoManager.java │ └── Test.java ├── 120-Command ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── AttendMeetingCommand.java │ ├── Boss.java │ ├── Command.java │ ├── LookVenueCommand.java │ ├── SendTweetsCommand.java │ ├── Staff.java │ └── Test.java ├── 121-Mediator ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── Boss.java │ ├── IStaff.java │ ├── PersonnelManager.java │ ├── Staff.java │ ├── Test.java │ ├── WeChatWorkGroup.java │ └── WorkGroup.java ├── 122-Chain-of-Responsibility ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── Approver.java │ ├── BossApprover.java │ ├── GroupLeaderApprover.java │ ├── InspectorGeneralApprover.java │ ├── PersonalLeaveRequest.java │ ├── PersonnelManagerApprover.java │ └── Test.java ├── 123-Visitor ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── ArticleContent.java │ ├── Blog.java │ ├── Content.java │ ├── GuestUserVisitor.java │ ├── RegisteredUserVisitor.java │ ├── ResourceContent.java │ ├── Test.java │ └── Visitor.java ├── 124-State ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhh │ └── v1 │ ├── ElectricLightContext.java │ ├── ElectricLightState.java │ ├── Test.java │ ├── TurnOffState.java │ └── TurnOnState.java ├── README.md └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /build/ 27 | 28 | ### VS Code ### 29 | .vscode/ 30 | -------------------------------------------------------------------------------- /001-OCP/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 001-OCP 14 | 设计原则-开闭原则 15 | 16 | -------------------------------------------------------------------------------- /001-OCP/src/main/java/com/zhh/v1/Food.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 食品类 6 | * @date 2020-02-04 23:42 7 | */ 8 | public class Food implements IGoods { 9 | 10 | private Integer id; 11 | 12 | private String name; 13 | 14 | private Double price; 15 | 16 | public Food(Integer id, String name, Double price) { 17 | this.id = id; 18 | this.name = name; 19 | this.price = price; 20 | } 21 | 22 | public Integer getId() { 23 | return this.id; 24 | } 25 | 26 | public String getName() { 27 | return this.name; 28 | } 29 | 30 | public Double getPrice() { 31 | return this.price; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /001-OCP/src/main/java/com/zhh/v1/IGoods.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品接口 6 | * @date 2020-02-04 22:47 7 | */ 8 | public interface IGoods { 9 | 10 | /** 11 | * 获取商品ID 12 | */ 13 | Integer getId(); 14 | 15 | /** 16 | * 获取商品名称 17 | */ 18 | String getName(); 19 | 20 | /** 21 | * 获取商品价格 22 | */ 23 | Double getPrice(); 24 | } 25 | -------------------------------------------------------------------------------- /001-OCP/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-04 23:57 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | IGoods iGoods = new Food(1, "巧克力", 9.9); 12 | 13 | String msg = String.format("商品ID: %s, 商品名称: %s, 商品价格: %s", 14 | iGoods.getId(), iGoods.getName(), iGoods.getPrice()); 15 | 16 | System.out.println(msg); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /001-OCP/src/main/java/com/zhh/v2/Food.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 食品类 6 | * @date 2020-02-04 23:42 7 | */ 8 | public class Food implements IGoods { 9 | 10 | private Integer id; 11 | 12 | private String name; 13 | 14 | private Double price; 15 | 16 | public Food(Integer id, String name, Double price) { 17 | this.id = id; 18 | this.name = name; 19 | this.price = price; 20 | } 21 | 22 | public Integer getId() { 23 | return this.id; 24 | } 25 | 26 | public String getName() { 27 | return this.name; 28 | } 29 | 30 | public Double getPrice() { 31 | return this.price; 32 | } 33 | 34 | public Double getDiscountPrice() { 35 | return getPrice() * 0.6; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /001-OCP/src/main/java/com/zhh/v2/IGoods.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品接口 6 | * @date 2020-02-04 22:47 7 | */ 8 | public interface IGoods { 9 | 10 | /** 11 | * 获取商品ID 12 | */ 13 | Integer getId(); 14 | 15 | /** 16 | * 获取商品名称 17 | */ 18 | String getName(); 19 | 20 | /** 21 | * 获取商品价格 22 | */ 23 | Double getPrice(); 24 | 25 | /** 26 | * 获取商品折扣价格 27 | */ 28 | Double getDiscountPrice(); 29 | } 30 | -------------------------------------------------------------------------------- /001-OCP/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-04 23:57 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | IGoods iGoods = new Food(1, "巧克力", 9.9); 12 | 13 | String msg = String.format("商品ID: %s, 商品名称: %s, 商品价格: %s, 商品折后价: %s", 14 | iGoods.getId(), iGoods.getName(), iGoods.getPrice(), iGoods.getDiscountPrice()); 15 | 16 | System.out.println(msg); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /001-OCP/src/main/java/com/zhh/v3/Food.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 食品类 6 | * @date 2020-02-04 23:42 7 | */ 8 | public class Food implements IGoods { 9 | 10 | private Integer id; 11 | 12 | private String name; 13 | 14 | private Double price; 15 | 16 | public Food(Integer id, String name, Double price) { 17 | this.id = id; 18 | this.name = name; 19 | this.price = price; 20 | } 21 | 22 | public Integer getId() { 23 | return this.id; 24 | } 25 | 26 | public String getName() { 27 | return this.name; 28 | } 29 | 30 | public Double getPrice() { 31 | return this.price; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /001-OCP/src/main/java/com/zhh/v3/FoodDiscount.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 食品打折类 6 | * @date 2020-02-05 00:27 7 | */ 8 | public class FoodDiscount extends Food { 9 | 10 | public FoodDiscount(Integer id, String name, Double price) { 11 | super(id, name, price); 12 | } 13 | 14 | public Double getOriginPrice() { 15 | return super.getPrice(); 16 | } 17 | 18 | @Override 19 | public Double getPrice() { 20 | return this.getOriginPrice() * 0.6; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /001-OCP/src/main/java/com/zhh/v3/IGoods.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品接口 6 | * @date 2020-02-04 22:47 7 | */ 8 | public interface IGoods { 9 | 10 | /** 11 | * 获取商品ID 12 | */ 13 | Integer getId(); 14 | 15 | /** 16 | * 获取商品名称 17 | */ 18 | String getName(); 19 | 20 | /** 21 | * 获取商品价格 22 | */ 23 | Double getPrice(); 24 | } 25 | -------------------------------------------------------------------------------- /001-OCP/src/main/java/com/zhh/v3/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-04 23:57 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | IGoods iGoods = new FoodDiscount(1, "巧克力", 9.9); 12 | 13 | FoodDiscount foodDiscount = (FoodDiscount) iGoods; 14 | 15 | String msg = String.format("商品ID: %s, 商品名称: %s, 商品原价: %s, 商品折后价: %s", 16 | foodDiscount.getId(), foodDiscount.getName(), foodDiscount.getOriginPrice(), foodDiscount.getPrice()); 17 | 18 | System.out.println(msg); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /002-DIP/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 002-DIP 14 | 设计原则-依赖倒置原则 15 | 16 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v1/Person.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 人物类 6 | * @date 2020-02-05 20:29 7 | */ 8 | public class Person { 9 | 10 | /** 11 | * 购买可乐 12 | */ 13 | public void buyCoke() { 14 | System.out.println("笔者买了可乐"); 15 | } 16 | 17 | /** 18 | * 购买薯片 19 | */ 20 | public void buyCrisps() { 21 | System.out.println("笔者买了薯片"); 22 | } 23 | 24 | /** 25 | * 购买巧克力 26 | */ 27 | public void buyChocolate() { 28 | System.out.println("笔者买了巧克力"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-05 20:40 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Person person = new Person(); 12 | person.buyCoke(); 13 | person.buyCrisps(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v2/Chocolate.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 巧克力商品 6 | * @date 2020-02-05 20:53 7 | */ 8 | public class Chocolate implements IGoods { 9 | 10 | public void buyGood() { 11 | System.out.println("笔者买了巧克力"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v2/Coke.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 可乐商品 6 | * @date 2020-02-05 20:52 7 | */ 8 | public class Coke implements IGoods { 9 | 10 | public void buyGood() { 11 | System.out.println("笔者买了可乐"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v2/Crisps.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 薯片商品 6 | * @date 2020-02-05 20:52 7 | */ 8 | public class Crisps implements IGoods { 9 | 10 | public void buyGood() { 11 | System.out.println("笔者买了薯片"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v2/IGoods.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品接口 6 | * @date 2020-02-05 20:50 7 | */ 8 | public interface IGoods { 9 | 10 | /** 11 | * 购买商品 12 | */ 13 | void buyGood(); 14 | } 15 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v2/Person.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 人物类 6 | * @date 2020-02-05 20:29 7 | */ 8 | public class Person { 9 | 10 | public void buyGood(IGoods iGoods) { 11 | iGoods.buyGood(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-05 20:40 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Person person = new Person(); 12 | person.buyGood(new Coke()); 13 | person.buyGood(new Crisps()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v3/Chocolate.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 巧克力商品 6 | * @date 2020-02-05 20:53 7 | */ 8 | public class Chocolate implements IGoods { 9 | 10 | public void buyGood() { 11 | System.out.println("笔者买了巧克力"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v3/Coke.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 可乐商品 6 | * @date 2020-02-05 20:52 7 | */ 8 | public class Coke implements IGoods { 9 | 10 | public void buyGood() { 11 | System.out.println("笔者买了可乐"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v3/Crisps.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 薯片商品 6 | * @date 2020-02-05 20:52 7 | */ 8 | public class Crisps implements IGoods { 9 | 10 | public void buyGood() { 11 | System.out.println("笔者买了薯片"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v3/IGoods.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品接口 6 | * @date 2020-02-05 20:50 7 | */ 8 | public interface IGoods { 9 | 10 | /** 11 | * 购买商品 12 | */ 13 | void buyGood(); 14 | } 15 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v3/Person.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 人物类 6 | * @date 2020-02-05 20:29 7 | */ 8 | public class Person { 9 | 10 | private IGoods iGoods; 11 | 12 | public Person(IGoods iGoods) { 13 | this.iGoods = iGoods; 14 | } 15 | 16 | public void buyGood() { 17 | iGoods.buyGood(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v3/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-05 20:40 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Person person = new Person(new Coke()); 12 | person.buyGood(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v4/Chocolate.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 巧克力商品 6 | * @date 2020-02-05 20:53 7 | */ 8 | public class Chocolate implements IGoods { 9 | 10 | public void buyGood() { 11 | System.out.println("笔者买了巧克力"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v4/Coke.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 可乐商品 6 | * @date 2020-02-05 20:52 7 | */ 8 | public class Coke implements IGoods { 9 | 10 | public void buyGood() { 11 | System.out.println("笔者买了可乐"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v4/Crisps.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 薯片商品 6 | * @date 2020-02-05 20:52 7 | */ 8 | public class Crisps implements IGoods { 9 | 10 | public void buyGood() { 11 | System.out.println("笔者买了薯片"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v4/IGoods.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品接口 6 | * @date 2020-02-05 20:50 7 | */ 8 | public interface IGoods { 9 | 10 | /** 11 | * 购买商品 12 | */ 13 | void buyGood(); 14 | } 15 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v4/Person.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 人物类 6 | * @date 2020-02-05 20:29 7 | */ 8 | public class Person { 9 | 10 | private IGoods iGoods; 11 | 12 | public void setIGoods(IGoods iGoods) { 13 | this.iGoods = iGoods; 14 | } 15 | 16 | public void buyGood() { 17 | iGoods.buyGood(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /002-DIP/src/main/java/com/zhh/v4/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-05 20:40 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Person person = new Person(); 12 | person.setIGoods(new Coke()); 13 | person.buyGood(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /003-SRP/003-SRP.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /003-SRP/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 003-SRP 14 | 设计原则-单一职责原则 15 | 16 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v1/Drink.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 饮料类 6 | * @date 2020-02-05 22:23 7 | */ 8 | public class Drink { 9 | 10 | /** 11 | * 饮料类型 12 | * @param drinkName 饮料名称 13 | */ 14 | public void type(String drinkName) { 15 | System.out.println(drinkName + "是汽水"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-05 22:25 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Drink drink = new Drink(); 12 | drink.type("可乐"); 13 | drink.type("牛奶"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v2/Drink.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 饮料类 6 | * @date 2020-02-05 22:23 7 | */ 8 | public class Drink { 9 | 10 | /** 11 | * 饮料类型 12 | * @param drinkName 饮料名称 13 | */ 14 | public void type(String drinkName) { 15 | if ("牛奶".equals(drinkName)) { 16 | System.out.println(drinkName + "是奶制品"); 17 | } else { 18 | System.out.println(drinkName + "是汽水"); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-05 22:25 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Drink drink = new Drink(); 12 | drink.type("可乐"); 13 | drink.type("牛奶"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v3/DairyDrink.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 奶制品饮料 6 | * @date 2020-02-05 22:34 7 | */ 8 | public class DairyDrink { 9 | 10 | /** 11 | * 饮料类型 12 | * @param drinkName 饮料名称 13 | */ 14 | public void type(String drinkName) { 15 | System.out.println(drinkName + "是奶制品"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v3/SodaDrink.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 汽水饮料 6 | * @date 2020-02-05 22:34 7 | */ 8 | public class SodaDrink { 9 | 10 | /** 11 | * 饮料类型 12 | * @param drinkName 饮料名称 13 | */ 14 | public void type(String drinkName) { 15 | System.out.println(drinkName + "是汽水"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v3/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-05 22:25 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | SodaDrink sodaDrink = new SodaDrink(); 12 | sodaDrink.type("可乐"); 13 | 14 | DairyDrink dairyDrink = new DairyDrink(); 15 | dairyDrink.type("牛奶"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v4/GoodsImpl.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品实现类 6 | * @date 2020-02-05 22:56 7 | */ 8 | public class GoodsImpl implements IGoodsManager, IGoodsInfo { 9 | 10 | public String getGoodsName() { 11 | return "商品名称"; 12 | } 13 | 14 | public Double getGoodsPrice() { 15 | return 0.0; 16 | } 17 | 18 | public void buyGoods() { 19 | System.out.println("购买商品"); 20 | } 21 | 22 | public void refundGoods() { 23 | System.out.println("退款商品"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v4/IGoods.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品接口 6 | * @date 2020-02-05 22:49 7 | */ 8 | public interface IGoods { 9 | 10 | /** 11 | * 获取商品名称 12 | */ 13 | String getGoodsName(); 14 | 15 | /** 16 | * 获取商品价格 17 | */ 18 | Double getGoodsPrice(); 19 | 20 | /** 21 | * 购买商品 22 | */ 23 | void buyGoods(); 24 | 25 | /** 26 | * 退款商品 27 | */ 28 | void refundGoods(); 29 | } 30 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v4/IGoodsInfo.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品信息接口 6 | * @date 2020-02-05 22:52 7 | */ 8 | public interface IGoodsInfo { 9 | 10 | /** 11 | * 获取商品名称 12 | */ 13 | String getGoodsName(); 14 | 15 | /** 16 | * 获取商品价格 17 | */ 18 | Double getGoodsPrice(); 19 | } 20 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v4/IGoodsManager.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品管理接口 6 | * @date 2020-02-05 22:53 7 | */ 8 | public interface IGoodsManager { 9 | 10 | /** 11 | * 购买商品 12 | */ 13 | void buyGoods(); 14 | 15 | /** 16 | * 退款商品 17 | */ 18 | void refundGoods(); 19 | } 20 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v5/Method.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v5; 2 | 3 | /** 4 | * @author zhh 5 | * @description 6 | * @date 2020-02-05 22:59 7 | */ 8 | public class Method { 9 | 10 | public void updateGoodsName(String goodsName) { 11 | // 模拟更新商品名称 12 | goodsName = "可乐"; 13 | } 14 | 15 | public void updateGoodsPrice(Double goodsPrice) { 16 | // 模拟更新商品价格 17 | goodsPrice = 3.0; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /003-SRP/src/main/java/com/zhh/v5/OldMethod.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v5; 2 | 3 | /** 4 | * @author zhh 5 | * @description 6 | * @date 2020-02-05 23:04 7 | */ 8 | public class OldMethod { 9 | 10 | /** 11 | * 更新商品信息 12 | * @param goodsName 商品名称 13 | * @param goodsPrice 商品价格 14 | */ 15 | public void updateGoodsInfo(String goodsName, Double goodsPrice) { 16 | // 模拟更新商品名称、价格 17 | goodsName = "可乐"; 18 | goodsPrice = 3.0; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /004-ISP/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 004-ISP 14 | 设计原则-接口隔离原则 15 | 16 | -------------------------------------------------------------------------------- /004-ISP/src/main/java/com/zhh/v1/Bird.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 鸟 6 | * @date 2020-02-06 00:52 7 | */ 8 | public class Bird implements IAnimal { 9 | 10 | public void eat() { 11 | System.out.println("鸟可以吃"); 12 | } 13 | 14 | public void fly() { 15 | System.out.println("鸟可以飞"); 16 | } 17 | 18 | public void makeSound() { 19 | System.out.println("鸟可以发出声音"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /004-ISP/src/main/java/com/zhh/v1/Dog.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 狗 6 | * @date 2020-02-06 00:51 7 | */ 8 | public class Dog implements IAnimal { 9 | 10 | public void eat() { 11 | System.out.println("狗可以吃"); 12 | } 13 | 14 | public void fly() { 15 | // 狗不能飞 16 | } 17 | 18 | public void makeSound() { 19 | System.out.println("狗可以发出声音"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /004-ISP/src/main/java/com/zhh/v1/IAnimal.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 动物接口 6 | * @date 2020-02-06 00:49 7 | */ 8 | public interface IAnimal { 9 | 10 | /** 11 | * 吃 12 | */ 13 | void eat(); 14 | 15 | /** 16 | * 飞 17 | */ 18 | void fly(); 19 | 20 | /** 21 | * 发出声音 22 | */ 23 | void makeSound(); 24 | } 25 | -------------------------------------------------------------------------------- /004-ISP/src/main/java/com/zhh/v2/Bird.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 鸟 6 | * @date 2020-02-06 00:52 7 | */ 8 | public class Bird implements IEat, IFly, IMakeSound { 9 | 10 | public void eat() { 11 | System.out.println("鸟可以吃"); 12 | } 13 | 14 | public void fly() { 15 | System.out.println("鸟可以飞"); 16 | } 17 | 18 | public void makeSound() { 19 | System.out.println("鸟可以发出声音"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /004-ISP/src/main/java/com/zhh/v2/Dog.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 狗 6 | * @date 2020-02-06 00:51 7 | */ 8 | public class Dog implements IEat, IMakeSound { 9 | 10 | public void eat() { 11 | System.out.println("狗可以吃"); 12 | } 13 | 14 | public void makeSound() { 15 | System.out.println("狗可以发出声音"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /004-ISP/src/main/java/com/zhh/v2/IEat.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 6 | * @date 2020-02-06 00:55 7 | */ 8 | public interface IEat { 9 | 10 | /** 11 | * 吃 12 | */ 13 | void eat(); 14 | } 15 | -------------------------------------------------------------------------------- /004-ISP/src/main/java/com/zhh/v2/IFly.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 6 | * @date 2020-02-06 00:55 7 | */ 8 | public interface IFly { 9 | 10 | /** 11 | * 飞 12 | */ 13 | void fly(); 14 | } 15 | -------------------------------------------------------------------------------- /004-ISP/src/main/java/com/zhh/v2/IMakeSound.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 6 | * @date 2020-02-06 00:56 7 | */ 8 | public interface IMakeSound { 9 | 10 | /** 11 | * 发出声音 12 | */ 13 | void makeSound(); 14 | } 15 | -------------------------------------------------------------------------------- /005-LoD/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 005-LoD 14 | 设计原则-迪米特法则 15 | 16 | -------------------------------------------------------------------------------- /005-LoD/src/main/java/com/zhh/v1/Boss.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author zhh 8 | * @description 老板类 9 | * @date 2020-02-07 10:59 10 | */ 11 | public class Boss { 12 | 13 | /** 14 | * 老板要求职工查商品的数量 15 | * @param staff 职工 16 | */ 17 | public void requireCheckNumber(Staff staff) { 18 | List goodsList = new ArrayList(); 19 | 20 | // 这里模拟从数据库查商品数量 21 | for (int i = 0; i < 50; i++) { 22 | goodsList.add(new Goods()); 23 | } 24 | 25 | staff.checkNumber(goodsList); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /005-LoD/src/main/java/com/zhh/v1/Goods.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品类 6 | * @date 2020-02-07 11:00 7 | */ 8 | public class Goods { 9 | } 10 | -------------------------------------------------------------------------------- /005-LoD/src/main/java/com/zhh/v1/Staff.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * @author zhh 7 | * @description 职工类 8 | * @date 2020-02-07 11:02 9 | */ 10 | public class Staff { 11 | 12 | /** 13 | * 职工查询商品数量 14 | * @param goods 商品列表 15 | */ 16 | public void checkNumber(List goods) { 17 | System.out.println("目前超市内商品总数为: " + goods.size()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /005-LoD/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-07 11:09 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Boss boss = new Boss(); 12 | Staff staff = new Staff(); 13 | 14 | boss.requireCheckNumber(staff); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /005-LoD/src/main/java/com/zhh/v2/Boss.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 老板类 6 | * @date 2020-02-07 10:59 7 | */ 8 | public class Boss { 9 | 10 | /** 11 | * 老板要求职工查商品的数量 12 | * @param staff 职工 13 | */ 14 | public void requireCheckNumber(Staff staff) { 15 | staff.checkNumber(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /005-LoD/src/main/java/com/zhh/v2/Goods.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品类 6 | * @date 2020-02-07 11:00 7 | */ 8 | public class Goods { 9 | } 10 | -------------------------------------------------------------------------------- /005-LoD/src/main/java/com/zhh/v2/Staff.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author zhh 8 | * @description 职工类 9 | * @date 2020-02-07 11:02 10 | */ 11 | public class Staff { 12 | 13 | /** 14 | * 职工查询商品数量 15 | */ 16 | public void checkNumber() { 17 | List goodsList = new ArrayList(); 18 | 19 | // 这里模拟从数据库查商品数量 20 | for (int i = 0; i < 50; i++) { 21 | goodsList.add(new Goods()); 22 | } 23 | System.out.println("目前超市内商品总数为: " + goodsList.size()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /005-LoD/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-07 11:09 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Boss boss = new Boss(); 12 | Staff staff = new Staff(); 13 | 14 | boss.requireCheckNumber(staff); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /006-LSP/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 006-LSP 14 | 设计原则-里氏替换原则 (LSP) 15 | 16 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v1/Food.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 食品类 6 | * @date 2020-02-04 23:42 7 | */ 8 | public class Food implements IGoods { 9 | 10 | private Integer id; 11 | 12 | private String name; 13 | 14 | private Double price; 15 | 16 | public Food(Integer id, String name, Double price) { 17 | this.id = id; 18 | this.name = name; 19 | this.price = price; 20 | } 21 | 22 | public Integer getId() { 23 | return this.id; 24 | } 25 | 26 | public String getName() { 27 | return this.name; 28 | } 29 | 30 | public Double getPrice() { 31 | return this.price; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v1/FoodDiscount.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 食品打折类 6 | * @date 2020-02-05 00:27 7 | */ 8 | public class FoodDiscount extends Food { 9 | 10 | public FoodDiscount(Integer id, String name, Double price) { 11 | super(id, name, price); 12 | } 13 | 14 | public Double getDiscountPrice() { 15 | return super.getPrice() * 0.6; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v1/IGoods.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品接口 6 | * @date 2020-02-04 22:47 7 | */ 8 | public interface IGoods { 9 | 10 | /** 11 | * 获取商品ID 12 | */ 13 | Integer getId(); 14 | 15 | /** 16 | * 获取商品名称 17 | */ 18 | String getName(); 19 | 20 | /** 21 | * 获取商品价格 22 | */ 23 | Double getPrice(); 24 | } 25 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-04 23:57 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | IGoods iGoods = new FoodDiscount(1, "巧克力", 9.9); 12 | 13 | FoodDiscount foodDiscount = (FoodDiscount) iGoods; 14 | 15 | String msg = String.format("商品ID: %s, 商品名称: %s, 商品原价: %s, 商品折后价: %s", 16 | foodDiscount.getId(), foodDiscount.getName(), foodDiscount.getPrice(), foodDiscount.getDiscountPrice()); 17 | 18 | System.out.println(msg); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v2/Child.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * @author zhh 7 | * @description 子类 8 | * @date 2020-02-07 16:51 9 | */ 10 | public class Child extends Father { 11 | 12 | // 重载方法 13 | public void method(Map map) { 14 | System.out.println("子类Map被执行"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v2/Father.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | import java.util.HashMap; 4 | 5 | /** 6 | * @author zhh 7 | * @description 父类 8 | * @date 2020-02-07 16:50 9 | */ 10 | public class Father { 11 | 12 | public void method(HashMap hashMap) { 13 | System.out.println("父类被执行"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | import java.util.HashMap; 4 | 5 | /** 6 | * @author zhh 7 | * @description 测试类 8 | * @date 2020-02-07 16:53 9 | */ 10 | public class Test { 11 | 12 | public static void main(String[] args) { 13 | Child child = new Child(); 14 | HashMap hashMap = new HashMap(); 15 | child.method(hashMap); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v3/Child.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | import java.util.HashMap; 4 | 5 | /** 6 | * @author zhh 7 | * @description 子类 8 | * @date 2020-02-07 16:51 9 | */ 10 | public class Child extends Father { 11 | 12 | // 重载方法 13 | public void method(HashMap hashMap) { 14 | System.out.println("子类HashMap被执行"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v3/Father.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * @author zhh 7 | * @description 父类 8 | * @date 2020-02-07 16:50 9 | */ 10 | public class Father { 11 | 12 | public void method(Map map) { 13 | System.out.println("父类被执行"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v3/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | import java.util.HashMap; 4 | 5 | /** 6 | * @author zhh 7 | * @description 测试类 8 | * @date 2020-02-07 16:53 9 | */ 10 | public class Test { 11 | 12 | public static void main(String[] args) { 13 | Child child = new Child(); 14 | HashMap hashMap = new HashMap(); 15 | child.method(hashMap); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v4/Child.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | import java.util.HashMap; 4 | 5 | /** 6 | * @author zhh 7 | * @description 子类 8 | * @date 2020-02-08 11:13 9 | */ 10 | public class Child extends Father { 11 | 12 | @Override 13 | public HashMap method() { 14 | HashMap hashMap = new HashMap(); 15 | System.out.println("子类method方法被执行"); 16 | hashMap.put("username", "zhaohaihao"); 17 | return hashMap; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v4/Father.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * @author zhh 7 | * @description 父类 8 | * @date 2020-02-07 17:11 9 | */ 10 | public abstract class Father { 11 | 12 | public abstract Map method(); 13 | } 14 | -------------------------------------------------------------------------------- /006-LSP/src/main/java/com/zhh/v4/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-08 11:17 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Child child = new Child(); 12 | System.out.println(child.method()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /007-CRP/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 007-CRP 14 | 设计原则-合成复用原则 15 | 16 | -------------------------------------------------------------------------------- /007-CRP/src/main/java/com/zhh/v1/DBConnection.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 数据库连接 6 | * @date 2020-02-09 13:45 7 | */ 8 | public class DBConnection { 9 | 10 | /** 11 | * 获取数据库连接 12 | * @return 13 | */ 14 | public String getConnection() { 15 | return "MySQL数据库连接"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /007-CRP/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-09 13:53 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | UserDao userDao = new UserDao(); 12 | userDao.addUser(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /007-CRP/src/main/java/com/zhh/v1/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 用户数据访问层 6 | * @date 2020-02-09 13:51 7 | */ 8 | public class UserDao extends DBConnection { 9 | 10 | /** 11 | * 新增用户 12 | */ 13 | public void addUser() { 14 | String connection = super.getConnection(); 15 | System.out.println(String.format("使用%s添加用户", connection)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /007-CRP/src/main/java/com/zhh/v2/DBConnection.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 数据库连接 6 | * @date 2020-02-09 13:45 7 | */ 8 | public abstract class DBConnection { 9 | 10 | /** 11 | * 获取数据库连接 12 | * @return 13 | */ 14 | public abstract String getConnection(); 15 | } 16 | -------------------------------------------------------------------------------- /007-CRP/src/main/java/com/zhh/v2/MySQLConnection.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description MySQL数据库连接 6 | * @date 2020-02-09 14:16 7 | */ 8 | public class MySQLConnection extends DBConnection { 9 | 10 | @Override 11 | public String getConnection() { 12 | return "MySQL数据库连接"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /007-CRP/src/main/java/com/zhh/v2/OracleConnection.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description Oracle数据库连接 6 | * @date 2020-02-09 14:16 7 | */ 8 | public class OracleConnection extends DBConnection { 9 | 10 | @Override 11 | public String getConnection() { 12 | return "Oracle数据库连接"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /007-CRP/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-09 13:53 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | UserDao userDao = new UserDao(); 12 | userDao.setConnection(new MySQLConnection()); 13 | userDao.addUser(); 14 | 15 | userDao.setConnection(new OracleConnection()); 16 | userDao.addUser(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /007-CRP/src/main/java/com/zhh/v2/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 用户数据访问层 6 | * @date 2020-02-09 13:51 7 | */ 8 | public class UserDao { 9 | 10 | private DBConnection connection; 11 | 12 | public void setConnection(DBConnection connection) { 13 | this.connection = connection; 14 | } 15 | 16 | /** 17 | * 新增用户 18 | */ 19 | public void addUser() { 20 | System.out.println(String.format("使用%s添加用户", connection.getConnection())); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /101-Simple-Factory/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 101-Simple-Factory 14 | 创建型-简单工厂模式 15 | 16 | -------------------------------------------------------------------------------- /101-Simple-Factory/src/main/java/com/zhh/v1/IPhone.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 苹果手机 6 | * @date 2020-02-10 11:07 7 | */ 8 | public class IPhone implements Phone { 9 | 10 | public void call() { 11 | System.out.println("使用苹果手机拨打电话"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /101-Simple-Factory/src/main/java/com/zhh/v1/MiPhone.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 小米手机 6 | * @date 2020-02-10 11:08 7 | */ 8 | public class MiPhone implements Phone { 9 | 10 | public void call() { 11 | System.out.println("使用小米手机拨打电话"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /101-Simple-Factory/src/main/java/com/zhh/v1/Phone.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 手机接口 6 | * @date 2020-02-10 11:04 7 | */ 8 | public interface Phone { 9 | 10 | /** 11 | * 拨打电话 12 | */ 13 | void call(); 14 | } 15 | -------------------------------------------------------------------------------- /101-Simple-Factory/src/main/java/com/zhh/v1/PhoneFactory.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 手机工厂类 6 | * @date 2020-02-10 11:09 7 | */ 8 | public class PhoneFactory { 9 | 10 | /** 11 | * 获取手机 12 | * @param type 手机品牌类型 13 | * @return 14 | */ 15 | public static Phone getPhone(String type) { 16 | if ("iphone".equalsIgnoreCase(type)) { 17 | return new IPhone(); 18 | } else if ("miphone".equalsIgnoreCase(type)) { 19 | return new MiPhone(); 20 | } 21 | return null; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /101-Simple-Factory/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-10 11:11 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Phone phone = PhoneFactory.getPhone("iphone"); 12 | if (phone != null) { 13 | phone.call(); 14 | } else { 15 | System.out.println("无此手机品牌类型"); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /101-Simple-Factory/src/main/java/com/zhh/v2/IPhone.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 苹果手机 6 | * @date 2020-02-10 11:07 7 | */ 8 | public class IPhone implements Phone { 9 | 10 | public void call() { 11 | System.out.println("使用苹果手机拨打电话"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /101-Simple-Factory/src/main/java/com/zhh/v2/MiPhone.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 小米手机 6 | * @date 2020-02-10 11:08 7 | */ 8 | public class MiPhone implements Phone { 9 | 10 | public void call() { 11 | System.out.println("使用小米手机拨打电话"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /101-Simple-Factory/src/main/java/com/zhh/v2/Phone.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 手机接口 6 | * @date 2020-02-10 11:04 7 | */ 8 | public interface Phone { 9 | 10 | /** 11 | * 拨打电话 12 | */ 13 | void call(); 14 | } 15 | -------------------------------------------------------------------------------- /101-Simple-Factory/src/main/java/com/zhh/v2/PhoneFactory.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 手机工厂类 6 | * @date 2020-02-10 11:09 7 | */ 8 | public class PhoneFactory { 9 | 10 | /** 11 | * 获取手机(方案改进,利用反射) 12 | * @param clazz 类对象 13 | * @return 14 | */ 15 | public static Phone getPhone(Class clazz) { 16 | try { 17 | return (Phone) Class.forName(clazz.getName()).newInstance(); 18 | } catch (Exception e) { 19 | e.printStackTrace(); 20 | } 21 | return null; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /101-Simple-Factory/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-10 11:11 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Phone phone = PhoneFactory.getPhone(IPhone.class); 12 | if (phone != null) { 13 | phone.call(); 14 | } else { 15 | System.out.println("无此手机品牌类型"); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /102-Factory-Method/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 102-Factory 14 | 创建型-工厂方法模式 15 | 16 | -------------------------------------------------------------------------------- /102-Factory-Method/src/main/java/com/zhh/v1/IPhone.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 苹果手机 6 | * @date 2020-02-10 11:07 7 | */ 8 | public class IPhone implements Phone { 9 | 10 | public void call() { 11 | System.out.println("使用苹果手机拨打电话"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /102-Factory-Method/src/main/java/com/zhh/v1/IPhoneFactory.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 苹果手机工厂类 6 | * @date 2020-02-10 15:12 7 | */ 8 | public class IPhoneFactory implements PhoneFactory { 9 | 10 | public Phone getPhone() { 11 | return new IPhone(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /102-Factory-Method/src/main/java/com/zhh/v1/MiPhone.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 小米手机 6 | * @date 2020-02-10 11:08 7 | */ 8 | public class MiPhone implements Phone { 9 | 10 | public void call() { 11 | System.out.println("使用小米手机拨打电话"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /102-Factory-Method/src/main/java/com/zhh/v1/MiPhoneFactory.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 小米手机工厂类 6 | * @date 2020-02-10 15:12 7 | */ 8 | public class MiPhoneFactory implements PhoneFactory { 9 | 10 | public Phone getPhone() { 11 | return new MiPhone(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /102-Factory-Method/src/main/java/com/zhh/v1/Phone.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 手机接口 6 | * @date 2020-02-10 11:04 7 | */ 8 | public interface Phone { 9 | 10 | /** 11 | * 拨打电话 12 | */ 13 | void call(); 14 | } 15 | -------------------------------------------------------------------------------- /102-Factory-Method/src/main/java/com/zhh/v1/PhoneFactory.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 手机工厂接口 6 | * @date 2020-02-10 11:09 7 | */ 8 | public interface PhoneFactory { 9 | 10 | /** 11 | * 获取手机 12 | */ 13 | Phone getPhone(); 14 | } 15 | -------------------------------------------------------------------------------- /102-Factory-Method/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-10 11:11 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | PhoneFactory phoneFactory = new IPhoneFactory(); 12 | Phone phone = phoneFactory.getPhone(); 13 | phone.call(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /103-Abstract-Factory/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 103-Abstract-Factory 14 | 创建型-抽象工厂模式 15 | 16 | -------------------------------------------------------------------------------- /103-Abstract-Factory/src/main/java/com/zhh/v1/Computer.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 电脑类 6 | * @date 2020-02-11 00:57 7 | */ 8 | public abstract class Computer { 9 | 10 | public abstract void produce(); 11 | } 12 | -------------------------------------------------------------------------------- /103-Abstract-Factory/src/main/java/com/zhh/v1/ElectronicProductFactory.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 电子产品工厂类 6 | * @date 2020-02-11 00:55 7 | */ 8 | public interface ElectronicProductFactory { 9 | 10 | Phone getPhone(); 11 | 12 | Computer getComputer(); 13 | } 14 | -------------------------------------------------------------------------------- /103-Abstract-Factory/src/main/java/com/zhh/v1/IComputer.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 苹果电脑类 6 | * @date 2020-02-11 01:04 7 | */ 8 | public class IComputer extends Computer { 9 | 10 | @Override 11 | public void produce() { 12 | System.out.println("生产苹果电脑"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /103-Abstract-Factory/src/main/java/com/zhh/v1/IElectronicProductFactory.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 苹果电子产品工厂类 6 | * @date 2020-02-11 01:00 7 | */ 8 | public class IElectronicProductFactory implements ElectronicProductFactory { 9 | 10 | public Phone getPhone() { 11 | return new IPhone(); 12 | } 13 | 14 | public Computer getComputer() { 15 | return new IComputer(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /103-Abstract-Factory/src/main/java/com/zhh/v1/IPhone.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 苹果手机类 6 | * @date 2020-02-11 01:04 7 | */ 8 | public class IPhone extends Phone { 9 | 10 | @Override 11 | public void produce() { 12 | System.out.println("生产苹果手机"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /103-Abstract-Factory/src/main/java/com/zhh/v1/MiComputer.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 小米电脑类 6 | * @date 2020-02-11 01:02 7 | */ 8 | public class MiComputer extends Computer { 9 | 10 | @Override 11 | public void produce() { 12 | System.out.println("生产小米电脑"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /103-Abstract-Factory/src/main/java/com/zhh/v1/MiElectronicProductFactory.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 小米电子产品工厂类 6 | * @date 2020-02-11 01:01 7 | */ 8 | public class MiElectronicProductFactory implements ElectronicProductFactory { 9 | 10 | public Phone getPhone() { 11 | return new MiPhone(); 12 | } 13 | 14 | public Computer getComputer() { 15 | return new MiComputer(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /103-Abstract-Factory/src/main/java/com/zhh/v1/MiPhone.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 小米手机类 6 | * @date 2020-02-11 01:05 7 | */ 8 | public class MiPhone extends Phone { 9 | 10 | @Override 11 | public void produce() { 12 | System.out.println("生产小米手机"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /103-Abstract-Factory/src/main/java/com/zhh/v1/Phone.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 手机类 6 | * @date 2020-02-11 00:57 7 | */ 8 | public abstract class Phone { 9 | 10 | public abstract void produce(); 11 | } 12 | -------------------------------------------------------------------------------- /103-Abstract-Factory/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-11 01:19 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | ElectronicProductFactory factory = new IElectronicProductFactory(); 12 | Computer computer = factory.getComputer(); 13 | computer.produce(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /104-Builder/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 104-Builder 14 | 创建型-建造者模式 15 | 16 | -------------------------------------------------------------------------------- /104-Builder/src/main/java/com/zhh/v1/Computer.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 电脑类 6 | * @date 2020-02-11 14:01 7 | */ 8 | public class Computer { 9 | 10 | /** 11 | * cpu 12 | */ 13 | private String cpu; 14 | 15 | /** 16 | * 主板 17 | */ 18 | private String mainboard; 19 | 20 | /** 21 | * 内存 22 | */ 23 | private String memory; 24 | 25 | /** 26 | * 硬盘 27 | */ 28 | private String hardDisk; 29 | 30 | /** 31 | * 显卡 32 | */ 33 | private String gpu; 34 | 35 | /** 36 | * 机箱 37 | */ 38 | private String crate; 39 | 40 | /** 41 | * 电源 42 | */ 43 | private String powerSupply; 44 | 45 | /** 46 | * 显示器 47 | */ 48 | private String monitor; 49 | 50 | public String getCpu() { 51 | return cpu; 52 | } 53 | 54 | public void setCpu(String cpu) { 55 | this.cpu = cpu; 56 | } 57 | 58 | public String getMainboard() { 59 | return mainboard; 60 | } 61 | 62 | public void setMainboard(String mainboard) { 63 | this.mainboard = mainboard; 64 | } 65 | 66 | public String getMemory() { 67 | return memory; 68 | } 69 | 70 | public void setMemory(String memory) { 71 | this.memory = memory; 72 | } 73 | 74 | public String getHardDisk() { 75 | return hardDisk; 76 | } 77 | 78 | public void setHardDisk(String hardDisk) { 79 | this.hardDisk = hardDisk; 80 | } 81 | 82 | public String getGpu() { 83 | return gpu; 84 | } 85 | 86 | public void setGpu(String gpu) { 87 | this.gpu = gpu; 88 | } 89 | 90 | public String getCrate() { 91 | return crate; 92 | } 93 | 94 | public void setCrate(String crate) { 95 | this.crate = crate; 96 | } 97 | 98 | public String getPowerSupply() { 99 | return powerSupply; 100 | } 101 | 102 | public void setPowerSupply(String powerSupply) { 103 | this.powerSupply = powerSupply; 104 | } 105 | 106 | public String getMonitor() { 107 | return monitor; 108 | } 109 | 110 | public void setMonitor(String monitor) { 111 | this.monitor = monitor; 112 | } 113 | 114 | @Override 115 | public String toString() { 116 | return "Computer{" + "cpu='" + cpu + '\'' + ", mainboard='" + mainboard + '\'' + ", memory='" + memory + '\'' + 117 | ", hardDisk='" + hardDisk + '\'' + ", gpu='" + gpu + '\'' + ", crate='" + crate + '\'' + 118 | ", powerSupply='" + powerSupply + '\'' + ", monitor='" + monitor + '\'' + '}'; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /104-Builder/src/main/java/com/zhh/v1/ComputerActualBuilder.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 实际的电脑建造者类 6 | * @date 2020-02-11 14:41 7 | */ 8 | public class ComputerActualBuilder extends ComputerBuilder { 9 | 10 | private Computer computer = new Computer(); 11 | 12 | @Override 13 | public void buyCpu(String cpu) { 14 | computer.setCpu(cpu); 15 | } 16 | 17 | @Override 18 | public void buyMainboard(String mainboard) { 19 | computer.setMainboard(mainboard); 20 | } 21 | 22 | @Override 23 | public void buyMemory(String memory) { 24 | computer.setMemory(memory); 25 | } 26 | 27 | @Override 28 | public void buyHardDisk(String hardDisk) { 29 | computer.setHardDisk(hardDisk); 30 | } 31 | 32 | @Override 33 | public void buyGpu(String gpu) { 34 | computer.setGpu(gpu); 35 | } 36 | 37 | @Override 38 | public void buyCrate(String crate) { 39 | computer.setCrate(crate); 40 | } 41 | 42 | @Override 43 | public void buyPowerSupply(String powerSupply) { 44 | computer.setPowerSupply(powerSupply); 45 | } 46 | 47 | @Override 48 | public void buyMonitor(String monitor) { 49 | computer.setMonitor(monitor); 50 | } 51 | 52 | @Override 53 | public Computer assembleComputer() { 54 | return computer; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /104-Builder/src/main/java/com/zhh/v1/ComputerBuilder.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 抽象电脑建造者类 6 | * @date 2020-02-11 14:32 7 | */ 8 | public abstract class ComputerBuilder { 9 | 10 | public abstract void buyCpu(String cpu); 11 | public abstract void buyMainboard(String mainboard); 12 | public abstract void buyMemory(String memory); 13 | public abstract void buyHardDisk(String hardDisk); 14 | public abstract void buyGpu(String gpu); 15 | public abstract void buyCrate(String crate); 16 | public abstract void buyPowerSupply(String powerSupply); 17 | public abstract void buyMonitor(String monitor); 18 | 19 | /** 20 | * 组装电脑 21 | */ 22 | public abstract Computer assembleComputer(); 23 | } 24 | -------------------------------------------------------------------------------- /104-Builder/src/main/java/com/zhh/v1/Enterprise.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商家类 6 | * @date 2020-02-11 14:55 7 | */ 8 | public class Enterprise { 9 | 10 | private ComputerBuilder computerBuilder; 11 | 12 | public void setComputerBuilder(ComputerBuilder computerBuilder) { 13 | this.computerBuilder = computerBuilder; 14 | } 15 | 16 | /** 17 | * 组装电脑 18 | */ 19 | public Computer assembleComputer(String cpu, String mainboard, 20 | String memory, String hardDisk, 21 | String gpu, String crate, 22 | String powerSupply, String monitor) { 23 | computerBuilder.buyCpu(cpu); 24 | computerBuilder.buyMainboard(mainboard); 25 | computerBuilder.buyMemory(memory); 26 | computerBuilder.buyHardDisk(hardDisk); 27 | computerBuilder.buyGpu(gpu); 28 | computerBuilder.buyCrate(crate); 29 | computerBuilder.buyPowerSupply(powerSupply); 30 | computerBuilder.buyMonitor(monitor); 31 | 32 | return computerBuilder.assembleComputer(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /104-Builder/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-11 15:05 7 | */ 8 | public class Test { 9 | public static void main(String[] args) { 10 | ComputerBuilder computerBuilder = new ComputerActualBuilder(); 11 | Enterprise enterprise = new Enterprise(); 12 | enterprise.setComputerBuilder(computerBuilder); 13 | Computer computer = enterprise.assembleComputer("Intel 酷睿i7 8700K", 14 | "影驰B360M-M.2", 15 | "影驰GAMER 8GB DDR4 2400", 16 | "希捷Barracuda 1TB 7200转 64MB", 17 | "七彩虹iGame750Ti 烈焰战神U-Twin-2GD5", 18 | "金河田峥嵘Z30", 19 | "航嘉WD600K", 20 | "三星C27F390FHC"); 21 | 22 | System.out.println(computer); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /104-Builder/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-11 15:05 7 | */ 8 | public class Test { 9 | public static void main(String[] args) { 10 | Computer computer = new Computer.ComputerBuilder() 11 | .buyCpu("Intel 酷睿i7 8700K") 12 | .buyMainboard("影驰B360M-M.2") 13 | .buyMemory("影驰GAMER 8GB DDR4 2400") 14 | .buyHardDisk("希捷Barracuda 1TB 7200转 64MB") 15 | .buyGpu("七彩虹iGame750Ti 烈焰战神U-Twin-2GD5") 16 | .buyCrate("金河田峥嵘Z30") 17 | .buyPowerSupply("航嘉WD600K") 18 | .buyMonitor("三星C27F390FHC") 19 | .build(); 20 | 21 | System.out.println(computer); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /105-Singleton/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 105-Singleton 14 | 创建型-单例模式 15 | 16 | 17 | 18 | org.apache.commons 19 | commons-lang3 20 | 3.8.1 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /105-Singleton/src/main/java/com/zhh/v1/LazySingleton.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 单例模式-懒汉式 6 | * @date 2020-02-11 19:56 7 | */ 8 | public class LazySingleton { 9 | 10 | private static LazySingleton instance = null; 11 | 12 | private LazySingleton() {} 13 | 14 | public static LazySingleton getInstance() { 15 | if (instance == null) { 16 | instance = new LazySingleton(); 17 | } 18 | return instance; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /105-Singleton/src/main/java/com/zhh/v10/EnumSingleton.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v10; 2 | 3 | /** 4 | * @author zhh 5 | * @description 枚举单例模式 6 | * @date 2020-02-12 16:08 7 | */ 8 | public enum EnumSingleton { 9 | INSTANCE { 10 | @Override 11 | public void method() { 12 | System.out.println("任何其他方法"); 13 | } 14 | }; 15 | 16 | public abstract void method(); 17 | } 18 | -------------------------------------------------------------------------------- /105-Singleton/src/main/java/com/zhh/v11/ContainerSingleton.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v11; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | /** 9 | * @author zhh 10 | * @description 容器单例 11 | * @date 2020-02-12 16:28 12 | */ 13 | public class ContainerSingleton { 14 | 15 | private static Map map = new HashMap(); 16 | 17 | private ContainerSingleton() {} 18 | 19 | public static void putInstance(String key, Object instance) { 20 | if (StringUtils.isNotBlank(key) && instance != null) { 21 | if (!map.containsKey(key)) { 22 | map.put(key, instance); 23 | } 24 | } 25 | } 26 | 27 | public static Object getInstance(String key) { 28 | return map.get(key); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /105-Singleton/src/main/java/com/zhh/v12/ThreadLocalSingleton.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v12; 2 | 3 | /** 4 | * @author zhh 5 | * @description ThreadLocal线程单例 6 | * @date 2020-02-12 17:03 7 | */ 8 | public class ThreadLocalSingleton { 9 | 10 | private static final ThreadLocal THREAD_LOCAL = new ThreadLocal() { 11 | // 重写初始化方法 12 | @Override 13 | protected ThreadLocalSingleton initialValue() { 14 | return new ThreadLocalSingleton(); 15 | } 16 | }; 17 | 18 | private ThreadLocalSingleton() {} 19 | 20 | public static ThreadLocalSingleton getInstance() { 21 | return THREAD_LOCAL.get(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /105-Singleton/src/main/java/com/zhh/v2/LazySingleton.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 单例模式-懒汉式 6 | * @date 2020-02-11 19:56 7 | */ 8 | public class LazySingleton { 9 | 10 | private static LazySingleton instance = null; 11 | 12 | private LazySingleton() {} 13 | 14 | public static synchronized LazySingleton getInstance() { 15 | if (instance == null) { 16 | instance = new LazySingleton(); 17 | } 18 | return instance; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /105-Singleton/src/main/java/com/zhh/v3/LazySingleton.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 单例模式-懒汉式 6 | * @date 2020-02-11 19:56 7 | */ 8 | public class LazySingleton { 9 | 10 | private static LazySingleton instance = null; 11 | 12 | private LazySingleton() {} 13 | 14 | public static LazySingleton getInstance() { 15 | synchronized (LazySingleton.class) { 16 | if (instance == null) { 17 | instance = new LazySingleton(); 18 | } 19 | } 20 | return instance; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /105-Singleton/src/main/java/com/zhh/v4/LazySingleton.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 单例模式-懒汉式(双重检查) 6 | * @date 2020-02-11 19:56 7 | */ 8 | public class LazySingleton { 9 | 10 | private static LazySingleton instance = null; 11 | 12 | private LazySingleton() {} 13 | 14 | public static LazySingleton getInstance() { 15 | if (instance == null) { 16 | synchronized (LazySingleton.class) { 17 | if (instance == null){ 18 | instance = new LazySingleton(); 19 | } 20 | } 21 | } 22 | return instance; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /105-Singleton/src/main/java/com/zhh/v5/LazySingleton.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v5; 2 | 3 | /** 4 | * @author zhh 5 | * @description 单例模式-懒汉式(双重检查) 6 | * @date 2020-02-11 19:56 7 | */ 8 | public class LazySingleton { 9 | 10 | private static volatile LazySingleton instance = null; 11 | 12 | private LazySingleton() {} 13 | 14 | public static LazySingleton getInstance() { 15 | if (instance == null) { 16 | synchronized (LazySingleton.class) { 17 | if (instance == null){ 18 | instance = new LazySingleton(); 19 | } 20 | } 21 | } 22 | return instance; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /105-Singleton/src/main/java/com/zhh/v6/StaticInnerClassSingleton.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v6; 2 | 3 | /** 4 | * @author zhh 5 | * @description 静态内部类单例模式 6 | * @date 2020-02-12 01:36 7 | */ 8 | public class StaticInnerClassSingleton { 9 | 10 | private static class SingletonHolder { 11 | private static StaticInnerClassSingleton instance = new StaticInnerClassSingleton(); 12 | } 13 | 14 | private StaticInnerClassSingleton() {} 15 | 16 | public static StaticInnerClassSingleton getInstance() { 17 | return SingletonHolder.instance; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /105-Singleton/src/main/java/com/zhh/v7/HungrySingleton.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v7; 2 | 3 | /** 4 | * @author zhh 5 | * @description 单例模式-饿汉式 6 | * @date 2020-02-12 12:59 7 | */ 8 | public class HungrySingleton { 9 | 10 | private static HungrySingleton instance = new HungrySingleton(); 11 | 12 | private HungrySingleton() {} 13 | 14 | public static HungrySingleton getInstance() { 15 | return instance; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /105-Singleton/src/main/java/com/zhh/v8/HungrySingleton.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v8; 2 | 3 | /** 4 | * @author zhh 5 | * @description 单例模式-饿汉式 6 | * @date 2020-02-12 12:59 7 | */ 8 | public class HungrySingleton { 9 | 10 | private static HungrySingleton instance; 11 | 12 | static { 13 | instance = new HungrySingleton(); 14 | } 15 | 16 | private HungrySingleton() {} 17 | 18 | public static HungrySingleton getInstance() { 19 | return instance; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /105-Singleton/src/main/java/com/zhh/v9/EnumSingleton.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v9; 2 | 3 | /** 4 | * @author zhh 5 | * @description 枚举单例模式 6 | * @date 2020-02-12 16:08 7 | */ 8 | public enum EnumSingleton { 9 | INSTANCE; 10 | 11 | public void method() { 12 | System.out.println("任何其他方法"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /106-Prototype/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 106-Prototype 14 | 创建型-原型模式 15 | 16 | -------------------------------------------------------------------------------- /106-Prototype/src/main/java/com/zhh/v1/Resume.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * @author zhh 7 | * @description 简历类 8 | * @date 2020-02-13 00:13 9 | */ 10 | public class Resume implements Cloneable { 11 | 12 | /** 13 | * 姓名 14 | */ 15 | private String name; 16 | 17 | /** 18 | * 生日 19 | */ 20 | private Date birthday; 21 | 22 | /** 23 | * 性别 24 | */ 25 | private String sex; 26 | 27 | /** 28 | * 学校 29 | */ 30 | private String school; 31 | 32 | /** 33 | * 工龄 34 | */ 35 | private String socialWorkAge; 36 | 37 | /** 38 | * 公司 39 | */ 40 | private String company; 41 | 42 | /** 43 | * 工作描述 44 | */ 45 | private String workDescription; 46 | 47 | public Resume() { 48 | System.out.println("Resume类的无参构造函数"); 49 | } 50 | 51 | public String getName() { 52 | return name; 53 | } 54 | 55 | public void setName(String name) { 56 | this.name = name; 57 | } 58 | 59 | public Date getBirthday() { 60 | return birthday; 61 | } 62 | 63 | public void setBirthday(Date birthday) { 64 | this.birthday = birthday; 65 | } 66 | 67 | public String getSex() { 68 | return sex; 69 | } 70 | 71 | public void setSex(String sex) { 72 | this.sex = sex; 73 | } 74 | 75 | public String getSchool() { 76 | return school; 77 | } 78 | 79 | public void setSchool(String school) { 80 | this.school = school; 81 | } 82 | 83 | public String getSocialWorkAge() { 84 | return socialWorkAge; 85 | } 86 | 87 | public void setSocialWorkAge(String socialWorkAge) { 88 | this.socialWorkAge = socialWorkAge; 89 | } 90 | 91 | public String getCompany() { 92 | return company; 93 | } 94 | 95 | public void setCompany(String company) { 96 | this.company = company; 97 | } 98 | 99 | public String getWorkDescription() { 100 | return workDescription; 101 | } 102 | 103 | public void setWorkDescription(String workDescription) { 104 | this.workDescription = workDescription; 105 | } 106 | 107 | @Override 108 | protected Object clone() throws CloneNotSupportedException { 109 | System.out.println("开始克隆简历"); 110 | return super.clone(); 111 | } 112 | 113 | public void display() { 114 | System.out.println(String.format("姓名: %s", name)); 115 | System.out.println(String.format("生日: %s, 性别: %s, 毕业院校: %s, 工龄: %s", birthday, sex, school, socialWorkAge)); 116 | System.out.println(String.format("公司: %s, 工作描述: %s", company, workDescription)); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /106-Prototype/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * @author zhh 7 | * @description 测试类 8 | * @date 2020-02-13 00:20 9 | */ 10 | public class Test { 11 | 12 | public static void main(String[] args) throws Exception { 13 | // 原版简历 14 | Resume resumeA = new Resume(); 15 | resumeA.setName("海豪"); 16 | resumeA.setBirthday(new Date(94, 0, 1)); 17 | resumeA.setSex("男"); 18 | resumeA.setSchool("XXXX大学"); 19 | resumeA.setSocialWorkAge("1"); 20 | resumeA.setCompany("A科技有限公司"); 21 | resumeA.setWorkDescription("在A公司的工作描述"); 22 | 23 | // 拷贝简历改版 24 | Resume resumeB = (Resume) resumeA.clone(); 25 | resumeB.setSocialWorkAge("3"); 26 | resumeB.setCompany("B科技有限公司"); 27 | resumeB.setWorkDescription("在B公司的工作描述"); 28 | 29 | System.out.println("=====简历修改前====="); 30 | resumeA.display(); 31 | System.out.println("=====简历修改后====="); 32 | resumeB.display(); 33 | 34 | System.out.println("是否为同一对象: " + (resumeA == resumeB)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /106-Prototype/src/main/java/com/zhh/v2/Resume.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * @author zhh 7 | * @description 简历类 8 | * @date 2020-02-13 00:13 9 | */ 10 | public class Resume implements Cloneable { 11 | 12 | /** 13 | * 姓名 14 | */ 15 | private String name; 16 | 17 | /** 18 | * 生日 19 | */ 20 | private Date birthday; 21 | 22 | /** 23 | * 性别 24 | */ 25 | private String sex; 26 | 27 | /** 28 | * 学校 29 | */ 30 | private String school; 31 | 32 | /** 33 | * 工龄 34 | */ 35 | private String socialWorkAge; 36 | 37 | /** 38 | * 公司 39 | */ 40 | private String company; 41 | 42 | /** 43 | * 工作描述 44 | */ 45 | private String workDescription; 46 | 47 | public Resume() { 48 | System.out.println("Resume类的无参构造函数"); 49 | } 50 | 51 | public String getName() { 52 | return name; 53 | } 54 | 55 | public void setName(String name) { 56 | this.name = name; 57 | } 58 | 59 | public Date getBirthday() { 60 | return birthday; 61 | } 62 | 63 | public void setBirthday(Date birthday) { 64 | this.birthday = birthday; 65 | } 66 | 67 | public String getSex() { 68 | return sex; 69 | } 70 | 71 | public void setSex(String sex) { 72 | this.sex = sex; 73 | } 74 | 75 | public String getSchool() { 76 | return school; 77 | } 78 | 79 | public void setSchool(String school) { 80 | this.school = school; 81 | } 82 | 83 | public String getSocialWorkAge() { 84 | return socialWorkAge; 85 | } 86 | 87 | public void setSocialWorkAge(String socialWorkAge) { 88 | this.socialWorkAge = socialWorkAge; 89 | } 90 | 91 | public String getCompany() { 92 | return company; 93 | } 94 | 95 | public void setCompany(String company) { 96 | this.company = company; 97 | } 98 | 99 | public String getWorkDescription() { 100 | return workDescription; 101 | } 102 | 103 | public void setWorkDescription(String workDescription) { 104 | this.workDescription = workDescription; 105 | } 106 | 107 | @Override 108 | protected Object clone() throws CloneNotSupportedException { 109 | System.out.println("开始克隆简历"); 110 | return super.clone(); 111 | } 112 | 113 | public void display() { 114 | System.out.println(String.format("姓名: %s", name)); 115 | System.out.println(String.format("生日: %s, 性别: %s, 毕业院校: %s, 工龄: %s", birthday, sex, school, socialWorkAge)); 116 | System.out.println(String.format("公司: %s, 工作描述: %s", company, workDescription)); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /106-Prototype/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * @author zhh 7 | * @description 测试类 8 | * @date 2020-02-13 00:20 9 | */ 10 | public class Test { 11 | 12 | public static void main(String[] args) throws Exception { 13 | // 原版简历 14 | Resume resumeA = new Resume(); 15 | resumeA.setName("海豪"); 16 | resumeA.setBirthday(new Date(94, 0, 1)); 17 | resumeA.setSex("男"); 18 | resumeA.setSchool("XXXX大学"); 19 | resumeA.setSocialWorkAge("1"); 20 | resumeA.setCompany("A科技有限公司"); 21 | resumeA.setWorkDescription("在A公司的工作描述"); 22 | 23 | // 拷贝简历 24 | Resume resumeB = (Resume) resumeA.clone(); 25 | 26 | System.out.println("=====原版简历====="); 27 | resumeA.display(); 28 | System.out.println("=====拷贝简历====="); 29 | resumeB.display(); 30 | 31 | resumeA.getBirthday().setDate(5); 32 | 33 | System.out.println(); 34 | System.out.println("=====修改后原版简历====="); 35 | resumeA.display(); 36 | System.out.println("=====修改后拷贝简历====="); 37 | resumeB.display(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /106-Prototype/src/main/java/com/zhh/v3/Resume.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * @author zhh 7 | * @description 简历类 8 | * @date 2020-02-13 00:13 9 | */ 10 | public class Resume implements Cloneable { 11 | 12 | /** 13 | * 姓名 14 | */ 15 | private String name; 16 | 17 | /** 18 | * 生日 19 | */ 20 | private Date birthday; 21 | 22 | /** 23 | * 性别 24 | */ 25 | private String sex; 26 | 27 | /** 28 | * 学校 29 | */ 30 | private String school; 31 | 32 | /** 33 | * 工龄 34 | */ 35 | private String socialWorkAge; 36 | 37 | /** 38 | * 公司 39 | */ 40 | private String company; 41 | 42 | /** 43 | * 工作描述 44 | */ 45 | private String workDescription; 46 | 47 | public Resume() { 48 | System.out.println("Resume类的无参构造函数"); 49 | } 50 | 51 | public String getName() { 52 | return name; 53 | } 54 | 55 | public void setName(String name) { 56 | this.name = name; 57 | } 58 | 59 | public Date getBirthday() { 60 | return birthday; 61 | } 62 | 63 | public void setBirthday(Date birthday) { 64 | this.birthday = birthday; 65 | } 66 | 67 | public String getSex() { 68 | return sex; 69 | } 70 | 71 | public void setSex(String sex) { 72 | this.sex = sex; 73 | } 74 | 75 | public String getSchool() { 76 | return school; 77 | } 78 | 79 | public void setSchool(String school) { 80 | this.school = school; 81 | } 82 | 83 | public String getSocialWorkAge() { 84 | return socialWorkAge; 85 | } 86 | 87 | public void setSocialWorkAge(String socialWorkAge) { 88 | this.socialWorkAge = socialWorkAge; 89 | } 90 | 91 | public String getCompany() { 92 | return company; 93 | } 94 | 95 | public void setCompany(String company) { 96 | this.company = company; 97 | } 98 | 99 | public String getWorkDescription() { 100 | return workDescription; 101 | } 102 | 103 | public void setWorkDescription(String workDescription) { 104 | this.workDescription = workDescription; 105 | } 106 | 107 | @Override 108 | protected Object clone() throws CloneNotSupportedException { 109 | System.out.println("开始克隆简历"); 110 | Resume resume = (Resume) super.clone(); 111 | 112 | // 深克隆 113 | resume.birthday = (Date) resume.birthday.clone(); 114 | return resume; 115 | } 116 | 117 | public void display() { 118 | System.out.println(String.format("姓名: %s", name)); 119 | System.out.println(String.format("生日: %s, 性别: %s, 毕业院校: %s, 工龄: %s", birthday, sex, school, socialWorkAge)); 120 | System.out.println(String.format("公司: %s, 工作描述: %s", company, workDescription)); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /106-Prototype/src/main/java/com/zhh/v3/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * @author zhh 7 | * @description 测试类 8 | * @date 2020-02-13 00:20 9 | */ 10 | public class Test { 11 | 12 | public static void main(String[] args) throws Exception { 13 | // 原版简历 14 | Resume resumeA = new Resume(); 15 | resumeA.setName("海豪"); 16 | resumeA.setBirthday(new Date(94, 0, 1)); 17 | resumeA.setSex("男"); 18 | resumeA.setSchool("XXXX大学"); 19 | resumeA.setSocialWorkAge("1"); 20 | resumeA.setCompany("A科技有限公司"); 21 | resumeA.setWorkDescription("在A公司的工作描述"); 22 | 23 | // 拷贝简历 24 | Resume resumeB = (Resume) resumeA.clone(); 25 | 26 | System.out.println("=====原版简历====="); 27 | resumeA.display(); 28 | System.out.println("=====拷贝简历====="); 29 | resumeB.display(); 30 | 31 | resumeA.getBirthday().setDate(5); 32 | 33 | System.out.println(); 34 | System.out.println("=====修改后原版简历====="); 35 | resumeA.display(); 36 | System.out.println("=====修改后拷贝简历====="); 37 | resumeB.display(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /107-Facade/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 107-Facade 14 | 结构型-外观模式 15 | 16 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v1/OutpatientService.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 门诊类 6 | * @date 2020-02-13 16:18 7 | */ 8 | public class OutpatientService { 9 | 10 | public void operate(Patient patient) { 11 | // 模拟就诊 12 | System.out.println("病人: " + patient.getName() + " 进行就诊"); 13 | System.out.println("就诊完毕, 需要配药"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v1/Patient.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 病人类 6 | * @date 2020-02-13 16:21 7 | */ 8 | public class Patient { 9 | 10 | /** 11 | * 姓名 12 | */ 13 | private String name; 14 | 15 | public Patient(String name) { 16 | this.name = name; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v1/RegisterService.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 挂号类 6 | * @date 2020-02-13 16:15 7 | */ 8 | public class RegisterService { 9 | 10 | public void operate(Patient patient) { 11 | // 模拟挂号 12 | System.out.println("病人: " + patient.getName() + " 进行挂号"); 13 | System.out.println("挂号完毕"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v1/SettleAccountService.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 划价类 6 | * @date 2020-02-13 16:19 7 | */ 8 | public class SettleAccountService { 9 | 10 | public void operate(Patient patient) { 11 | // 模拟付款 12 | System.out.println("病人: " + patient.getName() + " 进行付款"); 13 | System.out.println("付款完成, 前往取药"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v1/TakeMedicineService.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 取药类 6 | * @date 2020-02-13 16:23 7 | */ 8 | public class TakeMedicineService { 9 | 10 | public void operate(Patient patient) { 11 | // 模拟取药 12 | System.out.println("病人: " + patient.getName() + " 进行取药"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-13 16:24 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Patient patient = new Patient("海豪"); 12 | System.out.println("病人进入医院"); 13 | new RegisterService().operate(patient); 14 | new OutpatientService().operate(patient); 15 | new SettleAccountService().operate(patient); 16 | new TakeMedicineService().operate(patient); 17 | System.out.println("病人离开医院"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v2/OutpatientService.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 门诊类 6 | * @date 2020-02-13 16:18 7 | */ 8 | public class OutpatientService { 9 | 10 | public void operate(Patient patient) { 11 | // 模拟就诊 12 | System.out.println("病人: " + patient.getName() + " 进行就诊"); 13 | System.out.println("就诊完毕, 需要配药"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v2/Patient.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 病人类 6 | * @date 2020-02-13 16:21 7 | */ 8 | public class Patient { 9 | 10 | /** 11 | * 姓名 12 | */ 13 | private String name; 14 | 15 | public Patient(String name) { 16 | this.name = name; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v2/Receptionist.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 接待员类 6 | * @date 2020-02-13 16:46 7 | */ 8 | public class Receptionist { 9 | 10 | // 模拟注入 11 | private RegisterService registerService = new RegisterService(); 12 | private OutpatientService outpatientService = new OutpatientService(); 13 | private SettleAccountService settleAccountService = new SettleAccountService(); 14 | private TakeMedicineService takeMedicineService = new TakeMedicineService(); 15 | 16 | 17 | /** 18 | * 接待病人 19 | * @param patient 20 | */ 21 | public void receivePatients(Patient patient) { 22 | System.out.println("接待员开始接待病人"); 23 | registerService.operate(patient); 24 | outpatientService.operate(patient); 25 | settleAccountService.operate(patient); 26 | takeMedicineService.operate(patient); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v2/RegisterService.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 挂号类 6 | * @date 2020-02-13 16:15 7 | */ 8 | public class RegisterService { 9 | 10 | public void operate(Patient patient) { 11 | // 模拟挂号 12 | System.out.println("病人: " + patient.getName() + " 进行挂号"); 13 | System.out.println("挂号完毕"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v2/SettleAccountService.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 划价类 6 | * @date 2020-02-13 16:19 7 | */ 8 | public class SettleAccountService { 9 | 10 | public void operate(Patient patient) { 11 | // 模拟付款 12 | System.out.println("病人: " + patient.getName() + " 进行付款"); 13 | System.out.println("付款完成, 前往取药"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v2/TakeMedicineService.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 取药类 6 | * @date 2020-02-13 16:23 7 | */ 8 | public class TakeMedicineService { 9 | 10 | public void operate(Patient patient) { 11 | // 模拟取药 12 | System.out.println("病人: " + patient.getName() + " 进行取药"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /107-Facade/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-13 16:24 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Patient patient = new Patient("海豪"); 12 | Receptionist receptionist = new Receptionist(); 13 | 14 | System.out.println("病人进入医院"); 15 | receptionist.receivePatients(patient); 16 | System.out.println("病人离开医院"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /108-Decorator/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 108-Decorator 14 | 结构型-装饰器模式 15 | 16 | -------------------------------------------------------------------------------- /108-Decorator/src/main/java/com/zhh/v1/AbstractDecorator.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 抽象装饰者类 6 | * @date 2020-02-13 23:37 7 | */ 8 | public abstract class AbstractDecorator extends AbstractMilkyTea { 9 | 10 | private AbstractMilkyTea abstractMilkyTea; 11 | 12 | public AbstractDecorator(AbstractMilkyTea abstractMilkyTea) { 13 | this.abstractMilkyTea = abstractMilkyTea; 14 | } 15 | 16 | @Override 17 | protected String desc() { 18 | return this.abstractMilkyTea.desc(); 19 | } 20 | 21 | @Override 22 | protected int price() { 23 | return this.abstractMilkyTea.price(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /108-Decorator/src/main/java/com/zhh/v1/AbstractMilkyTea.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 抽象奶茶类 6 | * @date 2020-02-13 23:30 7 | */ 8 | public abstract class AbstractMilkyTea { 9 | 10 | /** 11 | * 描述 12 | */ 13 | protected abstract String desc(); 14 | 15 | /** 16 | * 价格 17 | */ 18 | protected abstract int price(); 19 | } 20 | -------------------------------------------------------------------------------- /108-Decorator/src/main/java/com/zhh/v1/MilkyTea.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 具体奶茶类 6 | * @date 2020-02-13 23:35 7 | */ 8 | public class MilkyTea extends AbstractMilkyTea { 9 | 10 | @Override 11 | protected String desc() { 12 | return "奶茶"; 13 | } 14 | 15 | @Override 16 | protected int price() { 17 | return 7; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /108-Decorator/src/main/java/com/zhh/v1/PearlDecorator.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 珍珠装饰者 6 | * @date 2020-02-13 23:56 7 | */ 8 | public class PearlDecorator extends AbstractDecorator { 9 | 10 | public PearlDecorator(AbstractMilkyTea abstractMilkyTea) { 11 | super(abstractMilkyTea); 12 | } 13 | 14 | @Override 15 | protected String desc() { 16 | return super.desc() + " 加珍珠"; 17 | } 18 | 19 | @Override 20 | protected int price() { 21 | return super.price() + 2; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /108-Decorator/src/main/java/com/zhh/v1/RedBeanDecorator.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 红豆装饰者 6 | * @date 2020-02-14 00:14 7 | */ 8 | public class RedBeanDecorator extends AbstractDecorator { 9 | 10 | public RedBeanDecorator(AbstractMilkyTea abstractMilkyTea) { 11 | super(abstractMilkyTea); 12 | } 13 | 14 | @Override 15 | protected String desc() { 16 | return super.desc() + " 加红豆"; 17 | } 18 | 19 | @Override 20 | protected int price() { 21 | return super.price() + 2; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /108-Decorator/src/main/java/com/zhh/v1/SugarDecorator.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 糖装饰者 6 | * @date 2020-02-13 23:58 7 | */ 8 | public class SugarDecorator extends AbstractDecorator { 9 | 10 | public SugarDecorator(AbstractMilkyTea abstractMilkyTea) { 11 | super(abstractMilkyTea); 12 | } 13 | 14 | @Override 15 | protected String desc() { 16 | return super.desc() + " 加糖"; 17 | } 18 | 19 | @Override 20 | protected int price() { 21 | return super.price() + 1; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /108-Decorator/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-14 00:21 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | AbstractMilkyTea abstractMilkyTea = new MilkyTea(); 12 | abstractMilkyTea = new SugarDecorator(abstractMilkyTea); 13 | abstractMilkyTea = new PearlDecorator(abstractMilkyTea); 14 | System.out.println(abstractMilkyTea.desc() + " 的销售价格是:" + abstractMilkyTea.price()); 15 | 16 | abstractMilkyTea = new MilkyTea(); 17 | abstractMilkyTea = new RedBeanDecorator(abstractMilkyTea); 18 | abstractMilkyTea = new PearlDecorator(abstractMilkyTea); 19 | System.out.println(abstractMilkyTea.desc() + " 的销售价格是:" + abstractMilkyTea.price()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /109-Adapter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 109-Adapter 14 | 结构型-适配器模式 15 | 16 | -------------------------------------------------------------------------------- /109-Adapter/src/main/java/com/zhh/v1/SDInterface.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description SD卡接口 6 | * @date 2020-02-14 16:36 7 | */ 8 | public interface SDInterface { 9 | 10 | void sdRead(); 11 | } 12 | -------------------------------------------------------------------------------- /109-Adapter/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-14 16:59 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | SDInterface sdInterface = new TypeCDockStation(); 12 | sdInterface.sdRead(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /109-Adapter/src/main/java/com/zhh/v1/TypeCDockStation.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description Type-C扩展坞 6 | * @date 2020-02-14 16:43 7 | */ 8 | public class TypeCDockStation extends TypeCInterface implements SDInterface { 9 | 10 | public void sdRead() { 11 | System.out.println("SD卡接口读取..."); 12 | System.out.println("扩展坞转换"); 13 | super.typeCRead(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /109-Adapter/src/main/java/com/zhh/v1/TypeCInterface.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description TypeC接口类 6 | * @date 2020-02-14 16:33 7 | */ 8 | public class TypeCInterface { 9 | 10 | /** 11 | * type-C读取 12 | */ 13 | public void typeCRead() { 14 | System.out.println("Type-C接口读取..."); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /109-Adapter/src/main/java/com/zhh/v2/SDInterface.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description SD卡接口 6 | * @date 2020-02-14 16:36 7 | */ 8 | public interface SDInterface { 9 | 10 | void sdRead(); 11 | } 12 | -------------------------------------------------------------------------------- /109-Adapter/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-14 16:59 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | TypeCInterface typeCInterface = new TypeCInterface(); 12 | SDInterface sdInterface = new TypeCDockStation(typeCInterface); 13 | sdInterface.sdRead(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /109-Adapter/src/main/java/com/zhh/v2/TypeCDockStation.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 6 | * @date 2020-02-14 17:23 7 | */ 8 | public class TypeCDockStation implements SDInterface { 9 | 10 | private TypeCInterface typeCInterface; 11 | 12 | public TypeCDockStation(TypeCInterface typeCInterface) { 13 | this.typeCInterface = typeCInterface; 14 | } 15 | 16 | public void sdRead() { 17 | System.out.println("SD卡接口读取..."); 18 | System.out.println("扩展坞转换"); 19 | typeCInterface.typeCRead(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /109-Adapter/src/main/java/com/zhh/v2/TypeCInterface.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description TypeC接口类 6 | * @date 2020-02-14 16:33 7 | */ 8 | public class TypeCInterface { 9 | 10 | /** 11 | * type-C读取 12 | */ 13 | public void typeCRead() { 14 | System.out.println("Type-C接口读取..."); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /110-Flyweight/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 110-Flyweight 14 | 结构型-享元模式 15 | 16 | -------------------------------------------------------------------------------- /110-Flyweight/src/main/java/com/zhh/v1/Clothes.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 衣服类 6 | * @date 2020-02-16 22:36 7 | */ 8 | public interface Clothes { 9 | 10 | /** 11 | * 衣服信息 12 | */ 13 | void info(); 14 | } 15 | -------------------------------------------------------------------------------- /110-Flyweight/src/main/java/com/zhh/v1/ClothesFactory.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * @author zhh 8 | * @description 衣服工厂类 9 | * @date 2020-02-16 22:48 10 | */ 11 | public class ClothesFactory { 12 | 13 | private static final Map CLOTHES_MAP = new HashMap(); 14 | 15 | /** 16 | * 获取衬衣 17 | * @param brand 品牌 18 | * @return 19 | */ 20 | public static Clothes getShirt(String brand) { 21 | Shirt shirt = (Shirt) CLOTHES_MAP.get(brand); 22 | 23 | if (shirt == null) { 24 | shirt = new Shirt(brand); 25 | CLOTHES_MAP.put(brand, shirt); 26 | System.out.println("创建新的品牌衬衣, 品牌为: " + brand); 27 | } 28 | 29 | return shirt; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /110-Flyweight/src/main/java/com/zhh/v1/Shirt.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 衬衣类 6 | * @date 2020-02-16 22:37 7 | */ 8 | public class Shirt implements Clothes { 9 | 10 | /** 11 | * 品牌 12 | */ 13 | private String brand; 14 | 15 | /** 16 | * 大小 17 | */ 18 | private String size; 19 | 20 | /** 21 | * 成分 22 | */ 23 | private String component; 24 | 25 | /** 26 | * 价格 27 | */ 28 | private String price; 29 | 30 | public Shirt(String brand) { 31 | this.brand = brand; 32 | } 33 | 34 | public void setSize(String size) { 35 | this.size = size; 36 | } 37 | 38 | public void setComponent(String component) { 39 | this.component = component; 40 | } 41 | 42 | public void setPrice(String price) { 43 | this.price = price; 44 | } 45 | 46 | public void info() { 47 | System.out.println(String.format("当前衬衣信息: {品牌: %s, 大小: %s, 成分: %s, 价格: %s}", brand, size, component, price)); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /110-Flyweight/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.Random; 4 | 5 | /** 6 | * @author zhh 7 | * @description 测试类 8 | * @date 2020-02-16 23:04 9 | */ 10 | public class Test { 11 | 12 | private static final String[] BRAND = {"YOUNGOR雅戈尔", "FIRS杉杉", "ROMON罗蒙", "Hodo红豆"}; 13 | 14 | private static final String[] SIZE = {"S", "M", "L", "XL", "XXL"}; 15 | 16 | private static final String[] COMPONENT = {"棉", "涤纶", "真丝"}; 17 | 18 | private static final String[] PRICE = { "¥99", "¥129", "¥149", "¥199" }; 19 | 20 | /** 21 | * 随机模拟 22 | * @param strs 随机模拟数组 23 | * @return 24 | */ 25 | public static String getRandom(String[] strs) { 26 | return strs[new Random().nextInt(strs.length)]; 27 | } 28 | 29 | public static void main(String[] args) { 30 | for (int i = 0; i < 10; i++) { 31 | Shirt shirt = (Shirt) ClothesFactory.getShirt(getRandom(BRAND)); 32 | shirt.setSize(getRandom(SIZE)); 33 | shirt.setComponent(getRandom(COMPONENT)); 34 | shirt.setPrice(getRandom(PRICE)); 35 | shirt.info(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /111-Composite/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 111-Composite 14 | 结构型-组合模式 15 | 16 | -------------------------------------------------------------------------------- /111-Composite/src/main/java/com/zhh/v1/MenuCatalog.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author zhh 8 | * @description 菜单目录类 9 | * @date 2020-02-22 15:03 10 | */ 11 | public class MenuCatalog extends MenuComponent { 12 | 13 | /** 14 | * 菜单目录名称 15 | */ 16 | private String name; 17 | 18 | /** 19 | * 菜单目录层级, 方便区分 20 | */ 21 | private Integer level; 22 | 23 | /** 24 | * 子菜单项列表 25 | */ 26 | private List menuItems = new ArrayList(); 27 | 28 | public MenuCatalog(String name, Integer level) { 29 | this.name = name; 30 | this.level = level; 31 | } 32 | 33 | @Override 34 | public void add(MenuComponent menuComponent) { 35 | menuItems.add(menuComponent); 36 | } 37 | 38 | @Override 39 | public void remove(MenuComponent menuComponent) { 40 | menuItems.remove(menuComponent); 41 | } 42 | 43 | @Override 44 | public String getName(MenuComponent menuComponent) { 45 | return this.name; 46 | } 47 | 48 | @Override 49 | public void print() { 50 | System.out.println(this.name); 51 | for (MenuComponent menuComponent : menuItems) { 52 | if (this.level != null) { 53 | for (int i = 0; i < this.level; i++) { 54 | System.out.print("*"); 55 | } 56 | } 57 | menuComponent.print(); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /111-Composite/src/main/java/com/zhh/v1/MenuComponent.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 菜单组件类 6 | * @date 2020-02-22 11:13 7 | */ 8 | public abstract class MenuComponent { 9 | 10 | public void add(MenuComponent menuComponent) { 11 | throw new UnsupportedOperationException("不支持添加操作"); 12 | } 13 | 14 | public void remove(MenuComponent menuComponent) { 15 | throw new UnsupportedOperationException("不支持删除操作"); 16 | } 17 | 18 | public String getUrl(MenuComponent menuComponent) { 19 | throw new UnsupportedOperationException("不支持获取地址操作"); 20 | } 21 | 22 | public abstract String getName(MenuComponent menuComponent); 23 | 24 | public abstract void print(); 25 | } 26 | -------------------------------------------------------------------------------- /111-Composite/src/main/java/com/zhh/v1/MenuItem.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 菜单项类 6 | * @date 2020-02-22 11:44 7 | */ 8 | public class MenuItem extends MenuComponent { 9 | 10 | /** 11 | * 菜单项名称 12 | */ 13 | private String name; 14 | 15 | /** 16 | * 页面访问地址 17 | */ 18 | private String url; 19 | 20 | public MenuItem(String name, String url) { 21 | this.name = name; 22 | this.url = url; 23 | } 24 | 25 | @Override 26 | public String getName(MenuComponent menuComponent) { 27 | return this.name; 28 | } 29 | 30 | @Override 31 | public String getUrl(MenuComponent menuComponent) { 32 | return this.url; 33 | } 34 | 35 | @Override 36 | public void print() { 37 | System.out.println(String.format("%s, 菜单项的页面访问地址是: %s", name, url)); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /111-Composite/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-22 15:15 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | String site = "www.zhaohaihao.com"; 12 | // 首页 13 | MenuComponent index = new MenuItem("首页", site); 14 | 15 | // 编程手册 16 | MenuComponent programmingManual = new MenuCatalog("编程手册", 2); 17 | programmingManual.add(new MenuItem("设计模式", site + "/category/design-patterns")); 18 | programmingManual.add(new MenuItem("Spring Cloud", site + "/category/spring-cloud")); 19 | 20 | // 主题推荐 21 | MenuComponent topic = new MenuItem("主题推荐", site + "/topic"); 22 | 23 | // 网站导航栏, 顶级目录, 目录层级用1标记 24 | MenuComponent main = new MenuCatalog("网站导航目录", 1); 25 | main.add(index); 26 | main.add(programmingManual); 27 | main.add(topic); 28 | 29 | main.print(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /111-Composite/src/main/java/com/zhh/v2/MenuCatalog.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author zhh 8 | * @description 菜单目录类 9 | * @date 2020-02-22 15:03 10 | */ 11 | public class MenuCatalog extends MenuComponent { 12 | 13 | /** 14 | * 菜单目录名称 15 | */ 16 | private String name; 17 | 18 | /** 19 | * 菜单目录层级, 方便区分 20 | */ 21 | private Integer level; 22 | 23 | /** 24 | * 子菜单项列表 25 | */ 26 | private List menuItems = new ArrayList(); 27 | 28 | public MenuCatalog(String name, Integer level) { 29 | this.name = name; 30 | this.level = level; 31 | } 32 | 33 | public void add(MenuComponent menuComponent) { 34 | menuItems.add(menuComponent); 35 | } 36 | 37 | public void remove(MenuComponent menuComponent) { 38 | menuItems.remove(menuComponent); 39 | } 40 | 41 | @Override 42 | public String getName(MenuComponent menuComponent) { 43 | return this.name; 44 | } 45 | 46 | @Override 47 | public void print() { 48 | System.out.println(this.name); 49 | for (MenuComponent menuComponent : menuItems) { 50 | if (this.level != null) { 51 | for (int i = 0; i < this.level; i++) { 52 | System.out.print("*"); 53 | } 54 | } 55 | menuComponent.print(); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /111-Composite/src/main/java/com/zhh/v2/MenuComponent.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 菜单组件类 6 | * @date 2020-02-22 11:13 7 | */ 8 | public abstract class MenuComponent { 9 | 10 | public abstract String getName(MenuComponent menuComponent); 11 | 12 | public abstract void print(); 13 | } 14 | -------------------------------------------------------------------------------- /111-Composite/src/main/java/com/zhh/v2/MenuItem.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 菜单项类 6 | * @date 2020-02-22 11:44 7 | */ 8 | public class MenuItem extends MenuComponent { 9 | 10 | /** 11 | * 菜单项名称 12 | */ 13 | private String name; 14 | 15 | /** 16 | * 页面访问地址 17 | */ 18 | private String url; 19 | 20 | public MenuItem(String name, String url) { 21 | this.name = name; 22 | this.url = url; 23 | } 24 | 25 | public String getUrl(MenuComponent menuComponent) { 26 | return this.url; 27 | } 28 | 29 | @Override 30 | public String getName(MenuComponent menuComponent) { 31 | return this.name; 32 | } 33 | 34 | @Override 35 | public void print() { 36 | System.out.println(String.format("%s, 菜单项的页面访问地址是: %s", name, url)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /111-Composite/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-22 15:15 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | String site = "www.zhaohaihao.com"; 12 | // 首页 13 | MenuComponent index = new MenuItem("首页", site); 14 | 15 | // 编程手册 16 | MenuCatalog programmingManual = new MenuCatalog("编程手册", 2); 17 | programmingManual.add(new MenuItem("设计模式", site + "/category/design-patterns")); 18 | programmingManual.add(new MenuItem("Spring Cloud", site + "/category/spring-cloud")); 19 | 20 | // 主题推荐 21 | MenuComponent topic = new MenuItem("主题推荐", site + "/topic"); 22 | 23 | // 网站导航栏, 顶级目录, 目录层级用1标记 24 | MenuCatalog main = new MenuCatalog("网站导航目录", 1); 25 | main.add(index); 26 | main.add(programmingManual); 27 | main.add(topic); 28 | 29 | main.print(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /112-Bridge/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 112-Bridge 14 | 结构型-桥接模式 15 | 16 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v1/BallPen.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 圆珠笔类 6 | * @date 2020-02-26 14:56 7 | */ 8 | public abstract class BallPen extends Pen { 9 | 10 | public BallPen() { 11 | super("圆珠笔类"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v1/CircleWithBallPen.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 圆珠笔画圆类 6 | * @date 2020-02-26 14:57 7 | */ 8 | public class CircleWithBallPen extends BallPen { 9 | 10 | @Override 11 | public void draw() { 12 | System.out.println(getName() + "画圆"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v1/CircleWithPencil.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 铅笔画圆类 6 | * @date 2020-02-26 14:56 7 | */ 8 | public class CircleWithPencil extends Pencil { 9 | 10 | @Override 11 | public void draw() { 12 | System.out.println(getName() + "画圆"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v1/Pen.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 笔类 6 | * @date 2020-02-26 14:52 7 | */ 8 | public abstract class Pen { 9 | 10 | /** 11 | * 笔的名称 12 | */ 13 | private String name; 14 | 15 | public Pen(String name) { 16 | this.name = name; 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 abstract void draw(); 28 | } 29 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v1/Pencil.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 铅笔类 6 | * @date 2020-02-26 14:55 7 | */ 8 | public abstract class Pencil extends Pen { 9 | 10 | public Pencil() { 11 | super("铅笔"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v1/SquareWithBallPen.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 圆珠笔画正方形类 6 | * @date 2020-02-26 14:59 7 | */ 8 | public class SquareWithBallPen extends BallPen { 9 | 10 | @Override 11 | public void draw() { 12 | System.out.println(getName() + "画正方形"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v1/SquareWithPencil.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 铅笔画正方形类 6 | * @date 2020-02-26 14:58 7 | */ 8 | public class SquareWithPencil extends Pencil { 9 | 10 | @Override 11 | public void draw() { 12 | System.out.println(getName() + "画正方形"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-26 15:00 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Pen circleWithBallPen = new CircleWithBallPen(); 12 | circleWithBallPen.draw(); 13 | 14 | Pen circleWithPencil = new CircleWithPencil(); 15 | circleWithPencil.draw(); 16 | 17 | Pen squareWithBallPen = new SquareWithBallPen(); 18 | squareWithBallPen.draw(); 19 | 20 | Pen squareWithPencil = new SquareWithPencil(); 21 | squareWithPencil.draw(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v2/BallPen.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 圆珠笔类 6 | * @date 2020-02-26 14:56 7 | */ 8 | public class BallPen extends Pen { 9 | 10 | public BallPen(Shape shape) { 11 | super(shape); 12 | } 13 | 14 | @Override 15 | public void draw() { 16 | System.out.println("圆珠笔" + super.shape.draw()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v2/Circle.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 圆形类 6 | * @date 2020-02-26 14:57 7 | */ 8 | public class Circle implements Shape { 9 | 10 | public String draw() { 11 | return "画圆"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v2/Pen.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 笔类 6 | * @date 2020-02-26 14:52 7 | */ 8 | public abstract class Pen { 9 | 10 | /** 11 | * 图形 12 | */ 13 | protected Shape shape; 14 | 15 | public Pen(Shape shape) { 16 | this.shape = shape; 17 | } 18 | 19 | public abstract void draw(); 20 | } 21 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v2/Pencil.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 铅笔类 6 | * @date 2020-02-26 14:55 7 | */ 8 | public class Pencil extends Pen { 9 | 10 | public Pencil(Shape shape) { 11 | super(shape); 12 | } 13 | 14 | @Override 15 | public void draw() { 16 | System.out.println("铅笔" + super.shape.draw()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v2/Shape.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 图形类 6 | * @date 2020-02-26 14:57 7 | */ 8 | public interface Shape { 9 | 10 | String draw(); 11 | } 12 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v2/Square.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 正方形类 6 | * @date 2020-02-26 14:57 7 | */ 8 | public class Square implements Shape { 9 | 10 | public String draw() { 11 | return "画正方形"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /112-Bridge/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-26 15:00 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Pen ballPen1 = new BallPen(new Circle()); 12 | ballPen1.draw(); 13 | 14 | Pen ballPen2 = new BallPen(new Square()); 15 | ballPen2.draw(); 16 | 17 | Pencil pencil1 = new Pencil(new Circle()); 18 | pencil1.draw(); 19 | 20 | Pencil pencil2 = new Pencil(new Square()); 21 | pencil2.draw(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /113-Proxy/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 113-Proxy 14 | 结构型-代理模式 15 | 16 | 17 | 18 | cglib 19 | cglib 20 | 3.3.0 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v1/Station.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 火车站(目标对象) 6 | * @date 2020-02-26 21:44 7 | */ 8 | public class Station { 9 | 10 | /** 11 | * 姓名 12 | */ 13 | private String username; 14 | 15 | /** 16 | * 始发站 17 | */ 18 | private String start; 19 | 20 | /** 21 | * 终点站 22 | */ 23 | private String end; 24 | 25 | public Station(String username, String start, String end) { 26 | this.username = username; 27 | this.start = start; 28 | this.end = end; 29 | } 30 | 31 | public String getUsername() { 32 | return username; 33 | } 34 | 35 | public void setUsername(String username) { 36 | this.username = username; 37 | } 38 | 39 | public String getStart() { 40 | return start; 41 | } 42 | 43 | public void setStart(String start) { 44 | this.start = start; 45 | } 46 | 47 | public String getEnd() { 48 | return end; 49 | } 50 | 51 | public void setEnd(String end) { 52 | this.end = end; 53 | } 54 | 55 | public void buy() { 56 | System.out.println(String.format("%s购买了从%s至%s的火车票", username, start, end)); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-26 21:54 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Station station = new Website12306("海豪", "诸暨", "杭州"); 12 | station.buy(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v1/Website12306.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 12306网站类(代理对象) 6 | * @date 2020-02-26 21:47 7 | */ 8 | public class Website12306 extends Station { 9 | 10 | public Website12306(String username, String start, String end) { 11 | super(username, start, end); 12 | } 13 | 14 | @Override 15 | public void buy() { 16 | // 模拟日志打印 17 | System.out.println(String.format("【打印日志】 %s在12306平台上购买从%s至%s的火车票", getUsername(), getStart(), getEnd())); 18 | super.buy(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v2/Station.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 抽象接口 6 | * @date 2020-02-26 21:44 7 | */ 8 | public interface Station { 9 | 10 | void buy(); 11 | } 12 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-26 21:54 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Station station = new Website12306("海豪", "诸暨", "杭州"); 12 | station.buy(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v2/Website12306.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 12306网站类(代理对象) 6 | * @date 2020-02-26 21:47 7 | */ 8 | public class Website12306 implements Station { 9 | 10 | /** 11 | * 姓名 12 | */ 13 | private String username; 14 | 15 | /** 16 | * 始发站 17 | */ 18 | private String start; 19 | 20 | /** 21 | * 终点站 22 | */ 23 | private String end; 24 | 25 | private Station station; 26 | 27 | public Website12306(String username, String start, String end) { 28 | this.username = username; 29 | this.start = start; 30 | this.end = end; 31 | } 32 | 33 | public void buy() { 34 | // 模拟日志打印 35 | System.out.println(String.format("【打印日志】 %s在12306平台上购买从%s至%s的火车票", username, start, end)); 36 | if (station == null) { 37 | station = new ZhuJiStation(username, start, end); 38 | } 39 | station.buy(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v2/ZhuJiStation.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 诸暨火车站(目标对象) 6 | * @date 2020-02-26 22:10 7 | */ 8 | public class ZhuJiStation implements Station { 9 | 10 | /** 11 | * 姓名 12 | */ 13 | private String username; 14 | 15 | /** 16 | * 始发站 17 | */ 18 | private String start; 19 | 20 | /** 21 | * 终点站 22 | */ 23 | private String end; 24 | 25 | public ZhuJiStation(String username, String start, String end) { 26 | this.username = username; 27 | this.start = start; 28 | this.end = end; 29 | } 30 | 31 | public String getUsername() { 32 | return username; 33 | } 34 | 35 | public void setUsername(String username) { 36 | this.username = username; 37 | } 38 | 39 | public String getStart() { 40 | return start; 41 | } 42 | 43 | public void setStart(String start) { 44 | this.start = start; 45 | } 46 | 47 | public String getEnd() { 48 | return end; 49 | } 50 | 51 | public void setEnd(String end) { 52 | this.end = end; 53 | } 54 | 55 | public void buy() { 56 | System.out.println(String.format("%s购买了从%s至%s的火车票", username, start, end)); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v3/Station.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 抽象接口 6 | * @date 2020-02-26 21:44 7 | */ 8 | public interface Station { 9 | 10 | void buy(); 11 | } 12 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v3/StationDynamicProxy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | import java.lang.reflect.InvocationHandler; 4 | import java.lang.reflect.Method; 5 | import java.lang.reflect.Proxy; 6 | 7 | /** 8 | * @author zhh 9 | * @description 动态代理类 10 | * @date 2020-02-26 22:32 11 | */ 12 | public class StationDynamicProxy implements InvocationHandler { 13 | 14 | private Object target; 15 | 16 | public StationDynamicProxy(Object target) { 17 | this.target = target; 18 | } 19 | 20 | public Object bind() { 21 | Class clazz = target.getClass(); 22 | return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this); 23 | } 24 | 25 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 26 | // 模拟日志打印 27 | System.out.println("【日志打印】真实操作之前进行日志打印"); 28 | return method.invoke(target, args); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v3/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-26 21:54 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Station station = (Station) new StationDynamicProxy(new ZhuJiStation("海豪", "诸暨", "杭州")).bind(); 12 | station.buy(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v3/ZhuJiStation.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v3; 2 | 3 | /** 4 | * @author zhh 5 | * @description 诸暨火车站(目标对象) 6 | * @date 2020-02-26 22:10 7 | */ 8 | public class ZhuJiStation implements Station { 9 | 10 | /** 11 | * 姓名 12 | */ 13 | private String username; 14 | 15 | /** 16 | * 始发站 17 | */ 18 | private String start; 19 | 20 | /** 21 | * 终点站 22 | */ 23 | private String end; 24 | 25 | public ZhuJiStation(String username, String start, String end) { 26 | this.username = username; 27 | this.start = start; 28 | this.end = end; 29 | } 30 | 31 | public String getUsername() { 32 | return username; 33 | } 34 | 35 | public void setUsername(String username) { 36 | this.username = username; 37 | } 38 | 39 | public String getStart() { 40 | return start; 41 | } 42 | 43 | public void setStart(String start) { 44 | this.start = start; 45 | } 46 | 47 | public String getEnd() { 48 | return end; 49 | } 50 | 51 | public void setEnd(String end) { 52 | this.end = end; 53 | } 54 | 55 | public void buy() { 56 | System.out.println(String.format("%s购买了从%s至%s的火车票", username, start, end)); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v4/StationDynamicProxy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | import net.sf.cglib.proxy.MethodInterceptor; 4 | import net.sf.cglib.proxy.MethodProxy; 5 | 6 | import java.lang.reflect.Method; 7 | 8 | /** 9 | * @author zhh 10 | * @description 动态代理类 11 | * @date 2020-02-26 22:32 12 | */ 13 | public class StationDynamicProxy implements MethodInterceptor { 14 | 15 | public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { 16 | // 模拟日志打印 17 | System.out.println("【日志打印】真实操作之前进行日志打印"); 18 | return methodProxy.invokeSuper(o, objects); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v4/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | import net.sf.cglib.proxy.Enhancer; 4 | 5 | /** 6 | * @author zhh 7 | * @description 测试类 8 | * @date 2020-02-26 21:54 9 | */ 10 | public class Test { 11 | 12 | public static void main(String[] args) { 13 | Enhancer enhancer = new Enhancer(); 14 | enhancer.setSuperclass(ZhuJiStation.class); 15 | enhancer.setCallback(new StationDynamicProxy()); 16 | ZhuJiStation proxy = (ZhuJiStation) enhancer.create(); 17 | proxy.setUsername("海豪"); 18 | proxy.setStart("诸暨"); 19 | proxy.setEnd("杭州"); 20 | proxy.buy(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /113-Proxy/src/main/java/com/zhh/v4/ZhuJiStation.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v4; 2 | 3 | /** 4 | * @author zhh 5 | * @description 诸暨火车站(目标对象) 6 | * @date 2020-02-26 22:10 7 | */ 8 | public class ZhuJiStation { 9 | 10 | /** 11 | * 姓名 12 | */ 13 | private String username; 14 | 15 | /** 16 | * 始发站 17 | */ 18 | private String start; 19 | 20 | /** 21 | * 终点站 22 | */ 23 | private String end; 24 | 25 | public String getUsername() { 26 | return username; 27 | } 28 | 29 | public void setUsername(String username) { 30 | this.username = username; 31 | } 32 | 33 | public String getStart() { 34 | return start; 35 | } 36 | 37 | public void setStart(String start) { 38 | this.start = start; 39 | } 40 | 41 | public String getEnd() { 42 | return end; 43 | } 44 | 45 | public void setEnd(String end) { 46 | this.end = end; 47 | } 48 | 49 | public void buy() { 50 | System.out.println(String.format("%s购买了从%s至%s的火车票", username, start, end)); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /114-Template-Method/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 114-Template-Method 14 | 行为型-模板方法模式 15 | 16 | -------------------------------------------------------------------------------- /114-Template-Method/src/main/java/com/zhh/v1/AbstractCook.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 抽象模板类 6 | * @date 2020-02-27 15:47 7 | */ 8 | public abstract class AbstractCook { 9 | 10 | /** 11 | * 做饭的整体步骤 12 | */ 13 | protected final void cook() { 14 | this.pourOil(); 15 | this.addEgg(); 16 | this.addTomato(); 17 | this.pourSeasoning(); 18 | this.stirFry(); 19 | if (needChoppedGreenOnion()) { 20 | this.addChoppedGreenOnion(); 21 | } 22 | } 23 | 24 | /** 25 | * 倒油 26 | */ 27 | final void pourOil() { 28 | System.out.println("倒入食用油"); 29 | } 30 | 31 | /** 32 | * 放鸡蛋 33 | */ 34 | final void addEgg() { 35 | System.out.println("放入鸡蛋"); 36 | } 37 | 38 | /** 39 | * 放番茄 40 | */ 41 | final void addTomato() { 42 | System.out.println("放入番茄"); 43 | } 44 | 45 | /** 46 | * 翻炒 47 | */ 48 | final void stirFry() { 49 | System.out.println("快速翻炒"); 50 | } 51 | 52 | /** 53 | * 放葱花 54 | */ 55 | final void addChoppedGreenOnion() { 56 | System.out.println("放点葱花"); 57 | } 58 | 59 | /** 60 | * 是否需要葱花 (钩子方法) 61 | */ 62 | protected boolean needChoppedGreenOnion() { 63 | return false; 64 | } 65 | 66 | /** 67 | * 倒调味品 68 | */ 69 | abstract void pourSeasoning(); 70 | } 71 | -------------------------------------------------------------------------------- /114-Template-Method/src/main/java/com/zhh/v1/GirlFriendCook.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 具体子类 6 | * @date 2020-02-27 16:07 7 | */ 8 | public class GirlFriendCook extends AbstractCook { 9 | 10 | @Override 11 | void pourSeasoning() { 12 | System.out.println("放点盐"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /114-Template-Method/src/main/java/com/zhh/v1/MeCook.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 具体子类 6 | * @date 2020-02-27 16:04 7 | */ 8 | public class MeCook extends AbstractCook { 9 | 10 | @Override 11 | void pourSeasoning() { 12 | System.out.println("放点盐"); 13 | System.out.println("放点酱油"); 14 | } 15 | 16 | /** 17 | * 此处覆盖了父类钩子方法的默认实现 18 | */ 19 | @Override 20 | protected boolean needChoppedGreenOnion() { 21 | return true; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /114-Template-Method/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-27 16:13 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | // 我做番茄炒蛋 12 | System.out.println("---我做番茄炒蛋 开始---"); 13 | AbstractCook meCook = new MeCook(); 14 | meCook.cook(); 15 | System.out.println("---我做番茄炒蛋 结束---"); 16 | 17 | // 女朋友做番茄炒蛋 18 | System.out.println("---女朋友做番茄炒蛋 开始---"); 19 | AbstractCook girlFriendCook = new GirlFriendCook(); 20 | girlFriendCook.cook(); 21 | System.out.println("---女朋友做番茄炒蛋 结束---"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /115-Iterator/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 115-Iterator 14 | 行为型-迭代器模式 15 | 16 | -------------------------------------------------------------------------------- /115-Iterator/src/main/java/com/zhh/v1/Goods.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品实体类 6 | * @date 2020-02-28 14:20 7 | */ 8 | public class Goods { 9 | 10 | /** 11 | * 名称 12 | */ 13 | private String name; 14 | 15 | /** 16 | * 价格 17 | */ 18 | private double price; 19 | 20 | public Goods(String name, double price) { 21 | this.name = name; 22 | this.price = price; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public double getPrice() { 30 | return price; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /115-Iterator/src/main/java/com/zhh/v1/GoodsAggregate.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品抽象聚合接口 6 | * @date 2020-02-28 14:26 7 | */ 8 | public interface GoodsAggregate { 9 | 10 | /** 11 | * 添加商品 12 | */ 13 | void addGoods(Goods goods); 14 | 15 | /** 16 | * 删除商品 17 | */ 18 | void removeGoods(Goods goods); 19 | 20 | /** 21 | * 获取商品迭代器 22 | */ 23 | GoodsIterator getGoodsIterator(); 24 | } 25 | -------------------------------------------------------------------------------- /115-Iterator/src/main/java/com/zhh/v1/GoodsAggregateImpl.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author zhh 8 | * @description 商品具体聚合类 9 | * @date 2020-02-28 14:30 10 | */ 11 | public class GoodsAggregateImpl implements GoodsAggregate { 12 | 13 | private List goodsList = new ArrayList(); 14 | 15 | public void addGoods(Goods goods) { 16 | goodsList.add(goods); 17 | } 18 | 19 | public void removeGoods(Goods goods) { 20 | goodsList.remove(goods); 21 | } 22 | 23 | public GoodsIterator getGoodsIterator() { 24 | return new GoodsIteratorImpl(goodsList); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /115-Iterator/src/main/java/com/zhh/v1/GoodsIterator.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 商品抽象迭代器接口 6 | * @date 2020-02-28 14:28 7 | */ 8 | public interface GoodsIterator { 9 | 10 | /** 11 | * 获取下一个商品 12 | */ 13 | Goods nextGoods(); 14 | 15 | /** 16 | * 判断是否还有商品 17 | */ 18 | boolean hasGoods(); 19 | } 20 | -------------------------------------------------------------------------------- /115-Iterator/src/main/java/com/zhh/v1/GoodsIteratorImpl.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * @author zhh 7 | * @description 商品抽象迭代器类 8 | * @date 2020-02-28 14:32 9 | */ 10 | public class GoodsIteratorImpl implements GoodsIterator { 11 | 12 | private List goodsList; 13 | 14 | /** 15 | * 商品位置 16 | */ 17 | private int index; 18 | 19 | public GoodsIteratorImpl(List goodsList) { 20 | this.goodsList = goodsList; 21 | } 22 | 23 | public Goods nextGoods() { 24 | System.out.println("当前商品的位置是" + index); 25 | Goods goods = goodsList.get(index); 26 | index++; 27 | return goods; 28 | } 29 | 30 | public boolean hasGoods() { 31 | if (index < goodsList.size()) { 32 | return true; 33 | } 34 | return false; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /115-Iterator/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-28 14:40 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | // 创建商品 12 | Goods goods1 = new Goods("可乐", 3.0); 13 | Goods goods2 = new Goods("薯片", 5.0); 14 | Goods goods3 = new Goods("奶茶", 3.0); 15 | Goods goods4 = new Goods("香肠", 10.0); 16 | Goods goods5 = new Goods("奶粉", 33.0); 17 | Goods goods6 = new Goods("牛肉干", 23.0); 18 | 19 | GoodsAggregate goodsAggregate = new GoodsAggregateImpl(); 20 | goodsAggregate.addGoods(goods1); 21 | goodsAggregate.addGoods(goods2); 22 | goodsAggregate.addGoods(goods3); 23 | goodsAggregate.addGoods(goods4); 24 | goodsAggregate.addGoods(goods5); 25 | goodsAggregate.addGoods(goods6); 26 | 27 | System.out.println("---商品清单---"); 28 | GoodsIterator goodsIterator = goodsAggregate.getGoodsIterator(); 29 | Goods goods; 30 | while (goodsIterator.hasGoods()) { 31 | goods = goodsIterator.nextGoods(); 32 | System.out.println(String.format("商品名称: %s, 商品价格: %s", goods.getName(), goods.getPrice())); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /116-Strategy/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 116-Strategy 14 | 行为型-策略模式 15 | 16 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v1/DiamondMemberStrategy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 钻石会员策略 6 | * @date 2020-02-28 17:01 7 | */ 8 | public class DiamondMemberStrategy implements MemberStrategy { 9 | 10 | public void discount() { 11 | System.out.println("钻石会员所有商品打八折"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v1/DiscountActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 打折活动 6 | * @date 2020-02-28 17:03 7 | */ 8 | public class DiscountActivity { 9 | 10 | private MemberStrategy memberStrategy; 11 | 12 | public DiscountActivity(MemberStrategy memberStrategy) { 13 | this.memberStrategy = memberStrategy; 14 | } 15 | 16 | /** 17 | * 打折 18 | */ 19 | public void discount() { 20 | memberStrategy.discount(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v1/GoldMemberStrategy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 黄金会员策略 6 | * @date 2020-02-28 16:59 7 | */ 8 | public class GoldMemberStrategy implements MemberStrategy { 9 | 10 | public void discount() { 11 | System.out.println("黄金会员所有商品打九五折"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v1/MemberStrategy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 会员策略 6 | * @date 2020-02-28 16:56 7 | */ 8 | public interface MemberStrategy { 9 | 10 | /** 11 | * 打折 12 | */ 13 | void discount(); 14 | } 15 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v1/OrdinaryMemberStrategy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 普通会员策略 6 | * @date 2020-02-28 16:58 7 | */ 8 | public class OrdinaryMemberStrategy implements MemberStrategy { 9 | 10 | public void discount() { 11 | System.out.println("普通会员不打折"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v1/PlatinumMemberStrategy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 铂金会员策略 6 | * @date 2020-02-28 17:00 7 | */ 8 | public class PlatinumMemberStrategy implements MemberStrategy { 9 | 10 | public void discount() { 11 | System.out.println("铂金会员所有商品打八八折"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-28 17:05 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | String member = "黄金会员"; 12 | DiscountActivity discountActivity; 13 | 14 | if ("黄金会员".equals(member)) { 15 | discountActivity = new DiscountActivity(new GoldMemberStrategy()); 16 | } else if ("铂金会员".equals(member)) { 17 | discountActivity = new DiscountActivity(new PlatinumMemberStrategy()); 18 | } else if ("钻石会员".equals(member)) { 19 | discountActivity = new DiscountActivity(new DiamondMemberStrategy()); 20 | } else { 21 | discountActivity = new DiscountActivity(new OrdinaryMemberStrategy()); 22 | } 23 | 24 | discountActivity.discount(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v2/DiamondMemberStrategy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 钻石会员策略 6 | * @date 2020-02-28 17:01 7 | */ 8 | public class DiamondMemberStrategy implements MemberStrategy { 9 | 10 | public void discount() { 11 | System.out.println("钻石会员所有商品打八折"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v2/DiscountActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 打折活动 6 | * @date 2020-02-28 17:03 7 | */ 8 | public class DiscountActivity { 9 | 10 | private MemberStrategy memberStrategy; 11 | 12 | public DiscountActivity(MemberStrategy memberStrategy) { 13 | this.memberStrategy = memberStrategy; 14 | } 15 | 16 | /** 17 | * 打折 18 | */ 19 | public void discount() { 20 | memberStrategy.discount(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v2/GoldMemberStrategy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 黄金会员策略 6 | * @date 2020-02-28 16:59 7 | */ 8 | public class GoldMemberStrategy implements MemberStrategy { 9 | 10 | public void discount() { 11 | System.out.println("黄金会员所有商品打九五折"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v2/MemberStrategy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 会员策略 6 | * @date 2020-02-28 16:56 7 | */ 8 | public interface MemberStrategy { 9 | 10 | /** 11 | * 打折 12 | */ 13 | void discount(); 14 | } 15 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v2/MemberStrategyFactory.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * @author zhh 8 | * @description 会员策略工厂 9 | * @date 2020-02-28 17:24 10 | */ 11 | public class MemberStrategyFactory { 12 | 13 | private static Map MEMBER_STRATEGY_MAP = new HashMap(); 14 | 15 | static { 16 | init(); 17 | } 18 | 19 | private MemberStrategyFactory() {} 20 | 21 | /** 22 | * 获取会员策略 23 | * @param member 会员等级 24 | */ 25 | public static MemberStrategy getMemberStrategy(String member) { 26 | MemberStrategy memberStrategy = MEMBER_STRATEGY_MAP.get(member); 27 | return memberStrategy == null ? MEMBER_STRATEGY_MAP.get(MemberKey.ORDINARY) : memberStrategy; 28 | } 29 | 30 | /** 31 | * 初始化操作 32 | */ 33 | private static void init() { 34 | MEMBER_STRATEGY_MAP.put(MemberKey.ORDINARY, new OrdinaryMemberStrategy()); 35 | MEMBER_STRATEGY_MAP.put(MemberKey.GOLD, new GoldMemberStrategy()); 36 | MEMBER_STRATEGY_MAP.put(MemberKey.PLATINUM, new PlatinumMemberStrategy()); 37 | MEMBER_STRATEGY_MAP.put(MemberKey.DIAMOND, new DiamondMemberStrategy()); 38 | } 39 | 40 | /** 41 | * 会员键值 42 | */ 43 | private interface MemberKey { 44 | String ORDINARY = "普通会员"; 45 | String GOLD = "黄金会员"; 46 | String PLATINUM = "铂金会员"; 47 | String DIAMOND = "钻石会员"; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v2/OrdinaryMemberStrategy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 普通会员策略 6 | * @date 2020-02-28 16:58 7 | */ 8 | public class OrdinaryMemberStrategy implements MemberStrategy { 9 | 10 | public void discount() { 11 | System.out.println("普通会员不打折"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v2/PlatinumMemberStrategy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 铂金会员策略 6 | * @date 2020-02-28 17:00 7 | */ 8 | public class PlatinumMemberStrategy implements MemberStrategy { 9 | 10 | public void discount() { 11 | System.out.println("铂金会员所有商品打八八折"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /116-Strategy/src/main/java/com/zhh/v2/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v2; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-02-28 17:05 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | String member = "黄金会员"; 12 | DiscountActivity discountActivity = new DiscountActivity(MemberStrategyFactory.getMemberStrategy(member)); 13 | discountActivity.discount(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /117-Interpreter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 117-Interpreter 14 | 行为型-解释器模式 15 | 16 | -------------------------------------------------------------------------------- /117-Interpreter/src/main/java/com/zhh/v1/AddExpression.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 加法表达式 6 | * @date 2020-03-01 18:17 7 | */ 8 | public class AddExpression implements Expression { 9 | 10 | private Expression left; 11 | 12 | private Expression right; 13 | 14 | public AddExpression(Expression left, Expression right) { 15 | this.left = left; 16 | this.right = right; 17 | } 18 | 19 | public int interpret() { 20 | return this.left.interpret() + this.right.interpret(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /117-Interpreter/src/main/java/com/zhh/v1/Expression.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 抽象表达式 6 | * @date 2020-02-29 18:41 7 | */ 8 | public interface Expression { 9 | 10 | /** 11 | * 解释 12 | */ 13 | int interpret(); 14 | } 15 | -------------------------------------------------------------------------------- /117-Interpreter/src/main/java/com/zhh/v1/ExpressionContext.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * @author zhh 7 | * @description 表达式环境角色 8 | * @date 2020-03-01 18:23 9 | */ 10 | public class ExpressionContext { 11 | 12 | private Expression expression; 13 | 14 | /** 15 | * 解析 16 | * @param expStr 字符串表达式 17 | */ 18 | public int parse(String expStr) { 19 | final Stack stack = new Stack(); 20 | 21 | String[] expArray = expStr.split(","); 22 | 23 | Expression left; 24 | Expression right; 25 | 26 | for (int i = 0; i < expArray.length; i++) { 27 | String exp = expArray[i]; 28 | if (isSymbol(exp)) { 29 | // +或者-运算符号 30 | left = stack.pop(); 31 | right = new NumberExpression(expArray[++i]); 32 | System.out.println(String.format("数字%s和%s开始进行%s操作运算", left, right, exp)); 33 | Expression expression = getExpression(left, right, exp); 34 | 35 | int result = expression.interpret(); 36 | System.out.println(String.format("运算结果%s开始入栈", result)); 37 | stack.push(new NumberExpression(result)); 38 | } else { 39 | // 数字 40 | NumberExpression numberExpression = new NumberExpression(exp); 41 | System.out.println(String.format("数字%s开始入栈", numberExpression)); 42 | stack.push(numberExpression); 43 | } 44 | } 45 | 46 | return stack.pop().interpret(); 47 | } 48 | 49 | /** 50 | * 是否是符号 51 | * @param exp 表达式 52 | */ 53 | private boolean isSymbol(String exp) { 54 | return "+".equals(exp) || "-".equals(exp); 55 | } 56 | 57 | /** 58 | * 获取表达式 59 | * @param left 左表达式 60 | * @param right 右表达式 61 | * @param symbol 运算符号 62 | * @return 63 | */ 64 | private Expression getExpression(Expression left, Expression right, String symbol) { 65 | if ("+".equals(symbol)) { 66 | return new AddExpression(left, right); 67 | } else if ("-".equals(symbol)) { 68 | return new SubExpression(left, right); 69 | } else { 70 | throw new RuntimeException(String.format("当前解析器不支持该操作符%s的解析", symbol)); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /117-Interpreter/src/main/java/com/zhh/v1/NumberExpression.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 数字表达式 6 | * @date 2020-03-01 18:21 7 | */ 8 | public class NumberExpression implements Expression { 9 | 10 | private int number; 11 | 12 | public NumberExpression(int number) { 13 | this.number = number; 14 | } 15 | 16 | public NumberExpression(String number) { 17 | this.number = Integer.valueOf(number); 18 | } 19 | 20 | public int interpret() { 21 | return this.number; 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return String.format("%s", this.number); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /117-Interpreter/src/main/java/com/zhh/v1/SubExpression.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 减法表达式 6 | * @date 2020-03-01 18:17 7 | */ 8 | public class SubExpression implements Expression { 9 | 10 | private Expression left; 11 | 12 | private Expression right; 13 | 14 | public SubExpression(Expression left, Expression right) { 15 | this.left = left; 16 | this.right = right; 17 | } 18 | 19 | public int interpret() { 20 | return this.left.interpret() - this.right.interpret(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /117-Interpreter/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-03-01 18:26 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | String expStr = "10,-,4,+,1"; 12 | ExpressionContext expressionContext = new ExpressionContext(); 13 | int parse = expressionContext.parse(expStr); 14 | System.out.println("表达式最终的运算结果为: " + parse); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /118-Observer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 118-Observer 14 | 行为型-观察者模式 15 | -------------------------------------------------------------------------------- /118-Observer/src/main/java/com/zhh/v1/Customer.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.Observable; 4 | import java.util.Observer; 5 | 6 | /** 7 | * @author zhh 8 | * @description 顾客类 9 | * @date 2020-03-01 23:20 10 | */ 11 | public class Customer implements Observer { 12 | 13 | /** 14 | * 姓名 15 | */ 16 | private String name; 17 | 18 | public Customer(String name) { 19 | this.name = name; 20 | } 21 | 22 | public void update(Observable o, Object arg) { 23 | Pharmacy pharmacy = (Pharmacy) o; 24 | GauzeMask gauzeMask = (GauzeMask) arg; 25 | System.out.println(String.format("顾客%s收到%s的通知: 最近刚采购了一批%s, 数量为%s, 单价为%s", this.name, pharmacy.getName(), 26 | gauzeMask.getType(), gauzeMask.getAmount(), gauzeMask.getPrice())); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /118-Observer/src/main/java/com/zhh/v1/GauzeMask.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 口罩类 6 | * @date 2020-03-01 23:17 7 | */ 8 | public class GauzeMask { 9 | 10 | /** 11 | * 种类 12 | */ 13 | private String type; 14 | 15 | /** 16 | * 数量 17 | */ 18 | private int amount; 19 | 20 | /** 21 | * 价格 22 | */ 23 | private double price; 24 | 25 | public GauzeMask(String type, int amount, double price) { 26 | this.type = type; 27 | this.amount = amount; 28 | this.price = price; 29 | } 30 | 31 | public String getType() { 32 | return type; 33 | } 34 | 35 | public int getAmount() { 36 | return amount; 37 | } 38 | 39 | public double getPrice() { 40 | return price; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /118-Observer/src/main/java/com/zhh/v1/Pharmacy.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.Observable; 4 | 5 | /** 6 | * @author zhh 7 | * @description 药店类 8 | * @date 2020-03-01 23:15 9 | */ 10 | public class Pharmacy extends Observable { 11 | 12 | /** 13 | * 药店名称 14 | */ 15 | private String name; 16 | 17 | public Pharmacy(String name) { 18 | this.name = name; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | /** 26 | * 采购 27 | * @param pharmacy 药店 28 | * @param gauzeMask 口罩 29 | */ 30 | public void purchase(Pharmacy pharmacy, GauzeMask gauzeMask) { 31 | System.out.println(String.format("%s最近刚采购了一批%s, 数量为%s, 单价为%s", pharmacy.getName(), gauzeMask.getType(), 32 | gauzeMask.getAmount(), gauzeMask.getPrice())); 33 | 34 | // Observable 提供的方法, 代表主题状态的改变 35 | setChanged(); 36 | 37 | // Observable 提供的方法, 通知所有观察者 38 | notifyObservers(gauzeMask); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /118-Observer/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-03-01 23:46 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Pharmacy pharmacy = new Pharmacy("一方大药房"); 12 | GauzeMask gauzeMask = new GauzeMask("一次性口罩", 5000, 4.8); 13 | 14 | Customer customer1 = new Customer("海豪"); 15 | Customer customer2 = new Customer("亚萍"); 16 | pharmacy.addObserver(customer1); 17 | pharmacy.addObserver(customer2); 18 | 19 | pharmacy.purchase(pharmacy, gauzeMask); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /119-Memento/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 119-Memento 14 | 行为型-备忘录模式 15 | 16 | -------------------------------------------------------------------------------- /119-Memento/src/main/java/com/zhh/v1/CodeFile.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 代码文件(发起人) 6 | * @date 2020-03-02 11:22 7 | */ 8 | public class CodeFile { 9 | 10 | /** 11 | * 类名 12 | */ 13 | private String className; 14 | 15 | /** 16 | * 内容 17 | */ 18 | private String content; 19 | 20 | /** 21 | * 备份编号 22 | */ 23 | private String code; 24 | 25 | public CodeFile(String className, String content, String code) { 26 | this.className = className; 27 | this.content = content; 28 | this.code = code; 29 | } 30 | 31 | public String getClassName() { 32 | return className; 33 | } 34 | 35 | public void setClassName(String className) { 36 | this.className = className; 37 | } 38 | 39 | public String getContent() { 40 | return content; 41 | } 42 | 43 | public void setContent(String content) { 44 | this.content = content; 45 | } 46 | 47 | public String getCode() { 48 | return code; 49 | } 50 | 51 | public void setCode(String code) { 52 | this.code = code; 53 | } 54 | 55 | /** 56 | * 创建代码文件备份 57 | */ 58 | public CodeFileMemento saveToMemento() { 59 | System.out.println("创建了一个新的备份, 备份编号: " + this.code); 60 | return new CodeFileMemento(this.className, this.content, this.code); 61 | } 62 | 63 | /** 64 | * 撤销至某个备份 65 | * @param codeFileMemento 代码文件备份 66 | */ 67 | public void undoFromMemento(CodeFileMemento codeFileMemento) { 68 | System.out.println("撤销代码文件至原先备份, 内容为: " + codeFileMemento); 69 | this.className = codeFileMemento.getClassName(); 70 | this.content = codeFileMemento.getContent(); 71 | this.code = codeFileMemento.getCode(); 72 | } 73 | 74 | @Override 75 | public String toString() { 76 | return "CodeFile{" + "className='" + className + '\'' + ", content='" + content + '\'' + ", code='" + code + 77 | '\'' + '}'; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /119-Memento/src/main/java/com/zhh/v1/CodeFileMemento.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 代码文件备份 6 | * @date 2020-03-02 11:26 7 | */ 8 | public class CodeFileMemento { 9 | 10 | /** 11 | * 类名 12 | */ 13 | private String className; 14 | 15 | /** 16 | * 内容 17 | */ 18 | private String content; 19 | 20 | /** 21 | * 备份编号 22 | */ 23 | private String code; 24 | 25 | public CodeFileMemento(String className, String content, String code) { 26 | this.className = className; 27 | this.content = content; 28 | this.code = code; 29 | } 30 | 31 | public String getClassName() { 32 | return className; 33 | } 34 | 35 | public String getContent() { 36 | return content; 37 | } 38 | 39 | public String getCode() { 40 | return code; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "CodeFileMemento{" + "className='" + className + '\'' + ", content='" + content + '\'' + ", code='" + 46 | code + '\'' + '}'; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /119-Memento/src/main/java/com/zhh/v1/CodeFileMementoManager.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * @author zhh 7 | * @description 代码文件备份管理器 8 | * @date 2020-03-02 11:31 9 | */ 10 | public class CodeFileMementoManager { 11 | 12 | private final Stack CODE_FILE_MEMENTO_STACK = new Stack(); 13 | 14 | /** 15 | * 获取代码文件备份 16 | */ 17 | public CodeFileMemento getCodeFileMemento() { 18 | return CODE_FILE_MEMENTO_STACK.pop(); 19 | } 20 | 21 | /** 22 | * 添加代码文件备份 23 | * @param codeFileMemento 代码文件备份 24 | */ 25 | public void addCodeFileMemento(CodeFileMemento codeFileMemento) { 26 | CODE_FILE_MEMENTO_STACK.push(codeFileMemento); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /119-Memento/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-03-02 11:36 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | CodeFileMementoManager codeFileMementoManager = new CodeFileMementoManager(); 12 | 13 | // 代码文件进行备份 14 | CodeFile codeFile = new CodeFile("Test", "public class Test {}", "bak-0001"); 15 | CodeFileMemento codeFileMemento = codeFile.saveToMemento(); 16 | codeFileMementoManager.addCodeFileMemento(codeFileMemento); 17 | 18 | // 修改代码文件内容 19 | codeFile.setContent("public class Test {public static void main(String[] args) {}}"); 20 | codeFile.setCode("bak-0002"); 21 | 22 | System.out.println("当前文件内容: " + codeFile); 23 | 24 | // 后悔了, 进行撤销操作 25 | CodeFileMemento rollbackMemento = codeFileMementoManager.getCodeFileMemento(); 26 | codeFile.undoFromMemento(rollbackMemento); 27 | System.out.println("执行撤销操作后, 当前文件内容: " + codeFile); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /120-Command/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 120-Command 14 | 行为型-命令模式 15 | 16 | -------------------------------------------------------------------------------- /120-Command/src/main/java/com/zhh/v1/AttendMeetingCommand.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 安排会议命令 6 | * @date 2020-03-02 16:38 7 | */ 8 | public class AttendMeetingCommand implements Command { 9 | 10 | private Staff staff; 11 | 12 | public AttendMeetingCommand(Staff staff) { 13 | this.staff = staff; 14 | } 15 | 16 | public void execute() { 17 | staff.attendMeeting(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /120-Command/src/main/java/com/zhh/v1/Boss.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author zhh 8 | * @description 调用者类 9 | * @date 2020-03-02 16:43 10 | */ 11 | public class Boss { 12 | 13 | private List commandList = new ArrayList(); 14 | 15 | /** 16 | * 添加命令 17 | * @param command 命令 18 | */ 19 | public void addCommand(Command command) { 20 | commandList.add(command); 21 | } 22 | 23 | /** 24 | * 发送命令 25 | */ 26 | public void sendCommands() { 27 | for (Command command : commandList) { 28 | command.execute(); 29 | } 30 | commandList.clear(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /120-Command/src/main/java/com/zhh/v1/Command.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 抽象命令接口 6 | * @date 2020-03-02 16:31 7 | */ 8 | public interface Command { 9 | 10 | /** 11 | * 执行命令 12 | */ 13 | void execute(); 14 | } 15 | -------------------------------------------------------------------------------- /120-Command/src/main/java/com/zhh/v1/LookVenueCommand.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 看场地命令 6 | * @date 2020-03-02 16:37 7 | */ 8 | public class LookVenueCommand implements Command { 9 | 10 | private Staff staff; 11 | 12 | public LookVenueCommand(Staff staff) { 13 | this.staff = staff; 14 | } 15 | 16 | public void execute() { 17 | staff.lookVenue(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /120-Command/src/main/java/com/zhh/v1/SendTweetsCommand.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 发送推文命令 6 | * @date 2020-03-02 16:33 7 | */ 8 | public class SendTweetsCommand implements Command { 9 | 10 | private Staff staff; 11 | 12 | public SendTweetsCommand(Staff staff) { 13 | this.staff = staff; 14 | } 15 | 16 | public void execute() { 17 | staff.sendTweets(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /120-Command/src/main/java/com/zhh/v1/Staff.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 接受者类 6 | * @date 2020-03-02 16:58 7 | */ 8 | public class Staff { 9 | 10 | private String name; 11 | 12 | public Staff(String name) { 13 | this.name = name; 14 | } 15 | 16 | /** 17 | * 发推文 18 | */ 19 | public void sendTweets() { 20 | System.out.println(this.name + "早上9点准时在公司公号上发布一篇xx的推文"); 21 | } 22 | 23 | /** 24 | * 看场地 25 | */ 26 | public void lookVenue() { 27 | System.out.println(this.name + "上午和老板一起外出看讲座场地"); 28 | } 29 | 30 | /** 31 | * 安排会议 32 | */ 33 | public void attendMeeting() { 34 | System.out.println(this.name + "安排下午两点的会议"); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /120-Command/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-03-02 16:48 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | Staff staff = new Staff("亚萍"); 12 | Command sendTweetsCommand = new SendTweetsCommand(staff); 13 | Command lookVenueCommand = new LookVenueCommand(staff); 14 | Command attendMeetingCommand = new AttendMeetingCommand(staff); 15 | 16 | Boss boss = new Boss(); 17 | boss.addCommand(sendTweetsCommand); 18 | boss.addCommand(lookVenueCommand); 19 | boss.addCommand(attendMeetingCommand); 20 | 21 | boss.sendCommands(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /121-Mediator/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 121-Mediator 14 | 行为型-中介者模式 15 | 16 | -------------------------------------------------------------------------------- /121-Mediator/src/main/java/com/zhh/v1/Boss.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 老板 6 | * @date 2020-03-03 12:52 7 | */ 8 | public class Boss extends IStaff { 9 | 10 | public Boss(WorkGroup workGroup) { 11 | super(workGroup, null); 12 | // 注册至中介者对象当中 13 | workGroup.addStaff(this); 14 | } 15 | 16 | @Override 17 | public void receiveMessage(String message) { 18 | System.out.println(String.format("老板接收到消息: %s", message)); 19 | } 20 | 21 | @Override 22 | public void sendMessage(String message) { 23 | System.out.println(String.format("老板发送消息: %s", message)); 24 | workGroup.sendMessage(this, message); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /121-Mediator/src/main/java/com/zhh/v1/IStaff.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 员工类 6 | * @date 2020-03-03 12:26 7 | */ 8 | public abstract class IStaff { 9 | 10 | protected WorkGroup workGroup; 11 | 12 | /** 13 | * 姓名 14 | */ 15 | protected String name; 16 | 17 | public IStaff(WorkGroup workGroup, String name) { 18 | this.workGroup = workGroup; 19 | this.name = name; 20 | } 21 | 22 | /** 23 | * 接收消息 24 | * @param message 消息 25 | */ 26 | public abstract void receiveMessage(String message); 27 | 28 | /** 29 | * 发送消息 30 | * @param message 消息 31 | */ 32 | public abstract void sendMessage(String message); 33 | } 34 | -------------------------------------------------------------------------------- /121-Mediator/src/main/java/com/zhh/v1/PersonnelManager.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 人事经理 6 | * @date 2020-03-03 12:49 7 | */ 8 | public class PersonnelManager extends IStaff { 9 | 10 | public PersonnelManager(WorkGroup workGroup) { 11 | super(workGroup, null); 12 | // 注册至中介者对象当中 13 | workGroup.addStaff(this); 14 | } 15 | 16 | @Override 17 | public void receiveMessage(String message) { 18 | System.out.println(String.format("人事经理接收到消息: %s", message)); 19 | } 20 | 21 | @Override 22 | public void sendMessage(String message) { 23 | System.out.println(String.format("人事经理发送消息: %s", message)); 24 | workGroup.sendMessage(this, message); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /121-Mediator/src/main/java/com/zhh/v1/Staff.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 普通员工 6 | * @date 2020-03-03 12:48 7 | */ 8 | public class Staff extends IStaff { 9 | 10 | public Staff(WorkGroup workGroup, String name) { 11 | super(workGroup, name); 12 | // 注册至中介者对象当中 13 | workGroup.addStaff(this); 14 | } 15 | 16 | @Override 17 | public void receiveMessage(String message) { 18 | System.out.println(String.format("员工[%s]接收到消息: %s", this.name, message)); 19 | } 20 | 21 | @Override 22 | public void sendMessage(String message) { 23 | System.out.println(String.format("员工[%s]发送消息: %s", this.name, message)); 24 | workGroup.sendMessage(this, message); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /121-Mediator/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-03-03 12:53 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | // 创建中介者对象 12 | WorkGroup weChatWorkGroup = new WeChatWorkGroup(); 13 | 14 | // 创建具体同事对象 15 | Boss boss = new Boss(weChatWorkGroup); 16 | PersonnelManager personnelManager = new PersonnelManager(weChatWorkGroup); 17 | Staff staffA = new Staff(weChatWorkGroup, "海豪"); 18 | Staff staffB = new Staff(weChatWorkGroup, "亚萍"); 19 | 20 | System.out.println(">>>>>"); 21 | personnelManager.sendMessage("@所有人 由于近期新冠疫情影响,公司决定延迟复工, 具体时间另行通知, 收到请回复!"); 22 | System.out.println("<<<<<"); 23 | System.out.println(); 24 | 25 | System.out.println(">>>>>"); 26 | boss.sendMessage("疫情期间没事不要外出, 勤洗手, 戴口罩, 大家注意安全哦~"); 27 | System.out.println("<<<<<"); 28 | System.out.println(); 29 | 30 | System.out.println(">>>>>"); 31 | staffA.sendMessage("收到"); 32 | System.out.println("<<<<<"); 33 | System.out.println(); 34 | 35 | System.out.println(">>>>>"); 36 | staffB.sendMessage("收到"); 37 | System.out.println("<<<<<"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /121-Mediator/src/main/java/com/zhh/v1/WeChatWorkGroup.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author zhh 8 | * @description 微信工作群 9 | * @date 2020-03-03 12:29 10 | */ 11 | public class WeChatWorkGroup implements WorkGroup { 12 | 13 | private List staffList = new ArrayList(); 14 | 15 | public void addStaff(IStaff staff) { 16 | if (staff != null && !staffList.contains(staff)) { 17 | staffList.add(staff); 18 | } 19 | } 20 | 21 | public void sendMessage(IStaff staff, String message) { 22 | for (IStaff iStaff : staffList) { 23 | // 排除自身, 给别的员工发消息 24 | if (!staff.equals(iStaff)) { 25 | iStaff.receiveMessage(message); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /121-Mediator/src/main/java/com/zhh/v1/WorkGroup.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 工作群类 6 | * @date 2020-03-03 12:12 7 | */ 8 | public interface WorkGroup { 9 | 10 | /** 11 | * 添加员工 12 | * @param staff 员工 13 | */ 14 | void addStaff(IStaff staff); 15 | 16 | /** 17 | * 发送消息 18 | * @param staff 发送消息的员工 19 | * @param message 消息 20 | */ 21 | void sendMessage(IStaff staff, String message); 22 | } 23 | -------------------------------------------------------------------------------- /122-Chain-of-Responsibility/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 122-Chain-of-Responsibility 14 | 行为型-责任链模式 15 | 16 | 17 | 18 | org.apache.commons 19 | commons-lang3 20 | 3.8.1 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /122-Chain-of-Responsibility/src/main/java/com/zhh/v1/Approver.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 审批人 6 | * @date 2020-03-03 14:58 7 | */ 8 | public abstract class Approver { 9 | 10 | /** 11 | * 后继审批人 12 | */ 13 | protected Approver approver; 14 | 15 | /** 16 | * 设置后继审批人 17 | * @param approver 后继审批人 18 | */ 19 | public void setNextApprpver(Approver approver) { 20 | this.approver = approver; 21 | } 22 | 23 | /** 24 | * 审批 25 | * @param request 事假请求 26 | */ 27 | public abstract void approve(PersonalLeaveRequest request); 28 | } 29 | -------------------------------------------------------------------------------- /122-Chain-of-Responsibility/src/main/java/com/zhh/v1/BossApprover.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | 5 | /** 6 | * @author zhh 7 | * @description 老板 8 | * @date 2020-03-03 15:11 9 | */ 10 | public class BossApprover extends Approver { 11 | 12 | @Override 13 | public void approve(PersonalLeaveRequest request) { 14 | if (StringUtils.isNotEmpty(request.getReason())) { 15 | System.out.println("老板审核结果: 批准"); 16 | if (approver != null) { 17 | approver.approve(request); 18 | } 19 | } else { 20 | System.out.println("老板审核结果: 不批准"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /122-Chain-of-Responsibility/src/main/java/com/zhh/v1/GroupLeaderApprover.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | 5 | /** 6 | * @author zhh 7 | * @description 组长 8 | * @date 2020-03-03 15:07 9 | */ 10 | public class GroupLeaderApprover extends Approver { 11 | 12 | @Override 13 | public void approve(PersonalLeaveRequest request) { 14 | if (StringUtils.isNotEmpty(request.getReason())) { 15 | System.out.println("组长审核结果: 批准"); 16 | if (approver != null) { 17 | approver.approve(request); 18 | } 19 | } else { 20 | System.out.println("组长审核结果: 不批准"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /122-Chain-of-Responsibility/src/main/java/com/zhh/v1/InspectorGeneralApprover.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | 5 | /** 6 | * @author zhh 7 | * @description 总监 8 | * @date 2020-03-03 15:11 9 | */ 10 | public class InspectorGeneralApprover extends Approver { 11 | 12 | @Override 13 | public void approve(PersonalLeaveRequest request) { 14 | if (StringUtils.isNotEmpty(request.getReason())) { 15 | System.out.println("总监审核结果: 批准"); 16 | if (approver != null) { 17 | approver.approve(request); 18 | } 19 | } else { 20 | System.out.println("总监审核结果: 不批准"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /122-Chain-of-Responsibility/src/main/java/com/zhh/v1/PersonalLeaveRequest.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 事假请求实体类 6 | * @date 2020-03-03 14:50 7 | */ 8 | public class PersonalLeaveRequest { 9 | 10 | /** 11 | * 申请人 12 | */ 13 | private String applicant; 14 | 15 | /** 16 | * 天数 17 | */ 18 | private Integer days; 19 | 20 | /** 21 | * 开始时间 22 | */ 23 | private String startTime; 24 | 25 | /** 26 | * 理由 27 | */ 28 | private String reason; 29 | 30 | public String getApplicant() { 31 | return applicant; 32 | } 33 | 34 | public void setApplicant(String applicant) { 35 | this.applicant = applicant; 36 | } 37 | 38 | public Integer getDays() { 39 | return days; 40 | } 41 | 42 | public void setDays(Integer days) { 43 | this.days = days; 44 | } 45 | 46 | public String getStartTime() { 47 | return startTime; 48 | } 49 | 50 | public void setStartTime(String startTime) { 51 | this.startTime = startTime; 52 | } 53 | 54 | public String getReason() { 55 | return reason; 56 | } 57 | 58 | public void setReason(String reason) { 59 | this.reason = reason; 60 | } 61 | 62 | /** 63 | * 天数常量 64 | */ 65 | public interface Days { 66 | Integer ONE = 1; 67 | Integer SEVEN = 7; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /122-Chain-of-Responsibility/src/main/java/com/zhh/v1/PersonnelManagerApprover.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | 5 | /** 6 | * @author zhh 7 | * @description 人事经理 8 | * @date 2020-03-03 15:07 9 | */ 10 | public class PersonnelManagerApprover extends Approver { 11 | 12 | @Override 13 | public void approve(PersonalLeaveRequest request) { 14 | if (StringUtils.isNotEmpty(request.getReason())) { 15 | System.out.println("人事经理审核结果: 批准"); 16 | if (approver != null) { 17 | approver.approve(request); 18 | } 19 | } else { 20 | System.out.println("人事经理审核结果: 不批准"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /122-Chain-of-Responsibility/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-03-03 15:23 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | PersonalLeaveRequest request = new PersonalLeaveRequest(); 12 | request.setApplicant("海豪"); 13 | request.setDays(6); 14 | request.setStartTime("2020年03月03日15:24:37"); 15 | request.setReason("家中有事"); 16 | 17 | Approver groupLeaderApprover = new GroupLeaderApprover(); 18 | Approver personnelManagerApprover = new PersonnelManagerApprover(); 19 | Approver inspectorGeneralApprover = new InspectorGeneralApprover(); 20 | Approver bossApprover = new BossApprover(); 21 | 22 | Integer days = request.getDays(); 23 | // 审批事假规则 24 | if (PersonalLeaveRequest.Days.ONE.equals(days)) { 25 | // 事假天数1天 26 | groupLeaderApprover.setNextApprpver(personnelManagerApprover); 27 | } else if (days > PersonalLeaveRequest.Days.ONE && days <= PersonalLeaveRequest.Days.SEVEN) { 28 | // 事假天数1天-7天(包含7天) 29 | groupLeaderApprover.setNextApprpver(inspectorGeneralApprover); 30 | inspectorGeneralApprover.setNextApprpver(personnelManagerApprover); 31 | } else if (days > PersonalLeaveRequest.Days.SEVEN) { 32 | // 事假天数7天以上 33 | groupLeaderApprover.setNextApprpver(inspectorGeneralApprover); 34 | groupLeaderApprover.setNextApprpver(bossApprover); 35 | bossApprover.setNextApprpver(personnelManagerApprover); 36 | } else { 37 | throw new RuntimeException("请假天数异常"); 38 | } 39 | 40 | System.out.println(">>>>"); 41 | System.out.println( 42 | String.format("[%s]发起请假申请, 请假天数[%s]天, 请假开始时间[%s], 请假理由[%s]", request.getApplicant(), request.getDays(), 43 | request.getStartTime(), request.getReason())); 44 | System.out.println("<<<<"); 45 | 46 | System.out.println("审核结果如下:"); 47 | groupLeaderApprover.approve(request); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /123-Visitor/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 123-Visitor 14 | 行为型-访问者模式 15 | 16 | -------------------------------------------------------------------------------- /123-Visitor/src/main/java/com/zhh/v1/ArticleContent.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 文章内容 6 | * @date 2020-03-03 17:11 7 | */ 8 | public class ArticleContent extends Content { 9 | 10 | public ArticleContent(String name) { 11 | super(name); 12 | } 13 | 14 | @Override 15 | public void accept(Visitor visitor) { 16 | visitor.visit(this); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /123-Visitor/src/main/java/com/zhh/v1/Blog.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author zhh 8 | * @description 博客类 9 | * @date 2020-03-03 17:34 10 | */ 11 | public class Blog { 12 | 13 | private List contentList = new ArrayList(); 14 | 15 | /** 16 | * 添加内容 17 | * @param content 内容 18 | */ 19 | public void addContent(Content content) { 20 | contentList.add(content); 21 | } 22 | 23 | /** 24 | * 删除内容 25 | * @param content 内容 26 | */ 27 | public void removeContent(Content content) { 28 | contentList.remove(content); 29 | } 30 | 31 | /** 32 | * 接受访问者访问 33 | * @param visitor 访问者 34 | */ 35 | public void accept(Visitor visitor) { 36 | for (Content content : contentList) { 37 | content.accept(visitor); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /123-Visitor/src/main/java/com/zhh/v1/Content.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 内容类 6 | * @date 2020-03-03 17:08 7 | */ 8 | public abstract class Content { 9 | 10 | /** 11 | * 名称 12 | */ 13 | private String name; 14 | 15 | public Content(String name) { 16 | this.name = name; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | /** 24 | * 接受操作, 核心方法, 接受访问者访问 25 | * @param visitor 访问者 26 | */ 27 | public abstract void accept(Visitor visitor); 28 | } 29 | -------------------------------------------------------------------------------- /123-Visitor/src/main/java/com/zhh/v1/GuestUserVisitor.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 非注册用户 6 | * @date 2020-03-03 17:06 7 | */ 8 | public class GuestUserVisitor implements Visitor { 9 | 10 | public void visit(ArticleContent articleContent) { 11 | System.out.println(String.format("匿名用户访问博客文章[%s]", articleContent.getName())); 12 | } 13 | 14 | public void visit(ResourceContent resourceContent) { 15 | if (resourceContent.isNeedLogin()) { 16 | System.out.println("匿名用户无法访问当前资源, 请登录!"); 17 | return; 18 | } 19 | System.out.println(String.format("匿名用户下载博客资源[%s]", resourceContent.getName())); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /123-Visitor/src/main/java/com/zhh/v1/RegisteredUserVisitor.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 注册用户 6 | * @date 2020-03-03 17:14 7 | */ 8 | public class RegisteredUserVisitor implements Visitor { 9 | 10 | public void visit(ArticleContent articleContent) { 11 | System.out.println(String.format("CSDN用户访问博客文章[%s]", articleContent.getName())); 12 | } 13 | 14 | public void visit(ResourceContent resourceContent) { 15 | System.out.println(String.format("CSDN用户下载博客资源[%s]", resourceContent.getName())); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /123-Visitor/src/main/java/com/zhh/v1/ResourceContent.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 资源内容 6 | * @date 2020-03-03 17:15 7 | */ 8 | public class ResourceContent extends Content { 9 | 10 | /** 11 | * 是否需要登录 12 | */ 13 | private boolean needLogin; 14 | 15 | public ResourceContent(String name, boolean needLogin) { 16 | super(name); 17 | this.needLogin = needLogin; 18 | } 19 | 20 | public boolean isNeedLogin() { 21 | return needLogin; 22 | } 23 | 24 | @Override 25 | public void accept(Visitor visitor) { 26 | visitor.visit(this); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /123-Visitor/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-03-03 17:34 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | // 创建对象结构 12 | Blog blog = new Blog(); 13 | 14 | // 创建具体元素 15 | Content articleContent = new ArticleContent("Java设计模式之行为型-访问者模式 (Visitor)"); 16 | Content resourceContent = new ResourceContent("代码生成器工具.zip", true); 17 | 18 | blog.addContent(articleContent); 19 | blog.addContent(resourceContent); 20 | 21 | // 创建不同访问对象 22 | Visitor registeredUserVisitor = new RegisteredUserVisitor(); 23 | Visitor guestUserVisitor = new GuestUserVisitor(); 24 | 25 | // 不同对象分别访问数据 26 | blog.accept(registeredUserVisitor); 27 | blog.accept(guestUserVisitor); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /123-Visitor/src/main/java/com/zhh/v1/Visitor.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 抽象访问者 6 | * @date 2020-03-03 17:07 7 | */ 8 | public interface Visitor { 9 | 10 | /** 11 | * 访问文章内容 12 | * @param articleContent 文章内容 13 | */ 14 | void visit(ArticleContent articleContent); 15 | 16 | /** 17 | * 访问资源内容 18 | * @param resourceContent 资源内容 19 | */ 20 | void visit(ResourceContent resourceContent); 21 | } 22 | -------------------------------------------------------------------------------- /124-State/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | 8 | java-design-patterns 9 | com.zhh 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 124-State 14 | 行为型-状态模式 15 | 16 | -------------------------------------------------------------------------------- /124-State/src/main/java/com/zhh/v1/ElectricLightContext.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 电灯环境类(环境类) 6 | * @date 2020-03-03 22:39 7 | */ 8 | public class ElectricLightContext { 9 | 10 | private ElectricLightState electricLightState; 11 | 12 | public final static TurnOnState TURN_ON_STATE = new TurnOnState(); 13 | 14 | public final static TurnOffState TURN_OFF_STATE = new TurnOffState(); 15 | 16 | public ElectricLightState getElectricLightState() { 17 | return electricLightState; 18 | } 19 | 20 | public void setElectricLightState(ElectricLightState electricLightState) { 21 | this.electricLightState = electricLightState; 22 | // 将当前的环境通知到各个状态实现类 23 | this.electricLightState.setElectricLightContext(this); 24 | } 25 | 26 | public void turnOn() { 27 | this.electricLightState.turnOn(); 28 | } 29 | 30 | public void turnOff() { 31 | this.electricLightState.turnOff(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /124-State/src/main/java/com/zhh/v1/ElectricLightState.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 电灯状态类(抽象状态类) 6 | * @date 2020-03-03 22:38 7 | */ 8 | public abstract class ElectricLightState { 9 | 10 | protected ElectricLightContext electricLightContext; 11 | 12 | public void setElectricLightContext(ElectricLightContext electricLightContext) { 13 | this.electricLightContext = electricLightContext; 14 | } 15 | 16 | /** 17 | * 开灯 18 | */ 19 | public abstract void turnOn(); 20 | 21 | /** 22 | * 关灯 23 | */ 24 | public abstract void turnOff(); 25 | 26 | /** 27 | * 获取状态 28 | */ 29 | public abstract String getState(); 30 | } 31 | -------------------------------------------------------------------------------- /124-State/src/main/java/com/zhh/v1/Test.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 测试类 6 | * @date 2020-03-03 22:55 7 | */ 8 | public class Test { 9 | 10 | public static void main(String[] args) { 11 | ElectricLightContext electricLightContext = new ElectricLightContext(); 12 | electricLightContext.setElectricLightState(new TurnOnState()); 13 | System.out.println(String.format("当前电灯的状态: [%s]", electricLightContext.getElectricLightState().getState())); 14 | electricLightContext.turnOn(); 15 | 16 | electricLightContext.turnOff(); 17 | System.out.println(String.format("当前电灯的状态: [%s]", electricLightContext.getElectricLightState().getState())); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /124-State/src/main/java/com/zhh/v1/TurnOffState.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 关闭状态 6 | * @date 2020-03-03 22:43 7 | */ 8 | public class TurnOffState extends ElectricLightState { 9 | 10 | @Override 11 | public void turnOn() { 12 | super.electricLightContext.setElectricLightState(ElectricLightContext.TURN_ON_STATE); 13 | } 14 | 15 | @Override 16 | public void turnOff() { 17 | System.out.println("操作错误, 当前电灯状态已经是关闭状态"); 18 | } 19 | 20 | @Override 21 | public String getState() { 22 | return "关闭状态"; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /124-State/src/main/java/com/zhh/v1/TurnOnState.java: -------------------------------------------------------------------------------- 1 | package com.zhh.v1; 2 | 3 | /** 4 | * @author zhh 5 | * @description 打开状态 6 | * @date 2020-03-03 22:42 7 | */ 8 | public class TurnOnState extends ElectricLightState { 9 | 10 | @Override 11 | public void turnOn() { 12 | System.out.println("操作错误, 当前电灯状态已经是打开状态"); 13 | } 14 | 15 | @Override 16 | public void turnOff() { 17 | super.electricLightContext.setElectricLightState(ElectricLightContext.TURN_OFF_STATE); 18 | } 19 | 20 | @Override 21 | public String getState() { 22 | return "打开状态"; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 设计模式 2 | 本仓库为笔者个人设计模式的学习笔记中的代码片段,目前已经完结,欢迎食用、指点。 3 | 4 | 具体学习笔记的内容可前往 [个人博客](https://www.zhaohaihao.com/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/) 或者 [CSDN博客](https://blog.csdn.net/dh_chao/category_7023938.html) 查看。 5 | 6 | ## 目录 7 | ### 设计原则 8 | - [x] [设计原则-开闭原则 (OCP)](https://www.yuque.com/zhaohaihao/coding/issue-0001) 9 | - [x] [设计原则-依赖倒置原则 (DIP)](https://www.yuque.com/zhaohaihao/coding/issue-0002) 10 | - [x] [设计原则-单一职责原则 (SRP)](https://www.yuque.com/zhaohaihao/coding/issue-0003) 11 | - [x] [设计原则-接口隔离原则 (ISP)](https://www.yuque.com/zhaohaihao/coding/issue-0004) 12 | - [x] [设计原则-迪米特法则 (LoD)](https://www.yuque.com/zhaohaihao/coding/issue-0005) 13 | - [x] [设计原则-里氏替换原则 (LSP)](https://www.yuque.com/zhaohaihao/coding/issue-0006) 14 | - [x] [设计原则-合成复用原则 (CRP)](https://www.yuque.com/zhaohaihao/coding/issue-0007) 15 | ### 创建型模式 16 | - [x] [创建型-简单工厂模式 (Simple Factory)](https://www.yuque.com/zhaohaihao/coding/issue-0008) 17 | - [x] [创建型-工厂方法模式 (Factory Method)](https://www.yuque.com/zhaohaihao/coding/issue-0009) 18 | - [x] [创建型-抽象工厂模式 (Abstract Factory)](https://www.yuque.com/zhaohaihao/coding/issue-0010) 19 | - [x] [创建型-建造者模式 (Builder)](https://www.yuque.com/zhaohaihao/coding/issue-0011) 20 | - [x] [创建型-单例模式 (Singleton)](https://www.yuque.com/zhaohaihao/coding/issue-0012) 21 | - [x] [创建型-原型模式 (Prototype)](https://www.yuque.com/zhaohaihao/coding/issue-0013) 22 | ### 结构型模式 23 | - [x] [结构型-外观模式 (Facade)](https://www.yuque.com/zhaohaihao/coding/issue-0014) 24 | - [x] [结构型-装饰器模式 (Decorator)](https://www.yuque.com/zhaohaihao/coding/issue-0015) 25 | - [x] [结构型-适配器模式 (Adapter)](https://www.yuque.com/zhaohaihao/coding/issue-0016) 26 | - [x] [结构型-享元模式 (Flyweight)](https://www.yuque.com/zhaohaihao/coding/issue-0017) 27 | - [x] [结构型-组合模式 (Composite)](https://www.yuque.com/zhaohaihao/coding/issue-0018) 28 | - [x] [结构型-桥接模式 (Bridge)](https://www.yuque.com/zhaohaihao/coding/issue-0019) 29 | - [x] [结构型-代理模式 (Proxy)](https://www.yuque.com/zhaohaihao/coding/issue-0020) 30 | ### 行为型模式 31 | - [x] [行为型-模板方法模式 (Template Method)](https://www.yuque.com/zhaohaihao/coding/issue-0021) 32 | - [x] [行为型-迭代器模式 (Iterator)](https://www.yuque.com/zhaohaihao/coding/issue-0022) 33 | - [x] [行为型-策略模式 (Strategy)](https://www.yuque.com/zhaohaihao/coding/issue-0023) 34 | - [x] [行为型-解释器模式 (Interpreter)](https://www.yuque.com/zhaohaihao/coding/issue-0024) 35 | - [x] [行为型-观察者模式 (Observer)](https://www.yuque.com/zhaohaihao/coding/issue-0025) 36 | - [x] [行为型-备忘录模式 (Memento)](https://www.yuque.com/zhaohaihao/coding/issue-0026) 37 | - [x] [行为型-命令模式 (Command)](https://www.yuque.com/zhaohaihao/coding/issue-0027) 38 | - [x] [行为型-中介者模式 (Mediator)](https://www.yuque.com/zhaohaihao/coding/issue-0028) 39 | - [x] [行为型-责任链模式 (Chain of Responsibility)](https://www.yuque.com/zhaohaihao/coding/issue-0029) 40 | - [x] [行为型-访问者模式 (Visitor)](https://www.yuque.com/zhaohaihao/coding/issue-0030) 41 | - [x] [行为型-状态模式 (State)](https://www.yuque.com/zhaohaihao/coding/issue-0031) 42 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.zhh 8 | java-design-patterns 9 | pom 10 | 1.0-SNAPSHOT 11 | Java版设计模式学习笔记 12 | 13 | 001-OCP 14 | 002-DIP 15 | 003-SRP 16 | 004-ISP 17 | 005-LoD 18 | 006-LSP 19 | 007-CRP 20 | 101-Simple-Factory 21 | 102-Factory-Method 22 | 103-Abstract-Factory 23 | 104-Builder 24 | 105-Singleton 25 | 106-Prototype 26 | 107-Facade 27 | 108-Decorator 28 | 109-Adapter 29 | 110-Flyweight 30 | 111-Composite 31 | 112-Bridge 32 | 113-Proxy 33 | 114-Template-Method 34 | 115-Iterator 35 | 116-Strategy 36 | 117-Interpreter 37 | 118-Observer 38 | 119-Memento 39 | 120-Command 40 | 121-Mediator 41 | 122-Chain-of-Responsibility 42 | 123-Visitor 43 | 124-State 44 | 45 | 46 | 47 | 48 | zhh 49 | zhh2933@gmail.com 50 | 51 | 52 | --------------------------------------------------------------------------------