├── AbstractFactoryPattern
├── .classpath
├── .project
└── src
│ └── example
│ ├── AK.java
│ ├── AKBullet.java
│ ├── AK_Factory.java
│ ├── Factory.java
│ ├── FactoryTest.java
│ ├── IBullet.java
│ ├── IGun.java
│ ├── M4A1.java
│ ├── M4A1Bullet.java
│ └── M4A1_Factory.java
├── AdapterPattern
├── .classpath
├── .project
└── src
│ └── adapterpattern
│ ├── Adaptee.java
│ ├── Adapter.java
│ ├── Client.java
│ ├── ConcreteTarget.java
│ └── Target.java
├── BuilderPattern
├── .classpath
├── .project
└── src
│ ├── builderpattern
│ ├── Builder.java
│ ├── ConcreteProduct.java
│ ├── Dicector.java
│ └── Product.java
│ └── example
│ ├── Builder.java
│ ├── Client.java
│ ├── Computer.java
│ ├── ConcreteBuilder.java
│ └── Director.java
├── ChainOfResponsibilityPattern
├── .classpath
├── .project
└── src
│ ├── ChainOfResponsibilityPattern
│ ├── Client.java
│ ├── ConcreteHandler1.java
│ ├── ConcreteHandler2.java
│ ├── ConcreteHandler3.java
│ ├── Handler.java
│ ├── Level.java
│ ├── Request.java
│ └── Response.java
│ └── example
│ ├── Client.java
│ ├── GeneralManager.java
│ ├── Handler.java
│ ├── Manager.java
│ └── TeamLeader.java
├── CommandPattern
├── .classpath
├── .project
└── src
│ ├── commandpattern
│ ├── Client.java
│ ├── Command.java
│ ├── ConcreteCommand1.java
│ ├── ConcreteCommand2.java
│ ├── ConcreteReceiver1.java
│ ├── ConcreteReceiver2.java
│ ├── Invoker.java
│ └── Receiver.java
│ └── example
│ ├── Client.java
│ ├── Command.java
│ ├── Consumer.java
│ ├── Cook.java
│ ├── CookA.java
│ └── CookCommand.java
├── DecoratorPattern
├── .classpath
├── .project
└── src
│ ├── decoratorpattern
│ ├── Client.java
│ ├── Component.java
│ ├── ConcreateComponent.java
│ ├── ConcreteDecorator1.java
│ ├── ConcreteDecorator2.java
│ └── Decorator.java
│ └── example
│ ├── Client.java
│ └── ToUpperCaseInputStream.java
├── FactoryPattern
├── .classpath
├── .project
└── src
│ ├── example
│ ├── Circle.java
│ ├── Client.java
│ ├── Factory.java
│ ├── IShape.java
│ ├── IShapeFactory.java
│ ├── Rectangle.java
│ └── Square.java
│ └── factorypattern
│ ├── Client.java
│ ├── Factory.java
│ ├── IFactory.java
│ ├── IProduct.java
│ └── Product.java
├── IteratorPattern
├── .classpath
├── .project
└── src
│ └── iteratorpattern
│ ├── Aggregate.java
│ ├── Client.java
│ ├── ConcreteAggregate.java
│ ├── ConcreteIterator.java
│ └── Iterator.java
├── MediatorPattern
├── .classpath
├── .project
└── src
│ ├── example
│ ├── Champions.java
│ ├── Client.java
│ ├── Emei.java
│ ├── Shaolin.java
│ ├── United.java
│ ├── Wudang.java
│ └── WulinAlliance.java
│ └── mediatorpattern
│ ├── Colleague.java
│ ├── ConcreteColleague1.java
│ ├── ConcreteColleague2.java
│ ├── ConcreteMediator.java
│ └── Mediator.java
├── ObserverPattern
├── .classpath
├── .project
└── src
│ ├── example
│ ├── Client.java
│ ├── Observer.java
│ ├── Subject.java
│ ├── baisheng.java
│ ├── shengchengang.java
│ ├── wuyong.java
│ └── yangzhi.java
│ └── observerpattern
│ ├── Client.java
│ ├── ConcreteObserver.java
│ ├── ConcreteSubject.java
│ ├── Observer.java
│ └── Subject.java
├── PrototypePattern
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
├── bin
│ ├── example
│ │ ├── Client.class
│ │ ├── Message.class
│ │ └── Temeplate.class
│ └── prototypepattern
│ │ ├── Client.class
│ │ ├── ConcretePrototype.class
│ │ └── Prototype.class
└── src
│ ├── example
│ ├── Client.java
│ ├── Message.java
│ └── Temeplate.java
│ └── prototypepattern
│ ├── Client.java
│ ├── ConcretePrototype.java
│ └── Prototype.java
├── ProxyPattern
├── .classpath
├── .project
└── src
│ ├── example
│ ├── Client.java
│ ├── DynamicProxyHandler.java
│ ├── LianJia.java
│ ├── RealSubject.java
│ └── Subject.java
│ └── proxypattern
│ ├── Proxy.java
│ ├── RealSubject.java
│ └── Subject.java
├── README.md
├── SingeltonPattern
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
└── src
│ └── singletonpattern
│ └── SingletonPattern.java
├── StrategyPattern
├── .classpath
├── .project
└── src
│ ├── StrategyPattern
│ ├── Client.java
│ ├── ConcreteStrategy1.java
│ ├── ConcreteStrategy2.java
│ ├── Context.java
│ └── Strategy.java
│ └── example
│ ├── BackDoor.java
│ ├── BlackEnemy.java
│ ├── Context.java
│ ├── GivenGreenLight.java
│ ├── Strategy.java
│ └── Zhaoyun.java
└── TemplateMethodPattern
├── .classpath
├── .project
└── src
├── example
├── Client.java
├── DNFHelper.java
├── PlayerA.java
└── PlayerB.java
└── templatemethod
├── AbstractClass.java
├── Clinet.java
└── ConcreteClass.java
/AbstractFactoryPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | AbstractFactoryPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/example/AK.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class AK implements IGun{
4 |
5 | @Override
6 | public void shoot() {
7 |
8 | System.out.println("AK shoot...");
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/example/AKBullet.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class AKBullet implements IBullet{
4 |
5 | @Override
6 | public void load() {
7 | System.out.println("load AK bullet...");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/example/AK_Factory.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class AK_Factory implements Factory{
4 |
5 | @Override
6 | public IGun productGun() {
7 | return new AK();
8 | }
9 |
10 | @Override
11 | public IBullet productBullet() {
12 | return new AKBullet();
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/example/Factory.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public interface Factory {
4 |
5 | IGun productGun();
6 | IBullet productBullet();
7 | }
8 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/example/FactoryTest.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 |
4 | import org.junit.Test;
5 |
6 | public class FactoryTest {
7 |
8 | @Test
9 | public void test() {
10 | Factory factory = new AK_Factory();
11 | IBullet bullet = factory.productBullet();
12 | IGun gun = factory.productGun();
13 | bullet.load();
14 | gun.shoot();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/example/IBullet.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public interface IBullet {
4 |
5 | void load();
6 | }
7 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/example/IGun.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public interface IGun {
4 |
5 | void shoot();
6 | }
7 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/example/M4A1.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class M4A1 implements IGun {
4 |
5 | @Override
6 | public void shoot() {
7 |
8 | System.out.println("M4A1 shoot...");
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/example/M4A1Bullet.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class M4A1Bullet implements IBullet{
4 |
5 | @Override
6 | public void load() {
7 | System.out.println("load M4A1 bullet...");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/example/M4A1_Factory.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class M4A1_Factory implements Factory{
4 |
5 | @Override
6 | public IGun productGun() {
7 | return new M4A1();
8 | }
9 |
10 | @Override
11 | public IBullet productBullet() {
12 | return new M4A1Bullet();
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/AdapterPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/AdapterPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | AdapterPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/AdapterPattern/src/adapterpattern/Adaptee.java:
--------------------------------------------------------------------------------
1 | package adapterpattern;
2 |
3 | public class Adaptee {
4 |
5 | public void doSth() {
6 | System.out.println("this is Adaptee");
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/AdapterPattern/src/adapterpattern/Adapter.java:
--------------------------------------------------------------------------------
1 | package adapterpattern;
2 |
3 | public class Adapter extends Adaptee implements Target{
4 |
5 | @Override
6 | public void request() {
7 |
8 | super.doSth();
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/AdapterPattern/src/adapterpattern/Client.java:
--------------------------------------------------------------------------------
1 | package adapterpattern;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 |
7 | Target target = new ConcreteTarget();
8 | target.request();
9 | Target target1 = new Adapter();
10 | target1.request();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/AdapterPattern/src/adapterpattern/ConcreteTarget.java:
--------------------------------------------------------------------------------
1 | package adapterpattern;
2 |
3 | public class ConcreteTarget implements Target{
4 |
5 | @Override
6 | public void request() {
7 | System.out.println("this is ConcreteTarget");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/AdapterPattern/src/adapterpattern/Target.java:
--------------------------------------------------------------------------------
1 | package adapterpattern;
2 |
3 | public interface Target {
4 |
5 | void request();
6 | }
7 |
--------------------------------------------------------------------------------
/BuilderPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/BuilderPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | BuilderPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/BuilderPattern/src/builderpattern/Builder.java:
--------------------------------------------------------------------------------
1 | package builderpattern;
2 |
3 | public abstract class Builder {
4 |
5 | public abstract void setPart();
6 |
7 | public abstract Product buildProduct();
8 | }
9 |
--------------------------------------------------------------------------------
/BuilderPattern/src/builderpattern/ConcreteProduct.java:
--------------------------------------------------------------------------------
1 | package builderpattern;
2 |
3 | public class ConcreteProduct extends Builder {
4 |
5 | private Product product = new Product();
6 |
7 | @Override
8 | public void setPart() {
9 | // TODO Auto-generated method stub
10 |
11 | }
12 |
13 | @Override
14 | public Product buildProduct() {
15 | // TODO Auto-generated method stub
16 | return product;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/BuilderPattern/src/builderpattern/Dicector.java:
--------------------------------------------------------------------------------
1 | package builderpattern;
2 |
3 | public class Dicector {
4 |
5 | private Builder builder = new ConcreteProduct();
6 |
7 | public Product getAProduct() {
8 |
9 | builder.setPart();
10 | // 业务逻辑
11 | return builder.buildProduct();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/BuilderPattern/src/builderpattern/Product.java:
--------------------------------------------------------------------------------
1 | package builderpattern;
2 |
3 | public class Product {
4 |
5 | public void doSth() {
6 | // TODO
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/BuilderPattern/src/example/Builder.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public abstract class Builder {
4 |
5 | // 第一步:装CPU
6 | // 声明为抽象方法,具体由子类实现
7 | public abstract void BuildCPU();
8 |
9 | //第二步:装主板
10 | //声明为抽象方法,具体由子类实现
11 | public abstract void BuildMainboard();
12 |
13 | // 第三步:装硬盘
14 | // 声明为抽象方法,具体由子类实现
15 | public abstract void BuildHD();
16 |
17 | // 返回产品的方法:获得组装好的电脑
18 | public abstract Computer GetComputer();
19 | }
20 |
--------------------------------------------------------------------------------
/BuilderPattern/src/example/Client.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] srgs) {
6 |
7 | Director director = new Director();
8 | Builder builder = new ConcreteBuilder();
9 | director.Construct(builder);
10 | Computer computer = builder.GetComputer();
11 | computer.Show();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/BuilderPattern/src/example/Computer.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class Computer {
7 |
8 | // 电脑组件的集合
9 | private List parts = new ArrayList();
10 |
11 | // 用于将组件组装到电脑里
12 | public void Add(String part) {
13 | parts.add(part);
14 | }
15 |
16 | public void Show() {
17 | for (int i = 0; i < parts.size(); i++) {
18 | System.out.println("组件" + parts.get(i) + "装好了");
19 | }
20 | System.out.println("电脑组装完成,请验收");
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/BuilderPattern/src/example/ConcreteBuilder.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class ConcreteBuilder extends Builder {
4 |
5 | // 创建产品实例
6 | Computer computer = new Computer();
7 |
8 | // 组装产品
9 | @Override
10 | public void BuildCPU() {
11 | computer.Add("组装CPU");
12 | }
13 |
14 | @Override
15 | public void BuildMainboard() {
16 | computer.Add("组装主板");
17 | }
18 |
19 | @Override
20 | public void BuildHD() {
21 | computer.Add("组装主板");
22 | }
23 |
24 | // 返回组装成功的电脑
25 | @Override
26 | public Computer GetComputer() {
27 | return computer;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/BuilderPattern/src/example/Director.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Director {
4 |
5 | public void Construct(Builder builder) {
6 |
7 | builder.BuildCPU();
8 | builder.BuildMainboard();
9 | builder.BuildHD();
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ChainOfResponsibilityPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/ChainOfResponsibilityPattern/Client.java:
--------------------------------------------------------------------------------
1 | package ChainOfResponsibilityPattern;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 |
7 | // 声明处理节点
8 | Handler h1 = new ConcreteHandler1();
9 | Handler h2 = new ConcreteHandler2();
10 | Handler h3 = new ConcreteHandler3();
11 |
12 | // 处理顺序
13 | h1.setNext(h2);
14 | h1.setNext(h3);
15 |
16 | // 返回结果
17 | Response response = h1.hanleMessage(new Request());
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/ChainOfResponsibilityPattern/ConcreteHandler1.java:
--------------------------------------------------------------------------------
1 | package ChainOfResponsibilityPattern;
2 |
3 | import java.util.logging.Level;
4 |
5 | public class ConcreteHandler1 extends Handler {
6 |
7 | @Override
8 | protected Level getHandlerLevel() {
9 | // TODO Auto-generated method stub
10 | return null;
11 | }
12 |
13 | @Override
14 | protected Response echo(Request request) {
15 | // TODO Auto-generated method stub
16 | return null;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/ChainOfResponsibilityPattern/ConcreteHandler2.java:
--------------------------------------------------------------------------------
1 | package ChainOfResponsibilityPattern;
2 |
3 | import java.util.logging.Level;
4 |
5 | public class ConcreteHandler2 extends Handler{
6 |
7 | @Override
8 | protected Level getHandlerLevel() {
9 | // TODO Auto-generated method stub
10 | return null;
11 | }
12 |
13 | @Override
14 | protected Response echo(Request request) {
15 | // TODO Auto-generated method stub
16 | return null;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/ChainOfResponsibilityPattern/ConcreteHandler3.java:
--------------------------------------------------------------------------------
1 | package ChainOfResponsibilityPattern;
2 |
3 | import java.util.logging.Level;
4 |
5 | public class ConcreteHandler3 extends Handler{
6 |
7 | @Override
8 | protected Level getHandlerLevel() {
9 | // TODO Auto-generated method stub
10 | return null;
11 | }
12 |
13 | @Override
14 | protected Response echo(Request request) {
15 | // TODO Auto-generated method stub
16 | return null;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/ChainOfResponsibilityPattern/Handler.java:
--------------------------------------------------------------------------------
1 | package ChainOfResponsibilityPattern;
2 |
3 | import java.util.logging.Level;
4 |
5 | public abstract class Handler {
6 |
7 | private Handler nextHandler;
8 |
9 | @SuppressWarnings("unlikely-arg-type")
10 | public final Response hanleMessage(Request request) {
11 |
12 | Response response = null;
13 |
14 | if (this.getHandlerLevel().equals(request.getRequestLevel())) {
15 | response = this.echo(request);
16 | } else {
17 | if(this.nextHandler != null) {
18 | response = this.nextHandler.hanleMessage(request);
19 | }else {
20 |
21 | }
22 | }
23 | return response;
24 | }
25 |
26 | public void setNext(Handler _handler) {
27 | this.nextHandler = _handler;
28 | }
29 |
30 | protected abstract Level getHandlerLevel();
31 |
32 | protected abstract Response echo(Request request);
33 | }
34 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/ChainOfResponsibilityPattern/Level.java:
--------------------------------------------------------------------------------
1 | package ChainOfResponsibilityPattern;
2 |
3 | public class Level {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/ChainOfResponsibilityPattern/Request.java:
--------------------------------------------------------------------------------
1 | package ChainOfResponsibilityPattern;
2 |
3 | public class Request {
4 |
5 | public Level getRequestLevel() {
6 | return null;
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/ChainOfResponsibilityPattern/Response.java:
--------------------------------------------------------------------------------
1 | package ChainOfResponsibilityPattern;
2 |
3 | public class Response {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/example/Client.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 |
7 | Handler h1 = new TeamLeader();
8 | Handler h2 = new Manager();
9 | Handler h3 = new GeneralManager();
10 |
11 | //开始测试
12 | String test1 = h1.handleRequest("张三", 2);
13 | System.out.println("test1 = " + test1);
14 | String test2 = h3.handleRequest("李四", 2);
15 | System.out.println("test2 = " + test2);
16 | System.out.println("---------------------------------------");
17 |
18 | String test3 = h2.handleRequest("张三", 4);
19 | System.out.println("test3 = " + test3);
20 | String test4 = h3.handleRequest("李四", 4);
21 | System.out.println("test4 = " + test4);
22 | System.out.println("---------------------------------------");
23 |
24 | String test5 = h3.handleRequest("张三", 7);
25 | System.out.println("test5 = " + test5);
26 | String test6 = h3.handleRequest("李四", 7);
27 | System.out.println("test6 = " + test6);
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/example/GeneralManager.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class GeneralManager extends Handler {
4 |
5 | @Override
6 | public String handleRequest(String user, int dayNum) {
7 |
8 | String str = "";
9 | // 总监权限最大
10 | if (dayNum < 10) {
11 | if ("张三".equals(user)) {
12 | str = "成功:总监同意【" + user + "】请假" + dayNum + "天";
13 | } else {
14 | // 其他人一律不同意
15 | str = "其他人一律不同意";
16 | }
17 | } else {
18 | // 如果还有后继的处理对象,继续传递
19 | if (getSuccessor() != null) {
20 | return getSuccessor().handleRequest(user, dayNum);
21 | }
22 | }
23 | return str;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/example/Handler.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public abstract class Handler {
4 | /**
5 | * 持有下一个处理请求的对象
6 | */
7 | protected Handler successor = null;
8 |
9 | /**
10 | * 取值方法
11 | */
12 | public Handler getSuccessor() {
13 | return successor;
14 | }
15 |
16 | /**
17 | * 设置下一个处理请求的对象
18 | */
19 | public void setSuccessor(Handler successor) {
20 | this.successor = successor;
21 | }
22 |
23 | /**
24 | * @return 成功或失败的具体通知
25 | */
26 | public abstract String handleRequest(String user, int dayNum);
27 |
28 | }
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/example/Manager.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Manager extends Handler{
4 |
5 | @Override
6 | public String handleRequest(String user, int dayNum) {
7 |
8 | String str = "";
9 | // 经理权限相对较大
10 | if (dayNum < 5) {
11 | if ("张三".equals(user)) {
12 | str = "成功:经理同意【" + user + "】请假" + dayNum + "天";
13 | } else {
14 | // 其他人一律不同意
15 | str = "其他人一律不同意";
16 | }
17 | } else {
18 | // 请假超过五天的
19 | if (getSuccessor() != null) {
20 | return getSuccessor().handleRequest(user, dayNum);
21 | }
22 | }
23 | return str;
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/src/example/TeamLeader.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class TeamLeader extends Handler {
4 |
5 | @Override
6 | public String handleRequest(String user, int dayNum) {
7 |
8 | String str = "";
9 | // TL权限比较小
10 | if (dayNum < 3) {
11 | if ("张三".equals(user)) {
12 | str = "成功:TL同意【" + user + "】请假" + dayNum + "天";
13 | } else {
14 | // 其他人一律不同意
15 | str = "其他人一律不同意";
16 | }
17 | } else {
18 | // 请假超过三天的
19 | if (getSuccessor() != null) {
20 | return getSuccessor().handleRequest(user, dayNum);
21 | }
22 | }
23 | return str;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/CommandPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/CommandPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | CommandPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/CommandPattern/src/commandpattern/Client.java:
--------------------------------------------------------------------------------
1 | package commandpattern;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 |
7 | Receiver receiver = new ConcreteReceiver1();
8 |
9 | Command command = new ConcreteCommand1(receiver);
10 |
11 | Invoker invoker = new Invoker();
12 |
13 | invoker.setCommand(command);
14 |
15 | invoker.action();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/CommandPattern/src/commandpattern/Command.java:
--------------------------------------------------------------------------------
1 | package commandpattern;
2 |
3 | public abstract class Command {
4 |
5 | public abstract void execute();
6 | }
7 |
--------------------------------------------------------------------------------
/CommandPattern/src/commandpattern/ConcreteCommand1.java:
--------------------------------------------------------------------------------
1 | package commandpattern;
2 |
3 | public class ConcreteCommand1 extends Command {
4 |
5 | private Receiver receiver;
6 |
7 | public ConcreteCommand1(Receiver _receiver) {
8 | this.receiver = _receiver;
9 | }
10 |
11 | @Override
12 | public void execute() {
13 | this.receiver.dth();
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/CommandPattern/src/commandpattern/ConcreteCommand2.java:
--------------------------------------------------------------------------------
1 | package commandpattern;
2 |
3 | public class ConcreteCommand2 extends Command {
4 |
5 | private Receiver receiver;
6 |
7 | public ConcreteCommand2(Receiver _receiver) {
8 | this.receiver = _receiver;
9 | }
10 |
11 | @Override
12 | public void execute() {
13 | this.receiver.dth();
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/CommandPattern/src/commandpattern/ConcreteReceiver1.java:
--------------------------------------------------------------------------------
1 | package commandpattern;
2 |
3 | public class ConcreteReceiver1 extends Receiver{
4 |
5 | @Override
6 | public void dth() {
7 | // TODO
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/CommandPattern/src/commandpattern/ConcreteReceiver2.java:
--------------------------------------------------------------------------------
1 | package commandpattern;
2 |
3 | public class ConcreteReceiver2 extends Receiver{
4 |
5 | @Override
6 | public void dth() {
7 | // TODO
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/CommandPattern/src/commandpattern/Invoker.java:
--------------------------------------------------------------------------------
1 | package commandpattern;
2 |
3 | public class Invoker {
4 |
5 | private Command command;
6 |
7 | public void setCommand(Command _command) {
8 | this.command = _command;
9 | }
10 |
11 | public void action() {
12 | this.command.execute();
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/CommandPattern/src/commandpattern/Receiver.java:
--------------------------------------------------------------------------------
1 | package commandpattern;
2 |
3 | public abstract class Receiver {
4 |
5 | public abstract void dth();
6 | }
7 |
--------------------------------------------------------------------------------
/CommandPattern/src/example/Client.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 |
7 | Cook cook = new CookA();
8 |
9 | Command command = new CookCommand(cook);
10 |
11 | Consumer cosumer=new Consumer();
12 |
13 | Consumer.order();
14 |
15 | cook.say();
16 |
17 | cosumer.setCommand(command);
18 |
19 | cosumer.action();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/CommandPattern/src/example/Command.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public abstract class Command {
4 |
5 | public abstract void orderList();
6 | }
7 |
--------------------------------------------------------------------------------
/CommandPattern/src/example/Consumer.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | /**
4 | * 相当于Invoker
5 | *
6 | * @author cm_wang
7 | *
8 | */
9 | public class Consumer {
10 |
11 | static void order() {
12 | System.out.println("小王点菜。。。");
13 | }
14 |
15 | private Command command;
16 |
17 | public void setCommand(Command _command) {
18 | this.command = _command;
19 | }
20 |
21 | public void action() {
22 | this.command.orderList();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/CommandPattern/src/example/Cook.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | /**
4 | * 相当于接收者
5 | *
6 | * @author cm_wang
7 | *
8 | */
9 | public abstract class Cook {
10 |
11 | /**
12 | * 厨师烹饪
13 | */
14 | public abstract void cooking();
15 |
16 | /**
17 | * 收到菜单
18 | */
19 | public abstract void say();
20 | }
21 |
--------------------------------------------------------------------------------
/CommandPattern/src/example/CookA.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class CookA extends Cook {
4 |
5 | @Override
6 | public void cooking() {
7 | System.out.println("厨师A开始做饭。。。");
8 | }
9 |
10 | @Override
11 | public void say() {
12 | System.out.println("厨师A收到菜单。。。");
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/CommandPattern/src/example/CookCommand.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 |
4 | public class CookCommand extends Command {
5 |
6 | private Cook cook;
7 |
8 | public CookCommand(Cook _cook) {
9 | this.cook = _cook;
10 | }
11 |
12 | @Override
13 | public void orderList() {
14 | cook.cooking();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/DecoratorPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/DecoratorPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | DecoratorPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/DecoratorPattern/src/decoratorpattern/Client.java:
--------------------------------------------------------------------------------
1 | package decoratorpattern;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] agrs) {
6 |
7 | Component component = new ConcreateComponent();
8 |
9 | // first
10 | component = new ConcreteDecorator1(component);
11 |
12 | // second
13 | component = new ConcreteDecorator2(component);
14 |
15 | // run
16 | component.operate();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/DecoratorPattern/src/decoratorpattern/Component.java:
--------------------------------------------------------------------------------
1 | package decoratorpattern;
2 |
3 | public abstract class Component {
4 |
5 | public abstract void operate();
6 | }
7 |
--------------------------------------------------------------------------------
/DecoratorPattern/src/decoratorpattern/ConcreateComponent.java:
--------------------------------------------------------------------------------
1 | package decoratorpattern;
2 |
3 | public class ConcreateComponent extends Component{
4 |
5 | @Override
6 | public void operate() {
7 | System.out.println("do something...");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/DecoratorPattern/src/decoratorpattern/ConcreteDecorator1.java:
--------------------------------------------------------------------------------
1 | package decoratorpattern;
2 |
3 | public class ConcreteDecorator1 extends Decorator{
4 |
5 | public ConcreteDecorator1(Component _component) {
6 | super(_component);
7 | }
8 |
9 | private void method1() {
10 | System.out.println("method1 装饰");
11 | }
12 |
13 | public void operate() {
14 | this.method1();
15 | super.operate();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/DecoratorPattern/src/decoratorpattern/ConcreteDecorator2.java:
--------------------------------------------------------------------------------
1 | package decoratorpattern;
2 |
3 | public class ConcreteDecorator2 extends Decorator{
4 |
5 | public ConcreteDecorator2(Component _component) {
6 | super(_component);
7 | }
8 |
9 | private void method2() {
10 | System.out.println("method2 装饰");
11 | }
12 |
13 | public void operate() {
14 | this.method2();
15 | super.operate();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/DecoratorPattern/src/decoratorpattern/Decorator.java:
--------------------------------------------------------------------------------
1 | package decoratorpattern;
2 |
3 | public abstract class Decorator extends Component {
4 |
5 | private Component component = null;
6 |
7 | public Decorator(Component _component) {
8 | this.component = _component;
9 | }
10 |
11 | @Override
12 | public void operate() {
13 | this.component.operate();
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/DecoratorPattern/src/example/Client.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import java.io.BufferedInputStream;
4 | import java.io.FileInputStream;
5 | import java.io.FileNotFoundException;
6 | import java.io.IOException;
7 | import java.io.InputStream;
8 |
9 | public class Client {
10 | public static void main(String[] args) {
11 | int result = 0;
12 | InputStream inputStream = null;
13 | try {
14 | inputStream = new ToUpperCaseInputStream(new BufferedInputStream(new FileInputStream("file path")));
15 |
16 | while ((result = inputStream.read()) >= 0) {
17 | System.out.print((char) result);
18 | }
19 |
20 | } catch (FileNotFoundException e) {
21 | e.printStackTrace();
22 | } catch (IOException e) {
23 | e.printStackTrace();
24 | } finally {
25 | try {
26 | inputStream.close();
27 | } catch (IOException e) {
28 | e.printStackTrace();
29 | }
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/DecoratorPattern/src/example/ToUpperCaseInputStream.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import java.io.FilterInputStream;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 |
7 | public class ToUpperCaseInputStream extends FilterInputStream {
8 |
9 | InputStream inputStream;
10 |
11 | protected ToUpperCaseInputStream(InputStream in) {
12 | super(in);
13 | this.inputStream = in;
14 | }
15 |
16 | /**
17 | * 读取单个字节
18 | *
19 | * @return
20 | * @throws IOException
21 | */
22 | @Override
23 | public int read() throws IOException {
24 | // 获取父类读取的结果
25 | int result = super.read();
26 | // 如果读取到字符a,就抛出异常
27 | if (result == 'a') {
28 | throw new ToUpperException();
29 | }
30 | // 如果等于-1,说明无内容
31 | // 否则,将字节转成char,再将char转换成大写的后返回
32 | // 返回值类型是int类型,这里返回一个字符会被自动转型
33 | return (result == -1 ? result : Character.toUpperCase(Character.toChars(result)[0]));
34 | }
35 |
36 | @Override
37 | public int read(byte[] b, int off, int len) throws IOException {
38 | int result = super.read(b, off, len);
39 | for (int i = off; i < off + result; i++) {
40 | // 将字节转成大写字符后再转成字节
41 | b[i] = (byte) Character.toUpperCase((char) b[i]);
42 | }
43 | return result;
44 | }
45 |
46 | /***
47 | * 这里是一个内部类,自定义异常
48 | */
49 | class ToUpperException extends IOException {
50 | @Override
51 | public void printStackTrace() {
52 | System.out.println("不好意思我遇到异常了,向上转型失败啦");
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/FactoryPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/FactoryPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | FactoryPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/FactoryPattern/src/example/Circle.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Circle implements IShape {
4 |
5 | @Override
6 | public void draw() {
7 | System.out.println("This's Circle");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/FactoryPattern/src/example/Client.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 | IShapeFactory a = new Factory();
7 | IShape b = a.getShape("Rectangle");
8 | b.draw();
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/FactoryPattern/src/example/Factory.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Factory implements IShapeFactory {
4 |
5 | @Override
6 | public IShape getShape(String shape) {
7 |
8 | if (shape.equals("Circle")) {
9 | return new Circle();
10 | }
11 | if (shape.equals("Square")) {
12 | return new Square();
13 | }
14 | if(shape.equals("Rectangle")){
15 | return new Rectangle();
16 | }
17 | return null;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/FactoryPattern/src/example/IShape.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public interface IShape {
4 |
5 | void draw();
6 | }
7 |
--------------------------------------------------------------------------------
/FactoryPattern/src/example/IShapeFactory.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public interface IShapeFactory {
4 |
5 | IShape getShape(String shape);
6 | }
7 |
--------------------------------------------------------------------------------
/FactoryPattern/src/example/Rectangle.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Rectangle implements IShape{
4 |
5 | @Override
6 | public void draw() {
7 | System.out.println("This's Rectangle");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/FactoryPattern/src/example/Square.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Square implements IShape{
4 |
5 | @Override
6 | public void draw() {
7 | System.out.println("This's Square");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/FactoryPattern/src/factorypattern/Client.java:
--------------------------------------------------------------------------------
1 | package factorypattern;
2 |
3 |
4 | public class Client {
5 |
6 | public static void main(String[] args) {
7 | IFactory f = new Factory();
8 | IProduct p = f.createProduct();
9 | p.productMethod();
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/FactoryPattern/src/factorypattern/Factory.java:
--------------------------------------------------------------------------------
1 | package factorypattern;
2 |
3 | public class Factory implements IFactory {
4 |
5 | @Override
6 | public IProduct createProduct() {
7 | return new Product();
8 | }
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/FactoryPattern/src/factorypattern/IFactory.java:
--------------------------------------------------------------------------------
1 | package factorypattern;
2 |
3 | public interface IFactory {
4 |
5 | IProduct createProduct();
6 | }
7 |
--------------------------------------------------------------------------------
/FactoryPattern/src/factorypattern/IProduct.java:
--------------------------------------------------------------------------------
1 | package factorypattern;
2 |
3 | public interface IProduct {
4 |
5 | void productMethod();
6 | }
7 |
--------------------------------------------------------------------------------
/FactoryPattern/src/factorypattern/Product.java:
--------------------------------------------------------------------------------
1 | package factorypattern;
2 |
3 | public class Product implements IProduct{
4 |
5 | @Override
6 | public void productMethod() {
7 | System.out.println("I'm a product");
8 | }
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/IteratorPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/IteratorPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | IteratorPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/IteratorPattern/src/iteratorpattern/Aggregate.java:
--------------------------------------------------------------------------------
1 | package iteratorpattern;
2 |
3 | public interface Aggregate {
4 |
5 | public void add(Object obj);
6 |
7 | public void remove(Object obj);
8 |
9 | public Iterator iterator();
10 | }
11 |
--------------------------------------------------------------------------------
/IteratorPattern/src/iteratorpattern/Client.java:
--------------------------------------------------------------------------------
1 | package iteratorpattern;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 | Aggregate ag = new ConcreteAggregate();
7 | ag.add("小明");
8 | ag.add("小红");
9 | ag.add("小刚");
10 | Iterator it = ag.iterator();
11 | while (it.hasNext()) {
12 | String str = (String) it.next();
13 | System.out.println(str);
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/IteratorPattern/src/iteratorpattern/ConcreteAggregate.java:
--------------------------------------------------------------------------------
1 | package iteratorpattern;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class ConcreteAggregate implements Aggregate {
7 |
8 | private List list = new ArrayList();
9 |
10 | @Override
11 | public void add(Object obj) {
12 | list.add(obj);
13 | }
14 |
15 | @Override
16 | public Iterator iterator() {
17 | return new ConcreteIterator(list);
18 | }
19 |
20 | @Override
21 | public void remove(Object obj) {
22 | list.remove(obj);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/IteratorPattern/src/iteratorpattern/ConcreteIterator.java:
--------------------------------------------------------------------------------
1 | package iteratorpattern;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class ConcreteIterator implements Iterator {
7 |
8 | private List list = new ArrayList();
9 |
10 | // 当前游标
11 | public int cursor = 0;
12 |
13 | public ConcreteIterator(List list) {
14 | this.list = list;
15 | }
16 |
17 | @Override
18 | public Object next() {
19 | Object obj = null;
20 | if (this.hasNext()) {
21 | obj = this.list.get(cursor++);
22 | }
23 | return obj;
24 | }
25 |
26 | @Override
27 | public boolean hasNext() {
28 | if (cursor == list.size()) {
29 | return false;
30 | }
31 | return true;
32 | }
33 |
34 | @Override
35 | public boolean remove() {
36 | this.list.remove(this.cursor);
37 | return true;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/IteratorPattern/src/iteratorpattern/Iterator.java:
--------------------------------------------------------------------------------
1 | package iteratorpattern;
2 |
3 | public interface Iterator {
4 |
5 | public Object next();
6 |
7 | public boolean hasNext();
8 |
9 | public boolean remove();
10 | }
11 |
--------------------------------------------------------------------------------
/MediatorPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/MediatorPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | MediatorPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/MediatorPattern/src/example/Champions.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Champions extends WulinAlliance {
4 | private Wudang wudang;
5 | private Shaolin shaolin;
6 | private Emei emei;
7 |
8 | public void setWudang(Wudang wudang) {
9 | this.wudang = wudang;
10 | }
11 |
12 | public void setEmei(Emei emei) {
13 | this.emei = emei;
14 | }
15 |
16 | public void setShaolin(Shaolin shaolin) {
17 | this.shaolin = shaolin;
18 | }
19 |
20 | @Override
21 | public void notice(String message, United united) {
22 | if (united == wudang) {
23 | shaolin.getNotice(message);
24 | } else if (united == emei) {
25 | shaolin.getNotice(message);
26 | } else if (united == shaolin) {
27 | wudang.getNotice(message);
28 | emei.getNotice(message);
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/MediatorPattern/src/example/Client.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Client {
4 | public static void main(String[] args) {
5 | Champions champions = new Champions();
6 | Wudang wudang = new Wudang(champions);
7 | Shaolin shaolin = new Shaolin(champions);
8 | Emei emei = new Emei(champions);
9 | champions.setWudang(wudang);
10 | champions.setShaolin(shaolin);
11 | champions.setEmei(emei);
12 | wudang.sendAlliance("武当弟子被少林大力金刚指所杀");
13 | emei.sendAlliance("峨眉弟子被少林大力金刚指所杀");
14 | shaolin.sendAlliance("少林弟子绝不会做出这种事情");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/MediatorPattern/src/example/Emei.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Emei extends United {
4 | public Emei(WulinAlliance wulinAlliance) {
5 | super(wulinAlliance);
6 | }
7 | public void sendAlliance(String message) {
8 | wulinAlliance.notice(message, Emei.this);
9 | }
10 | public void getNotice(String message) {
11 | System.out.println("峨眉收到消息:" + message);
12 | }
13 | }
--------------------------------------------------------------------------------
/MediatorPattern/src/example/Shaolin.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Shaolin extends United {
4 | public Shaolin(WulinAlliance wulinAlliance) {
5 | super(wulinAlliance);
6 | }
7 |
8 | public void sendAlliance(String message) {
9 | wulinAlliance.notice(message, Shaolin.this);
10 | }
11 |
12 | public void getNotice(String message) {
13 | System.out.println("少林收到消息:" + message);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/MediatorPattern/src/example/United.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public abstract class United {
4 |
5 | protected WulinAlliance wulinAlliance;
6 |
7 | public United(WulinAlliance wulinAlliance) {
8 | this.wulinAlliance = wulinAlliance;
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/MediatorPattern/src/example/Wudang.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Wudang extends United {
4 | public Wudang(WulinAlliance wulinAlliance) {
5 | super(wulinAlliance);
6 | }
7 |
8 | public void sendAlliance(String message) {
9 | wulinAlliance.notice(message, this);
10 | }
11 |
12 | public void getNotice(String message) {
13 | System.out.println("武当收到消息:" + message);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/MediatorPattern/src/example/WulinAlliance.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public abstract class WulinAlliance {
4 |
5 | abstract void notice(String message, United united);
6 | }
7 |
--------------------------------------------------------------------------------
/MediatorPattern/src/mediatorpattern/Colleague.java:
--------------------------------------------------------------------------------
1 | package mediatorpattern;
2 |
3 | public abstract class Colleague {
4 |
5 | protected Mediator mediator;
6 |
7 | public Colleague(Mediator _mediator) {
8 | this.mediator = _mediator;
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/MediatorPattern/src/mediatorpattern/ConcreteColleague1.java:
--------------------------------------------------------------------------------
1 | package mediatorpattern;
2 |
3 | public class ConcreteColleague1 extends Colleague {
4 |
5 | public ConcreteColleague1(Mediator _mediator) {
6 | super(_mediator);
7 | }
8 |
9 | public void selfMethod1() {
10 | //TODO
11 | }
12 |
13 | public void depMethod1() {
14 | super.mediator.doSth1();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/MediatorPattern/src/mediatorpattern/ConcreteColleague2.java:
--------------------------------------------------------------------------------
1 | package mediatorpattern;
2 |
3 | public class ConcreteColleague2 extends Colleague {
4 |
5 | public ConcreteColleague2(Mediator _mediator) {
6 | super(_mediator);
7 | }
8 |
9 | public void selfMethod2() {
10 | // TODO
11 | }
12 |
13 | public void depMethod2() {
14 | super.mediator.doSth2();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/MediatorPattern/src/mediatorpattern/ConcreteMediator.java:
--------------------------------------------------------------------------------
1 | package mediatorpattern;
2 |
3 | public class ConcreteMediator extends Mediator{
4 |
5 | @Override
6 | public void doSth1() {
7 | super.c1.selfMethod1();
8 | super.c2.selfMethod2();
9 | }
10 |
11 | @Override
12 | public void doSth2() {
13 | super.c1.selfMethod1();
14 | super.c2.selfMethod2();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/MediatorPattern/src/mediatorpattern/Mediator.java:
--------------------------------------------------------------------------------
1 | package mediatorpattern;
2 |
3 | public abstract class Mediator {
4 |
5 | protected ConcreteColleague1 c1;
6 | protected ConcreteColleague2 c2;
7 |
8 | public ConcreteColleague1 getC1() {
9 | return c1;
10 | }
11 | public void setC1(ConcreteColleague1 c1) {
12 | this.c1 = c1;
13 | }
14 | public ConcreteColleague2 getC2() {
15 | return c2;
16 | }
17 | public void setC2(ConcreteColleague2 c2) {
18 | this.c2 = c2;
19 | }
20 |
21 | public abstract void doSth1();
22 | public abstract void doSth2();
23 | }
24 |
--------------------------------------------------------------------------------
/ObserverPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/ObserverPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ObserverPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/ObserverPattern/src/example/Client.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 |
7 | shengchengang subject = new shengchengang();
8 |
9 | Observer obs = new yangzhi();
10 |
11 | Observer thief = new wuyong();
12 |
13 | Observer thief1 = new baisheng();
14 |
15 | // 吴用等人假装卖枣稳住杨志等人
16 | subject.addObserver(thief);
17 | // 白胜夺来放有蒙汗药的瓢倒在桶里麻倒对方
18 | subject.addObserver(thief1);
19 | // 杨志等人喝了带蒙汗药的酒
20 | subject.addObserver(obs);
21 |
22 | subject.doSth();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/ObserverPattern/src/example/Observer.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | /**
4 | * 观察者
5 | *
6 | * @author cm_wang
7 | *
8 | */
9 | public interface Observer {
10 |
11 | void update();
12 | }
13 |
--------------------------------------------------------------------------------
/ObserverPattern/src/example/Subject.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import java.util.Vector;
4 |
5 | /**
6 | * 被观察者
7 | *
8 | * @author cm_wang
9 | *
10 | */
11 | public abstract class Subject {
12 |
13 | private Vector obsVector = new Vector();
14 |
15 | public void addObserver(Observer o) {
16 | this.obsVector.add(o);
17 | }
18 |
19 | public void delObserver(Observer o) {
20 | this.obsVector.remove(o);
21 | }
22 |
23 | public void notifyObserver() {
24 | obsVector.forEach(i -> {
25 | i.update();
26 | });
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/ObserverPattern/src/example/baisheng.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class baisheng implements Observer{
4 |
5 | @Override
6 | public void update() {
7 | System.out.println("白胜夺来倒在桶里麻倒对方");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/ObserverPattern/src/example/shengchengang.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 |
4 | public class shengchengang extends Subject {
5 |
6 | public void doSth() {
7 | super.notifyObserver();
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/ObserverPattern/src/example/wuyong.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class wuyong implements Observer{
4 |
5 | @Override
6 | public void update() {
7 | System.out.println("吴用等人假装卖枣稳住杨志等人");
8 | System.out.println("吴用在杨志放松警惕时下药在瓢里");
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/ObserverPattern/src/example/yangzhi.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class yangzhi implements Observer{
4 |
5 | @Override
6 | public void update() {
7 | System.out.println("杨志等人喝了带蒙汗药的酒,就只能眼睁睁的看晁盖、吴用等人拿走生辰纲");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/ObserverPattern/src/observerpattern/Client.java:
--------------------------------------------------------------------------------
1 | package observerpattern;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 |
7 | ConcreteSubject subject = new ConcreteSubject();
8 |
9 | Observer obs = new ConcreteObserver();
10 |
11 | subject.addObserver(obs);
12 |
13 | subject.doSth();
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/ObserverPattern/src/observerpattern/ConcreteObserver.java:
--------------------------------------------------------------------------------
1 | package observerpattern;
2 |
3 | public class ConcreteObserver implements Observer{
4 |
5 | @Override
6 | public void update() {
7 | System.out.println("接收消息并处理");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/ObserverPattern/src/observerpattern/ConcreteSubject.java:
--------------------------------------------------------------------------------
1 | package observerpattern;
2 |
3 | public class ConcreteSubject extends Subject {
4 |
5 | public void doSth() {
6 | super.notifyObserver();
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/ObserverPattern/src/observerpattern/Observer.java:
--------------------------------------------------------------------------------
1 | package observerpattern;
2 |
3 | public interface Observer {
4 |
5 | void update();
6 | }
7 |
--------------------------------------------------------------------------------
/ObserverPattern/src/observerpattern/Subject.java:
--------------------------------------------------------------------------------
1 | package observerpattern;
2 |
3 | import java.util.Vector;
4 |
5 | public abstract class Subject {
6 |
7 | private Vector obsVector = new Vector();
8 |
9 | public void addObserver(Observer o) {
10 | this.obsVector.add(o);
11 | }
12 |
13 | public void delObserver(Observer o) {
14 | this.obsVector.remove(o);
15 | }
16 |
17 | public void notifyObserver() {
18 | obsVector.forEach(i -> {
19 | i.update();
20 | });
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/PrototypePattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/PrototypePattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | PrototypePattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/PrototypePattern/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.8
12 |
--------------------------------------------------------------------------------
/PrototypePattern/bin/example/Client.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangchengming666/DesignPattern/3cf5701b5b52d2c6ed545123d10dc41002ac9a2e/PrototypePattern/bin/example/Client.class
--------------------------------------------------------------------------------
/PrototypePattern/bin/example/Message.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangchengming666/DesignPattern/3cf5701b5b52d2c6ed545123d10dc41002ac9a2e/PrototypePattern/bin/example/Message.class
--------------------------------------------------------------------------------
/PrototypePattern/bin/example/Temeplate.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangchengming666/DesignPattern/3cf5701b5b52d2c6ed545123d10dc41002ac9a2e/PrototypePattern/bin/example/Temeplate.class
--------------------------------------------------------------------------------
/PrototypePattern/bin/prototypepattern/Client.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangchengming666/DesignPattern/3cf5701b5b52d2c6ed545123d10dc41002ac9a2e/PrototypePattern/bin/prototypepattern/Client.class
--------------------------------------------------------------------------------
/PrototypePattern/bin/prototypepattern/ConcretePrototype.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangchengming666/DesignPattern/3cf5701b5b52d2c6ed545123d10dc41002ac9a2e/PrototypePattern/bin/prototypepattern/ConcretePrototype.class
--------------------------------------------------------------------------------
/PrototypePattern/bin/prototypepattern/Prototype.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wangchengming666/DesignPattern/3cf5701b5b52d2c6ed545123d10dc41002ac9a2e/PrototypePattern/bin/prototypepattern/Prototype.class
--------------------------------------------------------------------------------
/PrototypePattern/src/example/Client.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import java.util.Random;
4 |
5 | public class Client {
6 |
7 | private static int count = 10;
8 |
9 | public static void main(String[] args) {
10 |
11 | int i = 0;
12 | Message msg = new Message(new Temeplate());
13 | while (i < count) {
14 | Message cloneMsg = msg.clone();
15 | Random rand = new Random();
16 | cloneMsg.setReceiver(rand.nextInt(1000000000) + 1+"@qq.com");
17 | send(cloneMsg);
18 | i++;
19 | }
20 | }
21 |
22 | public static void send(Message msg) {
23 | System.out.println(msg.getReceiver()+"接收成功");
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/PrototypePattern/src/example/Message.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Message implements Cloneable {
4 |
5 | private String receiver;
6 |
7 | private String subject;
8 |
9 | private String content;
10 |
11 | public Message(Temeplate temeplate) {
12 | this.subject = temeplate.getSubject();
13 | this.content = temeplate.getContent();
14 | }
15 |
16 | @Override
17 | public Message clone() {
18 | Message msg = null;
19 | try {
20 | msg = (Message)super.clone();
21 | }catch(CloneNotSupportedException ex) {
22 | ex.printStackTrace();
23 | }
24 | return msg;
25 | }
26 |
27 | public String getReceiver() {
28 | return receiver;
29 | }
30 |
31 | public void setReceiver(String receiver) {
32 | this.receiver = receiver;
33 | }
34 |
35 | public String getSubject() {
36 | return subject;
37 | }
38 |
39 | public void setSubject(String subject) {
40 | this.subject = subject;
41 | }
42 |
43 | public String getContent() {
44 | return content;
45 | }
46 |
47 | public void setContent(String content) {
48 | this.content = content;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/PrototypePattern/src/example/Temeplate.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Temeplate {
4 |
5 | // 短信名字
6 | private String subject = "【天猫双11活动】";
7 |
8 | private String content = "双11活动通知:满99减66.";
9 |
10 | public String getSubject() {
11 | return subject;
12 | }
13 |
14 | public String getContent() {
15 | return content;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/PrototypePattern/src/prototypepattern/Client.java:
--------------------------------------------------------------------------------
1 | package prototypepattern;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 | ConcretePrototype cp = new ConcretePrototype();
7 | for (int i = 0; i < 10; i++) {
8 | ConcretePrototype clonecp = (ConcretePrototype) cp.clone();
9 | clonecp.show();
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/PrototypePattern/src/prototypepattern/ConcretePrototype.java:
--------------------------------------------------------------------------------
1 | package prototypepattern;
2 |
3 | public class ConcretePrototype extends Prototype {
4 | public void show() {
5 | System.out.println("原型模式实现类");
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/PrototypePattern/src/prototypepattern/Prototype.java:
--------------------------------------------------------------------------------
1 | package prototypepattern;
2 |
3 | public class Prototype implements Cloneable {
4 |
5 | /* (non-Javadoc)
6 | * @see java.lang.Object#clone()
7 | *
8 | * 重写Object的clone()方法,必须是public
9 | *
10 | */
11 | @Override
12 | public Prototype clone() {
13 |
14 | Prototype prototype = null;
15 | try {
16 | prototype = (Prototype) super.clone();
17 | } catch (CloneNotSupportedException ex) {
18 | ex.printStackTrace();
19 | }
20 | return prototype;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/ProxyPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/ProxyPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ProxyPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/ProxyPattern/src/example/Client.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import java.lang.reflect.Proxy;
4 |
5 | public class Client {
6 |
7 | public static void main(String[] agrs) {
8 | // 静态代理
9 | // Subject s = new LianJia();
10 | // s.buyHouse();
11 |
12 | // 动态代理
13 | Subject subject = new LianJia();
14 | Subject dynamicProxy = (Subject) Proxy.newProxyInstance(Subject.class.getClassLoader(), new Class[] { Subject.class },
15 | new DynamicProxyHandler(subject));
16 | dynamicProxy.buyHouse();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/ProxyPattern/src/example/DynamicProxyHandler.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import java.lang.reflect.InvocationHandler;
4 | import java.lang.reflect.Method;
5 |
6 | /**
7 | * 动态代理版
8 | *
9 | * @author cm_wang
10 | *
11 | */
12 | public class DynamicProxyHandler implements InvocationHandler {
13 |
14 | private Object object;
15 |
16 | public DynamicProxyHandler(final Object object) {
17 | this.object = object;
18 | }
19 |
20 | @Override
21 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
22 |
23 | System.out.println("动态代理开始--------->");
24 | Object result = method.invoke(object, args);
25 | System.out.println("动态代理结束--------->");
26 | return result;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/ProxyPattern/src/example/LianJia.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class LianJia implements Subject{
4 |
5 | @Override
6 | public void buyHouse() {
7 | RealSubject subject = new RealSubject();
8 | subject.buyHouse();
9 | lianximaijia();
10 | deal();
11 | shoufei();
12 | }
13 |
14 | private void lianximaijia() {
15 | System.out.println("卖家联系成功。");
16 | }
17 |
18 | private void deal() {
19 | System.out.println("成交。");
20 | }
21 |
22 | private void shoufei() {
23 | System.out.println("你好,房子成交后我们要收取总房款2%的手续费哦。");
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/ProxyPattern/src/example/RealSubject.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class RealSubject implements Subject{
4 |
5 | @Override
6 | public void buyHouse() {
7 | System.out.println("你好,我要买房子。");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/ProxyPattern/src/example/Subject.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public interface Subject {
4 |
5 | void buyHouse();
6 | }
7 |
--------------------------------------------------------------------------------
/ProxyPattern/src/proxypattern/Proxy.java:
--------------------------------------------------------------------------------
1 | package proxypattern;
2 |
3 | public class Proxy implements Subject{
4 |
5 | // 要代理哪个实现类
6 | public Subject subject = null;
7 |
8 | // 默认被代理者
9 | public Proxy() {
10 | this.subject = new Proxy();
11 | }
12 |
13 | public Proxy(Object...objects) {
14 |
15 | }
16 |
17 | @Override
18 | public void request() {
19 | this.subject.request();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/ProxyPattern/src/proxypattern/RealSubject.java:
--------------------------------------------------------------------------------
1 | package proxypattern;
2 |
3 | public class RealSubject implements Subject{
4 |
5 | @Override
6 | public void request() {
7 | // TODO
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/ProxyPattern/src/proxypattern/Subject.java:
--------------------------------------------------------------------------------
1 | package proxypattern;
2 |
3 | public interface Subject {
4 |
5 | void request();
6 | }
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ##### 面向对象的三大特性
2 | - 封装(Encapsulation)
3 |
4 | 隐藏对象的具体实现细节,通过共有方法暴露对象的功能。内部结构可以自由修改,同时可对成员进行更加精确的控制 (比如在setter方法中加值合法判断)
5 |
6 | - 继承(Inheritance)
7 |
8 | 使用已经存在的类作为基础类(父类),在此基础上建立新类(子类),子类既可复用父类的功能,也能进行扩展,从而实现代码复用。 另外Java是单继承的,可以通过接口和内部类实现多继承。但是有几点需要注意一下:
9 |
10 | 1.子类拥有父类非private的属性与方法
11 |
12 | 2.构造方法只能调用,不能实现,子类默认调用父类的无参构造方法,如果父类没有无参的构造方法,需要使用super显式调用!
13 |
14 | 3.慎用继承,要考虑是否需要从子类向父类进行向上转型!(转型要instanceof)
15 |
16 |
17 | - 多态(Polymorphism)
18 |
19 | 一个类实例的相同方法在不同的情形下有不同的表现形式。多态分为两种:方法重载和重写(主要描述父类和子类之前的多态)。
20 |
21 | ##### 类与类间的关系
22 | - 继承,实现,依赖,关联,聚合,组合
23 | - 类的相关程度:继承 < 实现 < 依赖 < 关联 < 聚合 < 组合
24 | - 依次的UML类图标记
25 | - 继承
26 |
27 | 
28 |
29 | - 实现
30 |
31 | 
32 |
33 | - 依赖
34 |
35 | 
36 |
37 | - 关联
38 |
39 | 
40 |
41 | - 聚合
42 |
43 | 
44 |
45 | - 组合
46 |
47 | 
48 |
49 | ##### 博客地址
50 | - [设计模式之六大设计原则](https://blog.csdn.net/wangchengming1/article/details/83090643)
51 | - [单例模式(Singleton Pattern)](https://blog.csdn.net/wangchengming1/article/details/83311035)
52 | - [工厂模式(Factory Pattern)](https://blog.csdn.net/wangchengming1/article/details/83377635)
53 | - [抽象工厂模式(Abstract Factory Pattern)](https://blog.csdn.net/wangchengming1/article/details/83542422)
54 | - [模板方法模式(Template Method Pattern)](https://blog.csdn.net/wangchengming1/article/details/83621035)
55 | - [建造者模式(Builder Pattern)](https://blog.csdn.net/wangchengming1/article/details/83788919)
56 | - [代理模式(Proxy Pattern)](https://blog.csdn.net/wangchengming1/article/details/83858767)
57 | - [原型模式(Prototype Pattern)](https://blog.csdn.net/wangchengming1/article/details/84107248)
58 | - [中介者模式(Mediator Pattern)](https://blog.csdn.net/wangchengming1/article/details/84768795)
59 | - [命令模式(Command Pattern)](https://blog.csdn.net/wangchengming1/article/details/84927843)
60 | - [责任链模式(Chain of Responsibility Pattern)](https://blog.csdn.net/wangchengming1/article/details/85049192)
61 | - [策略模式(Strategy Pattern)](https://blog.csdn.net/wangchengming1/article/details/85068565)
62 | - [适配器模式(Adapter Pattern)](https://blog.csdn.net/wangchengming1/article/details/85096998)
63 | - [迭代器模式(Iterator Pattern)](https://blog.csdn.net/wangchengming1/article/details/85252054)
64 | - [观察者模式(Observer Pattern)](https://blog.csdn.net/wangchengming1/article/details/86624109)
65 | - [装饰模式(Decorator Pattern)](https://blog.csdn.net/wangchengming1/article/details/86644556)
66 |
--------------------------------------------------------------------------------
/SingeltonPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/SingeltonPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | SingeltonPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/SingeltonPattern/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.8
12 |
--------------------------------------------------------------------------------
/SingeltonPattern/src/singletonpattern/SingletonPattern.java:
--------------------------------------------------------------------------------
1 | package singletonpattern;
2 |
3 | /**
4 | * 单例模式通用代码
5 | *
6 | * @author cm_wang
7 | *
8 | */
9 | public class SingletonPattern {
10 |
11 | // private static final SingletonPattern singletonPattern = new
12 | // SingletonPattern();
13 | //
14 | // // 私有的构造方法,为了限制产生多个对象
15 | // private SingletonPattern() {
16 | // }
17 | //
18 | // /**
19 | // * 通过这个方法获得对象唯一实例
20 | // *
21 | // * @return
22 | // */
23 | // public static SingletonPattern getSingleton() {
24 | // return singletonPattern;
25 | // }
26 |
27 | // /**
28 | // * 懒汉式单例
29 | // *
30 | // * @author cm_wang
31 | // *
32 | // */
33 | // private static SingletonPattern singletonPattern;
34 | //
35 | // private SingletonPattern() {
36 | // }
37 | //
38 | // public static synchronized SingletonPattern getInstance() {
39 | // if (singletonPattern == null) {
40 | // singletonPattern = new SingletonPattern();
41 | // }
42 | // return singletonPattern;
43 | // }
44 |
45 | private static SingletonPattern singletonPattern;
46 |
47 | private SingletonPattern() {}
48 |
49 | /**
50 | * 双重校验版
51 | *
52 | * @return
53 | */
54 | public static SingletonPattern getInstance() {
55 | if(singletonPattern == null) {
56 | synchronized(SingletonPattern.class) {
57 | if(singletonPattern == null) {
58 | singletonPattern = new SingletonPattern();
59 | }
60 | }
61 | }
62 | return singletonPattern;
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/StrategyPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/StrategyPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | StrategyPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/StrategyPattern/src/StrategyPattern/Client.java:
--------------------------------------------------------------------------------
1 | package StrategyPattern;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 |
7 | Strategy strategy = new ConcreteStrategy1();
8 |
9 | Context context = new Context(strategy);
10 |
11 | context.doAnything();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/StrategyPattern/src/StrategyPattern/ConcreteStrategy1.java:
--------------------------------------------------------------------------------
1 | package StrategyPattern;
2 |
3 | public class ConcreteStrategy1 implements Strategy{
4 |
5 | @Override
6 | public void sth() {
7 |
8 | System.out.println("策略算法实现1。。。");
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/StrategyPattern/src/StrategyPattern/ConcreteStrategy2.java:
--------------------------------------------------------------------------------
1 | package StrategyPattern;
2 |
3 | public class ConcreteStrategy2 implements Strategy{
4 |
5 | @Override
6 | public void sth() {
7 |
8 | System.out.println("策略算法实现2。。。");
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/StrategyPattern/src/StrategyPattern/Context.java:
--------------------------------------------------------------------------------
1 | package StrategyPattern;
2 |
3 | public class Context {
4 |
5 | private Strategy strategy = null;
6 |
7 | public Context(Strategy _strategy) {
8 | this.strategy = _strategy;
9 | }
10 |
11 | /**
12 | * 封装后的策略方法
13 | */
14 | public void doAnything() {
15 | this.strategy.sth();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/StrategyPattern/src/StrategyPattern/Strategy.java:
--------------------------------------------------------------------------------
1 | package StrategyPattern;
2 |
3 | public interface Strategy {
4 |
5 | void sth();
6 | }
7 |
--------------------------------------------------------------------------------
/StrategyPattern/src/example/BackDoor.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class BackDoor implements Strategy {
4 | @Override
5 | public void operate() {
6 | System.out.println("找乔国老帮忙,让吴国太给孙权施加压力,使孙权不能杀刘备");
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/StrategyPattern/src/example/BlackEnemy.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class BlackEnemy implements Strategy {
4 | @Override
5 | public void operate() {
6 | System.out.println("孙夫人断后,挡住追兵");
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/StrategyPattern/src/example/Context.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Context {
4 |
5 | private Strategy strategy;
6 |
7 | // 构造函数,要你使用哪个妙计
8 | public Context(Strategy strategy) {
9 | this.strategy = strategy;
10 | }
11 |
12 | public void setStrategy(Strategy strategy) {
13 | this.strategy = strategy;
14 | }
15 |
16 | public void operate() {
17 | this.strategy.operate();
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/StrategyPattern/src/example/GivenGreenLight.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class GivenGreenLight implements Strategy {
4 | @Override
5 | public void operate() {
6 | System.out.println("求吴国太开个绿灯,放行");
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/StrategyPattern/src/example/Strategy.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public interface Strategy {
4 |
5 | void operate();
6 | }
7 |
--------------------------------------------------------------------------------
/StrategyPattern/src/example/Zhaoyun.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Zhaoyun {
4 |
5 | public static void main(String[] args) {
6 | Context context;
7 |
8 | System.out.println("----------刚到吴国使用第一个锦囊---------------");
9 | context = new Context(new BackDoor());
10 | context.operate();
11 | System.out.println("\n");
12 |
13 | System.out.println("----------刘备乐不思蜀使用第二个锦囊---------------");
14 | context.setStrategy(new GivenGreenLight());
15 | context.operate();
16 | System.out.println("\n");
17 |
18 | System.out.println("----------孙权的追兵来了,使用第三个锦囊---------------");
19 | context.setStrategy(new BlackEnemy());
20 | context.operate();
21 | System.out.println("\n");
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/TemplateMethodPattern/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/TemplateMethodPattern/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | TemplateMethodPattern
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/TemplateMethodPattern/src/example/Client.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 | PlayerA playA = new PlayerA();
7 | playA.process();
8 | System.out.println("-------------");
9 | PlayerB playB = new PlayerB();
10 | playB.process();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/TemplateMethodPattern/src/example/DNFHelper.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public abstract class DNFHelper {
4 |
5 | void openPC() {
6 | System.out.println("打开电脑...");
7 | };
8 |
9 | void openDNFClient() {
10 | System.out.println("打开DNF客户端...");
11 | }
12 |
13 | void loginDNF() {
14 | System.out.println("登录DNF账号...");
15 | }
16 |
17 | abstract void play();
18 |
19 | void process() {
20 | this.openPC();
21 | this.openDNFClient();
22 | this.loginDNF();
23 | this.play();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/TemplateMethodPattern/src/example/PlayerA.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class PlayerA extends DNFHelper{
4 |
5 | @Override
6 | void play() {
7 | System.out.println("玩家A开始玩DNF...");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/TemplateMethodPattern/src/example/PlayerB.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | public class PlayerB extends DNFHelper{
4 |
5 | @Override
6 | void play() {
7 | System.out.println("玩家B开始玩DNF...");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/TemplateMethodPattern/src/templatemethod/AbstractClass.java:
--------------------------------------------------------------------------------
1 | package templatemethod;
2 |
3 | public abstract class AbstractClass {
4 |
5 | protected abstract void otherMethod();
6 |
7 | public void templateMethod() {
8 | this.otherMethod();
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/TemplateMethodPattern/src/templatemethod/Clinet.java:
--------------------------------------------------------------------------------
1 | package templatemethod;
2 |
3 | public class Clinet {
4 |
5 | public static void main(String[] args) {
6 |
7 | ConcreteClass c = new ConcreteClass();
8 | c.templateMethod();
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/TemplateMethodPattern/src/templatemethod/ConcreteClass.java:
--------------------------------------------------------------------------------
1 | package templatemethod;
2 |
3 | public class ConcreteClass extends AbstractClass{
4 |
5 | @Override
6 | protected void otherMethod() {
7 | System.out.println("otherMethod...");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------