├── 02-策略模式 ├── readme.md ├── 策略模式.png └── Demo.java ├── .gitignore ├── 11-迪米特法则 └── readme.md ├── 01-简单工厂模式 ├── readme.md ├── 简单工厂.png └── SimpleFactory.java ├── 06-装饰模式 ├── 装饰模式.png └── DecoratorDemo.java ├── 07-代理模式 ├── 代理模式.png ├── readme.md └── ProxyDemo.java ├── 09-原型模式 ├── 原型模式.png ├── PrototypeDemo.java ├── PrototypeDemo3.java ├── PrototypeDemo2.java └── PrototypeDemo4.java ├── 10-模板方法 ├── 模板方法.png └── TemplateDemo.java ├── 12-外观模式 ├── 外观模式.png ├── readme.md └── FacadeDemo.java ├── 16-状态模式 ├── 状态模式.png └── StateDemo.java ├── 19-组合模式 ├── 组合模式.png └── CompositeDemo.java ├── 22-桥接模式 ├── 桥接模式.png └── BridgeDemo.java ├── 23-命令模式 ├── 命令模式.png └── CommandDemo.java ├── 26-享元模式 ├── 享元模式.png └── FlyWeightDemo.java ├── 08-工厂方法模式 ├── 工厂方法.png ├── readme.md └── FactoryDemo.java ├── 13-建造者模式 ├── 建造者模式.png └── BuilderDemo.java ├── 14-观察者模式 ├── 观察者模式.png └── ObserverDemo.java ├── 15-抽象工厂模式 ├── 抽象工厂模式.png └── AbstractFactoryDemo.java ├── 17-适配器模式 ├── 适配器模式.png └── AdapterDemo.java ├── 18-备忘录模式 ├── 备忘录模式.png └── MemntoDemo.java ├── 20-迭代器模式 ├── 迭代器模式.png └── IteratorDemo.java ├── 24-职责链模式 ├── 责任链模式.png └── ChainOfResponsibilityDemo.java ├── 25-中介者模式 ├── 中介者模式.png └── MediatorDemo.java ├── 27-解释器模式 ├── 解释器模式.png └── InterpreterDemo.java ├── 28-访问者模式 ├── 访问者模式.png └── VisitorDemo.java ├── 21-单例模式 └── SingletonDemo1.java └── readme.md /02-策略模式/readme.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | out/* 3 | *.iml 4 | *.xml 5 | *.class -------------------------------------------------------------------------------- /11-迪米特法则/readme.md: -------------------------------------------------------------------------------- 1 | 最少知识原则: 2 | 3 | - 成员对象尽量最低访问权限 4 | - 如果两个类没有必要的通信,应该使用第三方进行转发调用,进行解耦 -------------------------------------------------------------------------------- /01-简单工厂模式/readme.md: -------------------------------------------------------------------------------- 1 | 面向对象语言:可维护、可复用、可扩展、灵活性好. 2 | 3 | 涉及到UML图的绘制: 4 | 5 | 继承、实现、聚合、组合、关联、依赖 -------------------------------------------------------------------------------- /02-策略模式/策略模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/02-策略模式/策略模式.png -------------------------------------------------------------------------------- /06-装饰模式/装饰模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/06-装饰模式/装饰模式.png -------------------------------------------------------------------------------- /07-代理模式/代理模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/07-代理模式/代理模式.png -------------------------------------------------------------------------------- /09-原型模式/原型模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/09-原型模式/原型模式.png -------------------------------------------------------------------------------- /10-模板方法/模板方法.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/10-模板方法/模板方法.png -------------------------------------------------------------------------------- /12-外观模式/外观模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/12-外观模式/外观模式.png -------------------------------------------------------------------------------- /16-状态模式/状态模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/16-状态模式/状态模式.png -------------------------------------------------------------------------------- /19-组合模式/组合模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/19-组合模式/组合模式.png -------------------------------------------------------------------------------- /22-桥接模式/桥接模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/22-桥接模式/桥接模式.png -------------------------------------------------------------------------------- /23-命令模式/命令模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/23-命令模式/命令模式.png -------------------------------------------------------------------------------- /26-享元模式/享元模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/26-享元模式/享元模式.png -------------------------------------------------------------------------------- /01-简单工厂模式/简单工厂.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/01-简单工厂模式/简单工厂.png -------------------------------------------------------------------------------- /08-工厂方法模式/工厂方法.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/08-工厂方法模式/工厂方法.png -------------------------------------------------------------------------------- /13-建造者模式/建造者模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/13-建造者模式/建造者模式.png -------------------------------------------------------------------------------- /14-观察者模式/观察者模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/14-观察者模式/观察者模式.png -------------------------------------------------------------------------------- /15-抽象工厂模式/抽象工厂模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/15-抽象工厂模式/抽象工厂模式.png -------------------------------------------------------------------------------- /17-适配器模式/适配器模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/17-适配器模式/适配器模式.png -------------------------------------------------------------------------------- /18-备忘录模式/备忘录模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/18-备忘录模式/备忘录模式.png -------------------------------------------------------------------------------- /20-迭代器模式/迭代器模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/20-迭代器模式/迭代器模式.png -------------------------------------------------------------------------------- /24-职责链模式/责任链模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/24-职责链模式/责任链模式.png -------------------------------------------------------------------------------- /25-中介者模式/中介者模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/25-中介者模式/中介者模式.png -------------------------------------------------------------------------------- /27-解释器模式/解释器模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/27-解释器模式/解释器模式.png -------------------------------------------------------------------------------- /28-访问者模式/访问者模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xinghalo/DesignPattern/HEAD/28-访问者模式/访问者模式.png -------------------------------------------------------------------------------- /08-工厂方法模式/readme.md: -------------------------------------------------------------------------------- 1 | 简单工厂: 2 | 3 | 逻辑都封装在工厂中,客户端不需要增加代码,工厂就可以扩展功能。 4 | 5 | 工厂方法: 6 | 7 | 提供工厂和实例的接口,使用哪个工厂方法,由客户端确定。 -------------------------------------------------------------------------------- /12-外观模式/readme.md: -------------------------------------------------------------------------------- 1 | 外观模式的使用场景: 2 | 3 | - 设计初期,逻辑与实现分离 4 | - 开发阶段,代码不断复杂,减少应用与之依赖关系 5 | - 维护阶段,新系统与新接口对接,facade与遗留系统对接,减少复杂程度 -------------------------------------------------------------------------------- /07-代理模式/readme.md: -------------------------------------------------------------------------------- 1 | 代理模式的四个应用场景: 2 | 3 | - 远程代理,为对象在不同的空间提供局部代表 4 | - 虚拟代理,创建开销很大,存放实例化需要很长时间的对象 5 | - 安全代理,用来控制真实对象的访问权限 6 | - 智能引用,代理处理额外的一些事情 -------------------------------------------------------------------------------- /17-适配器模式/AdapterDemo.java: -------------------------------------------------------------------------------- 1 | abstract class Target { 2 | public void request(){ 3 | System.out.println("普通请求"); 4 | } 5 | } 6 | class Adaptee { 7 | public void specificRequest(){ 8 | System.out.println("特殊的请求"); 9 | } 10 | } 11 | class Adapter extends Target { 12 | private Adaptee adaptee = new Adaptee(); 13 | 14 | @Override 15 | public void request(){ 16 | adaptee.specificRequest(); 17 | } 18 | } 19 | 20 | public class AdapterDemo { 21 | public static void main(String[] args) { 22 | Target target = new Adapter(); 23 | target.request(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /23-命令模式/CommandDemo.java: -------------------------------------------------------------------------------- 1 | import javax.sound.midi.Receiver; 2 | 3 | abstract class Command { 4 | protected Reciever receiver; 5 | 6 | public Command(Reciever receiver) { 7 | this.receiver = receiver; 8 | } 9 | 10 | abstract public void execute(); 11 | } 12 | class ConcreteCommand extends Command { 13 | public ConcreteCommand(Reciever receiver) { 14 | super(receiver); 15 | } 16 | 17 | @Override 18 | public void execute() { 19 | receiver.action(); 20 | } 21 | } 22 | class Invoker { 23 | private Command command; 24 | 25 | public void setCommand(Command command) { 26 | this.command = command; 27 | } 28 | 29 | public void executeCommand(){ 30 | this.command.execute(); 31 | } 32 | } 33 | class Reciever { 34 | public void action(){ 35 | System.out.println("执行请求!"); 36 | } 37 | } 38 | public class CommandDemo { 39 | public static void main(String[] args) { 40 | Reciever r = new Reciever(); 41 | Command c = new ConcreteCommand(r); 42 | Invoker i = new Invoker(); 43 | 44 | i.setCommand(c); 45 | i.executeCommand(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /22-桥接模式/BridgeDemo.java: -------------------------------------------------------------------------------- 1 | interface Implementor { 2 | public void operation(); 3 | } 4 | class ConcreateImplementorA implements Implementor { 5 | @Override 6 | public void operation() { 7 | System.out.println("具体实现A的方法执行"); 8 | } 9 | } 10 | class ConcreateImplementorB implements Implementor { 11 | @Override 12 | public void operation() { 13 | System.out.println("具体实现B的方法执行"); 14 | } 15 | } 16 | class Abstraction { 17 | protected Implementor implementor; 18 | 19 | public void setImplementor(Implementor implementor) { 20 | this.implementor = implementor; 21 | } 22 | 23 | public void operation(){ 24 | implementor.operation(); 25 | } 26 | } 27 | class RefinedAbstraction extends Abstraction { 28 | @Override 29 | public void operation(){ 30 | implementor.operation(); 31 | } 32 | } 33 | public class BridgeDemo { 34 | public static void main(String[] args) { 35 | Abstraction ab = new RefinedAbstraction(); 36 | 37 | ab.setImplementor(new ConcreateImplementorA()); 38 | ab.operation(); 39 | 40 | ab.setImplementor(new ConcreateImplementorB()); 41 | ab.operation(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /21-单例模式/SingletonDemo1.java: -------------------------------------------------------------------------------- 1 | class Singleton1 { 2 | private static volatile Singleton1 instance = null; 3 | 4 | private Singleton1(){} 5 | 6 | public static Singleton1 getInstance(){ 7 | if(instance == null){ 8 | synchronized (Singleton1.class){ 9 | if(instance == null){ 10 | instance = new Singleton1(); 11 | } 12 | } 13 | } 14 | return instance; 15 | } 16 | } 17 | 18 | class Singleton7 { 19 | private Singleton7(){} 20 | 21 | public static Singleton7 getInstance(){ 22 | return InstanceHolder.instance; 23 | } 24 | 25 | static class InstanceHolder{ 26 | private static Singleton7 instance = new Singleton7(); 27 | } 28 | } 29 | public class SingletonDemo1 { 30 | public static void main(String[] args) { 31 | Singleton1 singleton21 = Singleton1.getInstance(); 32 | Singleton1 singleton211 = Singleton1.getInstance(); 33 | System.out.println(singleton21); 34 | System.out.println(singleton211); 35 | 36 | Singleton7 singleton1 = Singleton7.getInstance(); 37 | Singleton7 singleton11 = Singleton7.getInstance(); 38 | System.out.println(singleton1); 39 | System.out.println(singleton11); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /12-外观模式/FacadeDemo.java: -------------------------------------------------------------------------------- 1 | class SubSystemOne { 2 | public void methodOne(){ 3 | System.out.println("子系统1的方法"); 4 | } 5 | } 6 | class SubSystemTwo { 7 | public void methodTwo(){ 8 | System.out.println("子系统2的方法"); 9 | } 10 | } 11 | class SubSystemThree { 12 | public void methodThree(){ 13 | System.out.println("子系统3的方法"); 14 | } 15 | } 16 | class SubSystemFour { 17 | public void methodFour(){ 18 | System.out.println("子系统4的方法"); 19 | } 20 | } 21 | class Facade { 22 | SubSystemOne subSystemOne; 23 | SubSystemTwo subSystemTwo; 24 | SubSystemThree subSystemThree; 25 | SubSystemFour subSystemFour; 26 | 27 | public Facade () { 28 | subSystemOne = new SubSystemOne(); 29 | subSystemTwo = new SubSystemTwo(); 30 | subSystemThree = new SubSystemThree(); 31 | subSystemFour = new SubSystemFour(); 32 | } 33 | 34 | public void methodA() { 35 | System.out.println("方法A"); 36 | subSystemOne.methodOne(); 37 | subSystemTwo.methodTwo(); 38 | } 39 | 40 | public void methodB() { 41 | System.out.println("方法B"); 42 | subSystemThree.methodThree(); 43 | subSystemFour.methodFour(); 44 | } 45 | } 46 | 47 | public class FacadeDemo { 48 | public static void main(String[] args) { 49 | Facade facade = new Facade(); 50 | facade.methodA(); 51 | facade.methodB(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /10-模板方法/TemplateDemo.java: -------------------------------------------------------------------------------- 1 | abstract class Paper { 2 | void question1(){ 3 | System.out.println("你的名字是什么?"); 4 | } 5 | void question2(){ 6 | System.out.println("你的单位叫什么?"); 7 | } 8 | void question3(){ 9 | System.out.println("你的家乡是哪里?"); 10 | } 11 | 12 | abstract void answer1(); 13 | abstract void answer2(); 14 | abstract void answer3(); 15 | 16 | void result(){ 17 | question1(); 18 | answer1(); 19 | 20 | question2(); 21 | answer2(); 22 | 23 | question3(); 24 | answer3(); 25 | } 26 | } 27 | 28 | class APaper extends Paper{ 29 | 30 | @Override 31 | void answer1() { 32 | System.out.println("苏大强"); 33 | } 34 | 35 | @Override 36 | void answer2() { 37 | System.out.println("XX小学"); 38 | } 39 | 40 | @Override 41 | void answer3() { 42 | System.out.println("苏州"); 43 | } 44 | } 45 | 46 | class BPaper extends Paper{ 47 | 48 | @Override 49 | void answer1() { 50 | System.out.println("嬴政"); 51 | } 52 | 53 | @Override 54 | void answer2() { 55 | System.out.println("秦国"); 56 | } 57 | 58 | @Override 59 | void answer3() { 60 | System.out.println("赵国"); 61 | } 62 | } 63 | 64 | public class TemplateDemo { 65 | public static void main(String[] args) { 66 | new APaper().result(); 67 | new BPaper().result(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /09-原型模式/PrototypeDemo.java: -------------------------------------------------------------------------------- 1 | class Resume implements Cloneable{ 2 | private String name; 3 | private String sex; 4 | private String age; 5 | private String company; 6 | 7 | Resume(String name, String sex, String age, String company){ 8 | this.name = name; 9 | this.sex = sex; 10 | this.age = age; 11 | this.company = company; 12 | } 13 | 14 | public void setName(String name) { 15 | this.name = name; 16 | } 17 | 18 | public void setSex(String sex) { 19 | this.sex = sex; 20 | } 21 | 22 | public void setAge(String age) { 23 | this.age = age; 24 | } 25 | 26 | public void setCompany(String company) { 27 | this.company = company; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return name + sex + age + company; 33 | } 34 | 35 | @Override 36 | public Object clone() { 37 | Resume resume = null; 38 | try { 39 | resume = (Resume) super.clone(); 40 | } catch (CloneNotSupportedException e) { 41 | e.printStackTrace(); 42 | } 43 | return resume; 44 | } 45 | } 46 | 47 | public class PrototypeDemo { 48 | public static void main(String[] args) { 49 | Resume r1 = new Resume("大鸟", "男", "30", "天狗网"); 50 | System.out.println(r1); 51 | 52 | Resume r2 = (Resume) r1.clone(); 53 | r2.setName("菜鸟"); 54 | r2.setAge("21"); 55 | System.out.println(r2); 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /18-备忘录模式/MemntoDemo.java: -------------------------------------------------------------------------------- 1 | class Originator { 2 | private String state; 3 | 4 | public String getState() { 5 | return state; 6 | } 7 | 8 | public void setState(String state) { 9 | this.state = state; 10 | } 11 | 12 | public Memento createMemento(){ 13 | return new Memento(state); 14 | } 15 | 16 | public void setMemento(Memento memento) { 17 | state = memento.getState(); 18 | } 19 | 20 | public void show(){ 21 | System.out.println("state = "+state); 22 | } 23 | } 24 | 25 | class Memento { 26 | private String state; 27 | 28 | public Memento(String state) { 29 | this.state = state; 30 | } 31 | 32 | public String getState() { 33 | return state; 34 | } 35 | 36 | public void setState(String state) { 37 | this.state = state; 38 | } 39 | } 40 | 41 | class Caretaker { 42 | private Memento memento; 43 | 44 | public Memento getMemento() { 45 | return memento; 46 | } 47 | 48 | public void setMemento(Memento memento) { 49 | this.memento = memento; 50 | } 51 | } 52 | 53 | public class MemntoDemo { 54 | public static void main(String[] args) { 55 | Originator o = new Originator(); 56 | o.setState("On"); 57 | o.show(); 58 | 59 | Caretaker c = new Caretaker(); 60 | c.setMemento(o.createMemento()); 61 | o.setState("Off"); 62 | o.show(); 63 | 64 | o.setMemento(c.getMemento()); 65 | o.show(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /08-工厂方法模式/FactoryDemo.java: -------------------------------------------------------------------------------- 1 | interface LeiFeng { 2 | void sweep(); 3 | void wash(); 4 | void buyRice(); 5 | } 6 | class Undergraduate implements LeiFeng { 7 | @Override 8 | public void sweep() { 9 | System.out.println("大学生 帮忙 扫地"); 10 | } 11 | 12 | @Override 13 | public void wash() { 14 | System.out.println("大学生 帮忙 洗衣"); 15 | } 16 | 17 | @Override 18 | public void buyRice() { 19 | System.out.println("大学生 帮忙 买米"); 20 | } 21 | } 22 | class Volunteer implements LeiFeng { 23 | @Override 24 | public void sweep() { 25 | System.out.println("志愿者 帮忙 扫地"); 26 | } 27 | 28 | @Override 29 | public void wash() { 30 | System.out.println("志愿者 帮忙 洗衣"); 31 | } 32 | 33 | @Override 34 | public void buyRice() { 35 | System.out.println("志愿者 帮忙 买米"); 36 | } 37 | } 38 | interface IFactory { 39 | LeiFeng createLeiFeng(); 40 | } 41 | 42 | class UndergraduaateFactory implements IFactory { 43 | @Override 44 | public LeiFeng createLeiFeng() { 45 | return new Undergraduate(); 46 | } 47 | } 48 | class VolunteerFactory implements IFactory { 49 | @Override 50 | public LeiFeng createLeiFeng() { 51 | return new Volunteer(); 52 | } 53 | } 54 | 55 | public class FactoryDemo { 56 | public static void main(String[] args) { 57 | IFactory factory = new UndergraduaateFactory(); 58 | LeiFeng leiFeng1 = factory.createLeiFeng(); 59 | leiFeng1.sweep(); 60 | 61 | factory = new VolunteerFactory(); 62 | LeiFeng leiFeng2 = factory.createLeiFeng(); 63 | leiFeng2.buyRice(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /14-观察者模式/ObserverDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | abstract class Observer{ 5 | public abstract void update(); 6 | } 7 | abstract class Subject { 8 | private List observers = new ArrayList<>(); 9 | public void attach(Observer observer){ 10 | this.observers.add(observer); 11 | } 12 | public void dettach(Observer observer){ 13 | this.observers.remove(observer); 14 | } 15 | public void Notify(){ 16 | for(Observer observer : observers) { 17 | observer.update(); 18 | } 19 | } 20 | } 21 | class ConcreteSubject extends Subject { 22 | private String state; 23 | 24 | public String getState() { 25 | return state; 26 | } 27 | 28 | public void setState(String state) { 29 | this.state = state; 30 | } 31 | } 32 | class ConcreteObserver extends Observer { 33 | private String name; 34 | private String state; 35 | private ConcreteSubject subject; 36 | public ConcreteObserver(ConcreteSubject subject, String name){ 37 | this.subject = subject; 38 | this.name = name; 39 | } 40 | @Override 41 | public void update() { 42 | state = subject.getState(); 43 | System.out.println("观察者 "+name+" 的状态时 "+state); 44 | } 45 | } 46 | public class ObserverDemo { 47 | public static void main(String[] args) { 48 | ConcreteSubject s = new ConcreteSubject(); 49 | s.attach(new ConcreteObserver(s, "X")); 50 | s.attach(new ConcreteObserver(s, "Y")); 51 | s.attach(new ConcreteObserver(s, "Z")); 52 | 53 | s.setState("111"); 54 | s.Notify(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /07-代理模式/ProxyDemo.java: -------------------------------------------------------------------------------- 1 | interface IGiveGift { 2 | void GiveDolls(); 3 | void GiveFlowers(); 4 | void GiveChcolate(); 5 | } 6 | class Pursuit implements IGiveGift { 7 | 8 | SchoolGirl mm; 9 | 10 | public Pursuit(SchoolGirl mm) { 11 | this.mm = mm; 12 | } 13 | 14 | @Override 15 | public void GiveDolls() { 16 | System.out.println(mm.getName() + " 送你洋娃娃"); 17 | } 18 | 19 | @Override 20 | public void GiveFlowers() { 21 | System.out.println(mm.getName() + " 送你花"); 22 | } 23 | 24 | @Override 25 | public void GiveChcolate() { 26 | System.out.println(mm.getName() + " 送你巧克力"); 27 | } 28 | } 29 | 30 | class Proxy implements IGiveGift { 31 | 32 | Pursuit gg; 33 | 34 | public Proxy(SchoolGirl mm){ 35 | gg = new Pursuit(mm); 36 | } 37 | 38 | @Override 39 | public void GiveDolls() { 40 | System.out.println("借由proxy之手"); 41 | gg.GiveDolls(); 42 | } 43 | 44 | @Override 45 | public void GiveFlowers() { 46 | System.out.println("借由proxy之手"); 47 | gg.GiveFlowers(); 48 | } 49 | 50 | @Override 51 | public void GiveChcolate() { 52 | System.out.println("借由proxy之手"); 53 | gg.GiveChcolate(); 54 | } 55 | } 56 | 57 | class SchoolGirl { 58 | private String name; 59 | 60 | public String getName() { 61 | return name; 62 | } 63 | 64 | public void setName(String name) { 65 | this.name = name; 66 | } 67 | } 68 | 69 | public class ProxyDemo { 70 | public static void main(String[] args) { 71 | SchoolGirl jiaojiao = new SchoolGirl(); 72 | jiaojiao.setName("娇娇"); 73 | 74 | Proxy daili = new Proxy(jiaojiao); 75 | daili.GiveDolls(); 76 | daili.GiveFlowers(); 77 | daili.GiveChcolate(); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /25-中介者模式/MediatorDemo.java: -------------------------------------------------------------------------------- 1 | interface UnitedNations { 2 | public void declare(String message, Country country); 3 | } 4 | abstract class Country { 5 | protected UnitedNations mediator; 6 | 7 | public Country(UnitedNations mediator) { 8 | this.mediator = mediator; 9 | } 10 | } 11 | class USA extends Country { 12 | public USA(UnitedNations mediator) { 13 | super(mediator); 14 | } 15 | 16 | public void declare(String message) { 17 | mediator.declare(message, this); 18 | } 19 | 20 | public void getMessage(String message) { 21 | System.out.println("美国获得对方信息:"+message); 22 | } 23 | } 24 | class Iraq extends Country { 25 | public Iraq(UnitedNations mediator) { 26 | super(mediator); 27 | } 28 | 29 | public void declare(String message) { 30 | mediator.declare(message, this); 31 | } 32 | 33 | public void getMessage(String message) { 34 | System.out.println("伊拉克获得对方信息:"+message); 35 | } 36 | } 37 | class UnitedNationsSecurityCouncil implements UnitedNations { 38 | private USA country1; 39 | private Iraq country2; 40 | 41 | public void setCountries(USA country1, Iraq country2) { 42 | this.country1 = country1; 43 | this.country2 = country2; 44 | } 45 | 46 | @Override 47 | public void declare(String message, Country country) { 48 | if (country == this.country1) { 49 | this.country2.getMessage(message); 50 | }else{ 51 | this.country1.getMessage(message); 52 | } 53 | } 54 | } 55 | public class MediatorDemo { 56 | public static void main(String[] args) { 57 | 58 | UnitedNationsSecurityCouncil UNSC = new UnitedNationsSecurityCouncil(); 59 | 60 | USA c1 = new USA(UNSC); 61 | Iraq c2 = new Iraq(UNSC); 62 | 63 | UNSC.setCountries(c1, c2); 64 | 65 | c1.declare("不准制造核武器,否则发动战争!"); 66 | c2.declare("我们没有核武器,也不怕战争!"); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /13-建造者模式/BuilderDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | class Product { 5 | List parts = new ArrayList(); 6 | public void add(String part){ 7 | this.parts.add(part); 8 | } 9 | public void show(){ 10 | System.out.println("产品创建"); 11 | for(String part : parts) { 12 | System.out.println(part); 13 | } 14 | } 15 | } 16 | abstract class Builder { 17 | public abstract void buildPartA(); 18 | public abstract void buildPartB(); 19 | public abstract Product GetResult(); 20 | } 21 | class ConcreteBuilder1 extends Builder { 22 | private Product product = new Product(); 23 | @Override 24 | public void buildPartA() { 25 | product.add("部件A"); 26 | } 27 | @Override 28 | public void buildPartB() { 29 | product.add("部件B"); 30 | } 31 | @Override 32 | public Product GetResult() { 33 | return product; 34 | } 35 | } 36 | class ConcreteBuilder2 extends Builder { 37 | private Product product = new Product(); 38 | @Override 39 | public void buildPartA() { 40 | product.add("部件1"); 41 | } 42 | @Override 43 | public void buildPartB() { 44 | product.add("部件2"); 45 | } 46 | @Override 47 | public Product GetResult() { 48 | return product; 49 | } 50 | } 51 | class Director { 52 | public void construct(Builder builder) { 53 | builder.buildPartA(); 54 | builder.buildPartB(); 55 | } 56 | } 57 | public class BuilderDemo { 58 | public static void main(String[] args) { 59 | Director director = new Director(); 60 | 61 | Builder b1 = new ConcreteBuilder1(); 62 | director.construct(b1); 63 | Product p1 = b1.GetResult(); 64 | p1.show(); 65 | 66 | Builder b2 = new ConcreteBuilder2(); 67 | director.construct(b2); 68 | Product p2 = b2.GetResult(); 69 | p2.show(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /09-原型模式/PrototypeDemo3.java: -------------------------------------------------------------------------------- 1 | 2 | class Resume3 implements Cloneable{ 3 | private String name; 4 | private String sex; 5 | private String age; 6 | private String company; 7 | private Address address; 8 | 9 | Resume3(String name, String sex, String age, String company, String address){ 10 | this.name = name; 11 | this.sex = sex; 12 | this.age = age; 13 | this.company = company; 14 | this.address = new Address(address); 15 | } 16 | 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | 21 | public void setSex(String sex) { 22 | this.sex = sex; 23 | } 24 | 25 | public void setAge(String age) { 26 | this.age = age; 27 | } 28 | 29 | public void setCompany(String company) { 30 | this.company = company; 31 | } 32 | 33 | public void setAddress(Address address) { 34 | this.address = address; 35 | } 36 | 37 | public void updateAddress(String address) { 38 | this.address.setHome(address); 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return name + sex + age + company + address; 44 | } 45 | 46 | @Override 47 | public Object clone() { 48 | Resume3 resume = null; 49 | try { 50 | resume = (Resume3) super.clone(); 51 | resume.setAddress((Address)this.address.clone()); 52 | 53 | } catch (CloneNotSupportedException e) { 54 | e.printStackTrace(); 55 | } 56 | return resume; 57 | } 58 | } 59 | 60 | public class PrototypeDemo3 { 61 | public static void main(String[] args) { 62 | Resume3 r1 = new Resume3("大鸟", "男", "30", "天狗网", "大连甘井子"); 63 | 64 | 65 | Resume3 r2 = (Resume3) r1.clone(); 66 | r2.setName("菜鸟"); 67 | r2.setAge("21"); 68 | r2.updateAddress("大连高新"); 69 | 70 | System.out.println(r1); 71 | System.out.println(r2); 72 | 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /02-策略模式/Demo.java: -------------------------------------------------------------------------------- 1 | interface CashSuper { 2 | Double acceptCash(Double money); 3 | } 4 | 5 | class CashNormal implements CashSuper { 6 | @Override 7 | public Double acceptCash(Double money) { 8 | return money; 9 | } 10 | } 11 | 12 | class CashRebate implements CashSuper { 13 | private Double ratio; 14 | public CashRebate(Double ratio){ 15 | this.ratio = ratio; 16 | } 17 | @Override 18 | public Double acceptCash(Double money) { 19 | return money * ratio; 20 | } 21 | } 22 | 23 | class CashReturn implements CashSuper { 24 | private Double total; 25 | private Double sub; 26 | public CashReturn(Double total, Double sub){ 27 | this.total = total; 28 | this.sub = sub; 29 | } 30 | 31 | @Override 32 | public Double acceptCash(Double money) { 33 | if (money > this.total) { 34 | return money - this.sub; 35 | } 36 | return money; 37 | } 38 | } 39 | 40 | class CashContext { 41 | private CashSuper cs; 42 | 43 | public CashContext(String type){ 44 | switch (type) { 45 | case "正常收费": 46 | cs = new CashNormal(); 47 | break; 48 | case "满300返100": 49 | cs = new CashReturn(300d, 100d); 50 | break; 51 | case "打八折": 52 | cs = new CashRebate(0.8); 53 | break; 54 | default: 55 | //NOTHING 56 | } 57 | } 58 | 59 | public Double getResult(Double money){ 60 | return cs.acceptCash(money); 61 | } 62 | 63 | } 64 | 65 | public class Demo { 66 | public static void main(String[] args) { 67 | CashContext cc = new CashContext("打八折"); 68 | System.out.println(cc.getResult(400d)); 69 | 70 | cc = new CashContext("满300返100"); 71 | System.out.println(cc.getResult(400d)); 72 | 73 | cc = new CashContext("正常收费"); 74 | System.out.println(cc.getResult(400d)); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /26-享元模式/FlyWeightDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | class User1{ 4 | private String name; 5 | 6 | public User1(String name) { 7 | this.name = name; 8 | } 9 | 10 | public String getName() { 11 | return name; 12 | } 13 | 14 | public void setName(String name) { 15 | this.name = name; 16 | } 17 | } 18 | interface WebSite { 19 | public void use(User1 user); 20 | } 21 | class ConcreteWebSite implements WebSite { 22 | private String name = ""; 23 | 24 | public ConcreteWebSite(String name) { 25 | this.name = name; 26 | } 27 | 28 | @Override 29 | public void use(User1 user) { 30 | System.out.println("网站分类:"+name+" 用户:"+user.getName()); 31 | } 32 | } 33 | class WebSiteFactory { 34 | private HashMap flyweights = new HashMap<>(); 35 | 36 | public WebSite getWebSiteCategory(String key){ 37 | if (!flyweights.containsKey(key)){ 38 | flyweights.put(key, new ConcreteWebSite(key)); 39 | } 40 | return flyweights.get(key); 41 | } 42 | 43 | public Integer getWebSiteCount(){ 44 | return flyweights.size(); 45 | } 46 | } 47 | public class FlyWeightDemo { 48 | public static void main(String[] args) { 49 | WebSiteFactory factory = new WebSiteFactory(); 50 | 51 | WebSite fx = factory.getWebSiteCategory("产品展示"); 52 | fx.use(new User1("小菜")); 53 | 54 | WebSite fy = factory.getWebSiteCategory("产品展示"); 55 | fy.use(new User1("大鸟")); 56 | 57 | WebSite fz = factory.getWebSiteCategory("产品展示"); 58 | fz.use(new User1("娇娇")); 59 | 60 | WebSite fl = factory.getWebSiteCategory("博客"); 61 | fl.use(new User1("老顽童")); 62 | 63 | WebSite fm = factory.getWebSiteCategory("博客"); 64 | fm.use(new User1("桃谷六仙")); 65 | 66 | WebSite fn = factory.getWebSiteCategory("博客"); 67 | fn.use(new User1("南海鳄神")); 68 | 69 | System.out.println("得到网站分类总数为:"+factory.getWebSiteCount()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /06-装饰模式/DecoratorDemo.java: -------------------------------------------------------------------------------- 1 | class Person{ 2 | private String name; 3 | public Person(){} 4 | public Person(String name){ 5 | this.name = name; 6 | } 7 | public void show(){ 8 | System.out.println("装扮的"+name); 9 | } 10 | } 11 | class Finery extends Person { 12 | protected Person component; 13 | public void decorate(Person component){ 14 | this.component = component; 15 | } 16 | @Override 17 | public void show(){ 18 | if (component != null) { 19 | component.show(); 20 | } 21 | } 22 | } 23 | class TShirts extends Finery { 24 | @Override 25 | public void show(){ 26 | System.out.println("T恤"); 27 | super.show(); 28 | } 29 | } 30 | class BigTrousers extends Finery { 31 | @Override 32 | public void show(){ 33 | System.out.println("垮裤"); 34 | super.show(); 35 | } 36 | } 37 | class Sneaker extends Finery { 38 | @Override 39 | public void show(){ 40 | System.out.println("破球鞋"); 41 | super.show(); 42 | } 43 | } 44 | class LeatherShoes extends Finery { 45 | @Override 46 | public void show(){ 47 | System.out.println("皮鞋"); 48 | super.show(); 49 | } 50 | } 51 | class Tie extends Finery { 52 | @Override 53 | public void show() { 54 | System.out.println("领带"); 55 | super.show(); 56 | } 57 | } 58 | class Suit extends Finery { 59 | @Override 60 | public void show() { 61 | System.out.println("西装"); 62 | super.show(); 63 | } 64 | } 65 | public class DecoratorDemo { 66 | public static void main(String[] args) { 67 | // 为已有的方法,动态的添加其他的功能 68 | 69 | Person xc = new Person("小菜"); 70 | System.out.println("第一种装扮"); 71 | 72 | Sneaker f1 = new Sneaker(); 73 | BigTrousers f2 = new BigTrousers(); 74 | TShirts f3 = new TShirts(); 75 | 76 | f1.decorate(xc); 77 | f2.decorate(f1); 78 | f3.decorate(f2); 79 | f3.show(); 80 | 81 | 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /01-简单工厂模式/SimpleFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 抽象类 3 | */ 4 | abstract class Operator { 5 | private Integer num1; 6 | private Integer num2; 7 | 8 | public Integer getNum1() { 9 | return num1; 10 | } 11 | 12 | public void setNum1(Integer num1) { 13 | this.num1 = num1; 14 | } 15 | 16 | public Integer getNum2() { 17 | return num2; 18 | } 19 | 20 | public void setNum2(Integer num2) { 21 | this.num2 = num2; 22 | } 23 | 24 | abstract public Integer getResult(); 25 | } 26 | 27 | class Add extends Operator { 28 | @Override 29 | public Integer getResult() { 30 | return getNum1() + getNum2(); 31 | } 32 | } 33 | 34 | class Sub extends Operator { 35 | @Override 36 | public Integer getResult() { 37 | return getNum1() - getNum2(); 38 | } 39 | } 40 | 41 | class Mul extends Operator { 42 | @Override 43 | public Integer getResult() { 44 | return getNum1() * getNum2(); 45 | } 46 | } 47 | 48 | class Div extends Operator { 49 | @Override 50 | public Integer getResult() { 51 | return getNum1() / getNum2(); 52 | } 53 | } 54 | 55 | class OperationFactory { 56 | 57 | public static Operator createOperator(String operator){ 58 | Operator op = null; 59 | 60 | switch (operator) { 61 | case "+": 62 | op = new Add(); 63 | break; 64 | case "-": 65 | op = new Sub(); 66 | break; 67 | case "*": 68 | op = new Mul(); 69 | break; 70 | case "/": 71 | op = new Div(); 72 | break; 73 | default: 74 | // NOTHING 75 | } 76 | return op; 77 | } 78 | } 79 | 80 | public class SimpleFactory { 81 | public static void main(String[] args) { 82 | Operator op = OperationFactory.createOperator("+"); 83 | op.setNum1(10); 84 | op.setNum2(5); 85 | System.out.println(op.getResult()); 86 | 87 | op = OperationFactory.createOperator("*"); 88 | op.setNum1(10); 89 | op.setNum2(5); 90 | System.out.println(op.getResult()); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /20-迭代器模式/IteratorDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | abstract class Iterator { 5 | public abstract Object first(); 6 | public abstract Object next(); 7 | public abstract Boolean isDone(); 8 | public abstract Object current(); 9 | } 10 | abstract class Aggregate { 11 | public abstract Iterator createIterator(); 12 | } 13 | class ConcreteIterator extends Iterator { 14 | 15 | private ConcreteAggregate aggregate; 16 | private Integer current = 0; 17 | 18 | public ConcreteIterator(ConcreteAggregate aggregate) { 19 | this.aggregate = aggregate; 20 | } 21 | 22 | @Override 23 | public Object first() { 24 | return this.aggregate.get(0); 25 | } 26 | 27 | @Override 28 | public Object next() { 29 | Object ret = null; 30 | current++; 31 | if (current < aggregate.size()) { 32 | ret = aggregate.get(current); 33 | } 34 | return ret; 35 | } 36 | 37 | @Override 38 | public Boolean isDone() { 39 | return current >= aggregate.size(); 40 | } 41 | 42 | @Override 43 | public Object current() { 44 | return this.aggregate.get(current); 45 | } 46 | } 47 | class ConcreteAggregate extends Aggregate { 48 | private List items = new ArrayList<>(); 49 | 50 | @Override 51 | public Iterator createIterator() { 52 | return new ConcreteIterator(this); 53 | } 54 | public Object get(Integer index){ 55 | return items.get(index); 56 | } 57 | public void add(Object o){ 58 | this.items.add(o); 59 | } 60 | public Integer size(){ 61 | return items.size(); 62 | } 63 | } 64 | public class IteratorDemo { 65 | public static void main(String[] args) { 66 | ConcreteAggregate a = new ConcreteAggregate(); 67 | a.add("大鸟"); 68 | a.add("小菜"); 69 | a.add("行李"); 70 | a.add("老外"); 71 | a.add("内部员工"); 72 | a.add("小偷"); 73 | 74 | Iterator i = a.createIterator(); 75 | 76 | Object item = i.first(); 77 | 78 | while(!i.isDone()){ 79 | System.out.println(i.current()); 80 | i.next(); 81 | } 82 | 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /28-访问者模式/VisitorDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | interface Visitor{ 5 | public void visitConcreteElementA(ConcreteElementA concreteElementA); 6 | public void visitConcreteElementB(ConcreteElementB concreteElementB); 7 | } 8 | class ConcreteVisitor1 implements Visitor { 9 | @Override 10 | public void visitConcreteElementA(ConcreteElementA concreteElementA) { 11 | 12 | } 13 | 14 | @Override 15 | public void visitConcreteElementB(ConcreteElementB concreteElementB) { 16 | 17 | } 18 | } 19 | class ConcreteVisitor2 implements Visitor { 20 | @Override 21 | public void visitConcreteElementA(ConcreteElementA concreteElementA) { 22 | 23 | } 24 | 25 | @Override 26 | public void visitConcreteElementB(ConcreteElementB concreteElementB) { 27 | 28 | } 29 | } 30 | interface Element { 31 | public void accept(Visitor visitor); 32 | } 33 | class ConcreteElementA implements Element { 34 | @Override 35 | public void accept(Visitor visitor) { 36 | visitor.visitConcreteElementA(this); 37 | } 38 | public void operationA(){ 39 | 40 | } 41 | } 42 | class ConcreteElementB implements Element { 43 | @Override 44 | public void accept(Visitor visitor) { 45 | visitor.visitConcreteElementB(this); 46 | } 47 | public void operationB(){ 48 | 49 | } 50 | } 51 | class ObjectStructure { 52 | private List elementList = new ArrayList<>(); 53 | 54 | public void attach(Element element) { 55 | elementList.add(element); 56 | } 57 | 58 | public void detach(Element element) { 59 | elementList.remove(element); 60 | } 61 | 62 | public void accept(Visitor visitor){ 63 | for(Element e : elementList) { 64 | e.accept(visitor); 65 | } 66 | } 67 | } 68 | public class VisitorDemo { 69 | public static void main(String[] args) { 70 | ObjectStructure o = new ObjectStructure(); 71 | 72 | o.attach(new ConcreteElementA()); 73 | o.attach(new ConcreteElementB()); 74 | 75 | ConcreteVisitor1 v1 = new ConcreteVisitor1(); 76 | ConcreteVisitor2 v2 = new ConcreteVisitor2(); 77 | 78 | o.accept(v1); 79 | o.accept(v2); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /09-原型模式/PrototypeDemo2.java: -------------------------------------------------------------------------------- 1 | 2 | class Address implements Cloneable{ 3 | String home; 4 | 5 | public void setHome(String home) { 6 | this.home = home; 7 | } 8 | 9 | Address(String home){ 10 | this.home = home; 11 | } 12 | 13 | @Override 14 | public String toString() { 15 | return home; 16 | } 17 | 18 | @Override 19 | protected Object clone() throws CloneNotSupportedException { 20 | return super.clone(); 21 | } 22 | } 23 | class Resume2 implements Cloneable{ 24 | private String name; 25 | private String sex; 26 | private String age; 27 | private String company; 28 | private Address address; 29 | 30 | Resume2(String name, String sex, String age, String company, String address){ 31 | this.name = name; 32 | this.sex = sex; 33 | this.age = age; 34 | this.company = company; 35 | this.address = new Address(address); 36 | } 37 | 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | public void setSex(String sex) { 43 | this.sex = sex; 44 | } 45 | 46 | public void setAge(String age) { 47 | this.age = age; 48 | } 49 | 50 | public void setCompany(String company) { 51 | this.company = company; 52 | } 53 | 54 | public void setAddress(String address) { 55 | this.address.setHome(address); 56 | } 57 | 58 | @Override 59 | public String toString() { 60 | return name + sex + age + company + address; 61 | } 62 | 63 | @Override 64 | public Object clone() { 65 | Resume2 resume = null; 66 | try { 67 | resume = (Resume2) super.clone(); 68 | } catch (CloneNotSupportedException e) { 69 | e.printStackTrace(); 70 | } 71 | return resume; 72 | } 73 | } 74 | 75 | public class PrototypeDemo2 { 76 | public static void main(String[] args) { 77 | Resume2 r1 = new Resume2("大鸟", "男", "30", "天狗网", "大连甘井子"); 78 | 79 | 80 | Resume2 r2 = (Resume2) r1.clone(); 81 | r2.setName("菜鸟"); 82 | r2.setAge("21"); 83 | r2.setAddress("大连高新"); 84 | 85 | System.out.println(r1); 86 | System.out.println(r2); 87 | 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /09-原型模式/PrototypeDemo4.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | class Resume4 implements Cloneable{ 4 | private String name; 5 | private String sex; 6 | private String age; 7 | private String company; 8 | private Address address; 9 | 10 | Resume4(String name, String sex, String age, String company, String address){ 11 | this.name = name; 12 | this.sex = sex; 13 | this.age = age; 14 | this.company = company; 15 | this.address = new Address(address); 16 | } 17 | 18 | public void setName(String name) { 19 | this.name = name; 20 | } 21 | 22 | public void setSex(String sex) { 23 | this.sex = sex; 24 | } 25 | 26 | public void setAge(String age) { 27 | this.age = age; 28 | } 29 | 30 | public void setCompany(String company) { 31 | this.company = company; 32 | } 33 | 34 | public void setAddress(Address address) { 35 | this.address = address; 36 | } 37 | 38 | public void updateAddress(String address) { 39 | this.address.setHome(address); 40 | } 41 | 42 | @Override 43 | public String toString() { 44 | return name + sex + age + company + address; 45 | } 46 | 47 | @Override 48 | public Object clone() { 49 | Resume4 resume = null; 50 | try { 51 | 52 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 53 | ObjectOutputStream oos = new ObjectOutputStream(baos); 54 | oos.writeObject(this); 55 | 56 | // 将流序列化成对象 57 | ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 58 | ObjectInputStream ois = new ObjectInputStream(bais); 59 | resume = (Resume4) ois.readObject(); 60 | } catch (IOException | ClassNotFoundException e) { 61 | e.printStackTrace(); 62 | } 63 | return resume; 64 | } 65 | } 66 | 67 | public class PrototypeDemo4 { 68 | public static void main(String[] args) { 69 | Resume3 r1 = new Resume3("大鸟", "男", "30", "天狗网", "大连甘井子"); 70 | 71 | 72 | Resume3 r2 = (Resume3) r1.clone(); 73 | r2.setName("菜鸟"); 74 | r2.setAge("21"); 75 | r2.updateAddress("大连高新"); 76 | 77 | System.out.println(r1); 78 | System.out.println(r2); 79 | 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /19-组合模式/CompositeDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | abstract class Component { 5 | protected String name; 6 | 7 | public Component(String name) { 8 | this.name = name; 9 | } 10 | 11 | public abstract void add(Component c); 12 | public abstract void remove(Component c); 13 | public abstract void display(int depth); 14 | } 15 | class Leaf extends Component { 16 | public Leaf(String name) { 17 | super(name); 18 | } 19 | 20 | @Override 21 | public void add(Component c) { 22 | System.out.println("can not add to a leaf"); 23 | } 24 | 25 | @Override 26 | public void remove(Component c) { 27 | System.out.println("can not remove from a leaf"); 28 | } 29 | 30 | @Override 31 | public void display(int depth) { 32 | String prefix = ""; 33 | for (int i =0;i children = new ArrayList<>(); 42 | 43 | public Composite(String name) { 44 | super(name); 45 | } 46 | 47 | @Override 48 | public void add(Component c) { 49 | this.children.add(c); 50 | } 51 | 52 | @Override 53 | public void remove(Component c) { 54 | this.children.remove(c); 55 | } 56 | 57 | @Override 58 | public void display(int depth) { 59 | String prefix = ""; 60 | for (int i =0;i= 1000) { 88 | speed = "慢速"; 89 | } else { 90 | speed = "中速"; 91 | } 92 | System.out.println(speed + " "); 93 | } 94 | } 95 | public class InterpreterDemo { 96 | public static void main(String[] args) { 97 | PlayContext context = new PlayContext(); 98 | System.out.println("上海滩:"); 99 | context.setText("T 500 O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2"); 100 | Expression expression = null; 101 | 102 | try { 103 | while (context.getText().length() > 0) { 104 | String str = context.getText().substring(0, 1); 105 | 106 | switch (str){ 107 | case "O": 108 | expression = new Scale(); 109 | break; 110 | case "T": 111 | expression = new Speed(); 112 | break; 113 | case "C": 114 | case "D": 115 | case "E": 116 | case "F": 117 | case "G": 118 | case "A": 119 | case "B": 120 | case "P": 121 | expression = new Note(); 122 | break; 123 | default: 124 | // nothing 125 | } 126 | 127 | expression.interpret(context); 128 | } 129 | }catch (Exception e){ 130 | System.out.println(e.getMessage()); 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /24-职责链模式/ChainOfResponsibilityDemo.java: -------------------------------------------------------------------------------- 1 | abstract class Manager { 2 | protected String name; 3 | protected Manager superior; 4 | 5 | public Manager(String name) { 6 | this.name = name; 7 | } 8 | 9 | public void setSuperior(Manager superior){ 10 | this.superior = superior; 11 | } 12 | 13 | abstract public void requestApplications(Request request); 14 | } 15 | class CommonManager extends Manager { 16 | public CommonManager(String name) { 17 | super(name); 18 | } 19 | 20 | @Override 21 | public void requestApplications(Request request) { 22 | if ("请假".equals(request.getRequestType()) && request.getNumber() <= 2) { 23 | System.out.println(name + ":" + request.getRequestContent() + "数量"+request.getNumber()+"被批准"); 24 | } else { 25 | if (superior != null) { 26 | superior.requestApplications(request); 27 | } 28 | } 29 | } 30 | } 31 | class Majordomo extends Manager { 32 | public Majordomo(String name) { 33 | super(name); 34 | } 35 | 36 | @Override 37 | public void requestApplications(Request request) { 38 | if ("请假".equals(request.getRequestType()) && request.getNumber() <= 5) { 39 | System.out.println(name + ":" + request.getRequestContent() + "数量"+request.getNumber()+"被批准"); 40 | } else { 41 | if (superior != null) { 42 | superior.requestApplications(request); 43 | } 44 | } 45 | } 46 | } 47 | class GeneralManager extends Manager { 48 | public GeneralManager(String name) { 49 | super(name); 50 | } 51 | 52 | @Override 53 | public void requestApplications(Request request) { 54 | if ("请假".equals(request.getRequestType())) { 55 | System.out.println(name + ":" + request.getRequestContent() + "数量"+request.getNumber()+"被批准"); 56 | } else if ("加薪".equals(request.getRequestType()) && request.getNumber() <= 500) { 57 | System.out.println(name + ":" + request.getRequestContent() + "数量"+request.getNumber()+"被批准"); 58 | } else if ("加薪".equals(request.getRequestType()) && request.getNumber() > 500) { 59 | System.out.println(name + ":" + request.getRequestContent() + "数量"+request.getNumber()+"再说吧"); 60 | } 61 | } 62 | } 63 | class Request { 64 | private String requestType; 65 | private Integer number; 66 | private String requestContent; 67 | 68 | public String getRequestContent() { 69 | return requestContent; 70 | } 71 | 72 | public void setRequestContent(String requestContent) { 73 | this.requestContent = requestContent; 74 | } 75 | 76 | public Integer getNumber() { 77 | return number; 78 | } 79 | 80 | public void setNumber(Integer number) { 81 | this.number = number; 82 | } 83 | 84 | public String getRequestType() { 85 | return requestType; 86 | } 87 | 88 | public void setRequestType(String requestType) { 89 | this.requestType = requestType; 90 | } 91 | 92 | } 93 | 94 | public class ChainOfResponsibilityDemo { 95 | public static void main(String[] args) { 96 | CommonManager jinli = new CommonManager("经理"); 97 | Majordomo zongjian = new Majordomo("总监"); 98 | GeneralManager zongjingli = new GeneralManager("总经理"); 99 | 100 | jinli.setSuperior(zongjian); 101 | zongjian.setSuperior(zongjingli); 102 | 103 | Request request = new Request(); 104 | request.setRequestType("请假"); 105 | request.setRequestContent("小菜请假"); 106 | request.setNumber(1); 107 | jinli.requestApplications(request); 108 | 109 | Request request2 = new Request(); 110 | request2.setRequestType("请假"); 111 | request2.setRequestContent("小菜请假"); 112 | request2.setNumber(4); 113 | jinli.requestApplications(request2); 114 | 115 | Request request3 = new Request(); 116 | request3.setRequestType("加薪"); 117 | request3.setRequestContent("小菜请求加薪"); 118 | request3.setNumber(500); 119 | jinli.requestApplications(request3); 120 | 121 | Request request4 = new Request(); 122 | request4.setRequestType("加薪"); 123 | request4.setRequestContent("小菜请求加薪"); 124 | request4.setNumber(1000); 125 | jinli.requestApplications(request4); 126 | } 127 | } 128 | --------------------------------------------------------------------------------