Skip to content

Commit b8850d9

Browse files
committed
二十三种设计模式JAVA实现
二十三种设计模式JAVA实现
1 parent 379b94a commit b8850d9

File tree

264 files changed

+2114
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

264 files changed

+2114
-0
lines changed

.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>design_pattern</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8

.settings/org.eclipse.jdt.core.prefs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
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.7

bin/js/com/adapter/Adaptee.class

512 Bytes
Binary file not shown.

bin/js/com/adapter/Adapter.class

494 Bytes
Binary file not shown.

bin/js/com/adapter/Client.class

536 Bytes
Binary file not shown.

bin/js/com/adapter/Target.class

501 Bytes
Binary file not shown.

bin/js/com/bridge/Abstraction.class

597 Bytes
Binary file not shown.

bin/js/com/bridge/Client.class

738 Bytes
Binary file not shown.
592 Bytes
Binary file not shown.
592 Bytes
Binary file not shown.

bin/js/com/bridge/Implementor.class

143 Bytes
Binary file not shown.
493 Bytes
Binary file not shown.

bin/js/com/build/Builder.class

365 Bytes
Binary file not shown.

bin/js/com/build/Client.class

916 Bytes
Binary file not shown.
742 Bytes
Binary file not shown.
742 Bytes
Binary file not shown.

bin/js/com/build/Director.class

509 Bytes
Binary file not shown.

bin/js/com/build/Product.class

1004 Bytes
Binary file not shown.
1.04 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
506 Bytes
Binary file not shown.

bin/js/com/command/Client.class

793 Bytes
Binary file not shown.

bin/js/com/command/Command.class

405 Bytes
Binary file not shown.
527 Bytes
Binary file not shown.

bin/js/com/command/Invoker.class

583 Bytes
Binary file not shown.

bin/js/com/command/Receiver.class

509 Bytes
Binary file not shown.

bin/js/com/composite/Client.class

1.05 KB
Binary file not shown.

bin/js/com/composite/Component.class

469 Bytes
Binary file not shown.

bin/js/com/composite/Composite.class

1.29 KB
Binary file not shown.

bin/js/com/composite/Leaf.class

967 Bytes
Binary file not shown.

bin/js/com/decorator/Client.class

801 Bytes
Binary file not shown.

bin/js/com/decorator/Component.class

141 Bytes
Binary file not shown.
579 Bytes
Binary file not shown.
752 Bytes
Binary file not shown.
752 Bytes
Binary file not shown.

bin/js/com/decorator/Decorator.class

630 Bytes
Binary file not shown.

bin/js/com/facade/Client.class

690 Bytes
Binary file not shown.

bin/js/com/facade/Facade.class

832 Bytes
Binary file not shown.

bin/js/com/facade/SubSystem1.class

516 Bytes
Binary file not shown.

bin/js/com/facade/SubSystem2.class

516 Bytes
Binary file not shown.

bin/js/com/facade/SubSystem3.class

518 Bytes
Binary file not shown.
880 Bytes
Binary file not shown.
663 Bytes
Binary file not shown.
220 Bytes
Binary file not shown.
157 Bytes
Binary file not shown.
597 Bytes
Binary file not shown.
597 Bytes
Binary file not shown.
876 Bytes
Binary file not shown.
526 Bytes
Binary file not shown.
526 Bytes
Binary file not shown.
193 Bytes
Binary file not shown.
155 Bytes
Binary file not shown.
591 Bytes
Binary file not shown.
591 Bytes
Binary file not shown.
712 Bytes
Binary file not shown.
774 Bytes
Binary file not shown.
155 Bytes
Binary file not shown.
591 Bytes
Binary file not shown.
591 Bytes
Binary file not shown.

bin/js/com/fly_weight/Client.class

985 Bytes
Binary file not shown.
767 Bytes
Binary file not shown.

bin/js/com/fly_weight/FlyWeight.class

144 Bytes
Binary file not shown.
899 Bytes
Binary file not shown.
779 Bytes
Binary file not shown.
190 Bytes
Binary file not shown.

bin/js/com/interpreter/Client.class

1.14 KB
Binary file not shown.

bin/js/com/interpreter/Context.class

725 Bytes
Binary file not shown.
692 Bytes
Binary file not shown.
680 Bytes
Binary file not shown.

bin/js/com/iterator/Aggregate.class

171 Bytes
Binary file not shown.

bin/js/com/iterator/Client.class

1.21 KB
Binary file not shown.
1.23 KB
Binary file not shown.
1.06 KB
Binary file not shown.

bin/js/com/iterator/Iterator.class

212 Bytes
Binary file not shown.

bin/js/com/mediator/Client.class

1.02 KB
Binary file not shown.

bin/js/com/mediator/Colleague.class

397 Bytes
Binary file not shown.
1.01 KB
Binary file not shown.
1.01 KB
Binary file not shown.
1.12 KB
Binary file not shown.

bin/js/com/mediator/Mediator.class

179 Bytes
Binary file not shown.

bin/js/com/memento/Caretaker.class

559 Bytes
Binary file not shown.

bin/js/com/memento/Client.class

856 Bytes
Binary file not shown.

bin/js/com/memento/Memento.class

552 Bytes
Binary file not shown.

bin/js/com/memento/Originator.class

1.02 KB
Binary file not shown.

bin/js/com/observer/Client.class

917 Bytes
Binary file not shown.
1.25 KB
Binary file not shown.
583 Bytes
Binary file not shown.

bin/js/com/observer/Observer.class

136 Bytes
Binary file not shown.

bin/js/com/observer/Subject.class

1.18 KB
Binary file not shown.
839 Bytes
Binary file not shown.
259 Bytes
Binary file not shown.
493 Bytes
Binary file not shown.
188 Bytes
Binary file not shown.
921 Bytes
Binary file not shown.

bin/js/com/prototype/Client.class

749 Bytes
Binary file not shown.
562 Bytes
Binary file not shown.
1.05 KB
Binary file not shown.

bin/js/com/prototype/Prototype.class

723 Bytes
Binary file not shown.

bin/js/com/proxy/Client.class

572 Bytes
Binary file not shown.

bin/js/com/proxy/Proxy.class

693 Bytes
Binary file not shown.

bin/js/com/proxy/RealSubject.class

549 Bytes
Binary file not shown.

bin/js/com/proxy/Subject.class

132 Bytes
Binary file not shown.

bin/js/com/singleton/Hungry.class

478 Bytes
Binary file not shown.

bin/js/com/singleton/Lazy.class

513 Bytes
Binary file not shown.

bin/js/com/state/Client.class

589 Bytes
Binary file not shown.

bin/js/com/state/ConcreteStateA.class

584 Bytes
Binary file not shown.

bin/js/com/state/ConcreteStateB.class

584 Bytes
Binary file not shown.

bin/js/com/state/Context.class

967 Bytes
Binary file not shown.

bin/js/com/state/State.class

149 Bytes
Binary file not shown.

bin/js/com/strategy/Client.class

677 Bytes
Binary file not shown.

bin/js/com/strategy/Context.class

532 Bytes
Binary file not shown.

bin/js/com/strategy/Strategy.class

139 Bytes
Binary file not shown.

bin/js/com/strategy/StrategyA.class

551 Bytes
Binary file not shown.

bin/js/com/strategy/StrategyB.class

551 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
651 Bytes
Binary file not shown.
602 Bytes
Binary file not shown.
698 Bytes
Binary file not shown.

bin/js/com/visitor/Client.class

933 Bytes
Binary file not shown.
588 Bytes
Binary file not shown.
588 Bytes
Binary file not shown.
1015 Bytes
Binary file not shown.
1015 Bytes
Binary file not shown.

bin/js/com/visitor/Element.class

157 Bytes
Binary file not shown.
1.11 KB
Binary file not shown.

bin/js/com/visitor/Visitor.class

252 Bytes
Binary file not shown.

src/js/com/adapter/Adaptee.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package js.com.adapter;
2+
3+
/**
4+
* Created by Administrator on 2015/4/23.
5+
*/
6+
public class Adaptee {
7+
public void specificRequest(){
8+
System.out.println("特殊请求");
9+
}
10+
}

src/js/com/adapter/Adapter.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package js.com.adapter;
2+
3+
/**
4+
* Created by Administrator on 2015/4/23.
5+
*/
6+
public class Adapter extends Target{
7+
private Adaptee adaptee = new Adaptee();
8+
9+
@Override
10+
public void request() {
11+
adaptee.specificRequest();
12+
}
13+
}

src/js/com/adapter/Client.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package js.com.adapter;
2+
3+
/**
4+
* 适配器模式
5+
* Created by Administrator on 2015/4/23.
6+
*/
7+
public class Client {
8+
public static void main(String[] args) {
9+
Target target = new Adapter();
10+
target.request();
11+
}
12+
}

src/js/com/adapter/Target.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package js.com.adapter;
2+
3+
/**
4+
* Created by Administrator on 2015/4/23.
5+
*/
6+
public class Target {
7+
public void request(){
8+
System.out.println("普通请求");
9+
}
10+
}

src/js/com/bridge/Abstraction.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package js.com.bridge;
2+
3+
/**
4+
* Created by Administrator on 2015/4/23.
5+
*/
6+
public class Abstraction {
7+
protected Implementor implementor;
8+
9+
public void setImplementor(Implementor implementor) {
10+
this.implementor = implementor;
11+
}
12+
13+
public void operation(){
14+
implementor.operation();
15+
}
16+
}

src/js/com/bridge/Client.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package js.com.bridge;
2+
3+
/**
4+
* 桥接模式
5+
* Created by Administrator on 2015/4/23.
6+
*/
7+
public class Client {
8+
public static void main(String[] args) {
9+
Abstraction ab = new RefinedAbstraction();
10+
ab.setImplementor(new ConcreteImplementorA());
11+
ab.operation();
12+
13+
ab.setImplementor(new ConcreteImplementorB());
14+
ab.operation();
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package js.com.bridge;
2+
3+
/**
4+
* Created by Administrator on 2015/4/23.
5+
*/
6+
public class ConcreteImplementorA implements Implementor {
7+
@Override
8+
public void operation() {
9+
System.out.println("具体实现A的方法执行");
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package js.com.bridge;
2+
3+
/**
4+
* Created by Administrator on 2015/4/23.
5+
*/
6+
public class ConcreteImplementorB implements Implementor {
7+
@Override
8+
public void operation() {
9+
System.out.println("具体实现B的方法执行");
10+
}
11+
}

src/js/com/bridge/Implementor.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package js.com.bridge;
2+
3+
/**
4+
* Created by Administrator on 2015/4/23.
5+
*/
6+
public interface Implementor {
7+
public void operation();
8+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package js.com.bridge;
2+
3+
/**
4+
* Created by Administrator on 2015/4/23.
5+
*/
6+
public class RefinedAbstraction extends Abstraction {
7+
@Override
8+
public void operation() {
9+
implementor.operation();
10+
}
11+
}

src/js/com/build/Builder.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package js.com.build;
2+
3+
/**
4+
* Created by Administrator on 2015/4/22.
5+
*/
6+
public abstract class Builder {
7+
8+
public abstract void builderPartA();
9+
10+
public abstract void builderPartB();
11+
12+
public abstract Product getResult();
13+
}

src/js/com/build/Client.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package js.com.build;
2+
3+
/**
4+
* 创建模式,和工厂模式不同是这货用来创建复杂模型
5+
* 具体的Builder引用了Product,再让Director指挥builder去执行builder的创建方法。
6+
* 然后再自行返回Product
7+
* Created by Administrator on 2015/4/22.
8+
*/
9+
public class Client {
10+
public static void main(String[] args) {
11+
Director director = new Director();
12+
13+
Builder b1 = new ConcreteBuilder1();
14+
Builder b2 = new ConcreteBuilder2();
15+
16+
director.construct(b1);;
17+
Product p1 = b1.getResult();
18+
p1.show();
19+
20+
director.construct(b2);
21+
Product p2 = b2.getResult();
22+
p2.show();
23+
}
24+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package js.com.build;
2+
3+
/**
4+
* Created by Administrator on 2015/4/22.
5+
*/
6+
public class ConcreteBuilder1 extends Builder {
7+
8+
private Product product = new Product();
9+
10+
@Override
11+
public void builderPartA() {
12+
product.add("部件A");
13+
}
14+
15+
@Override
16+
public void builderPartB() {
17+
product.add("部件B");
18+
}
19+
20+
@Override
21+
public Product getResult() {
22+
return product;
23+
}
24+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package js.com.build;
2+
3+
/**
4+
* Created by Administrator on 2015/4/22.
5+
*/
6+
public class ConcreteBuilder2 extends Builder {
7+
8+
private Product product = new Product();
9+
10+
@Override
11+
public void builderPartA() {
12+
product.add("部件X");
13+
}
14+
15+
@Override
16+
public void builderPartB() {
17+
product.add("部件Y");
18+
}
19+
20+
@Override
21+
public Product getResult() {
22+
return product;
23+
}
24+
}

src/js/com/build/Director.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package js.com.build;
2+
3+
/**
4+
* Created by Administrator on 2015/4/22.
5+
*/
6+
public class Director {
7+
public void construct(Builder builder){
8+
builder.builderPartA();
9+
builder.builderPartB();
10+
}
11+
}

src/js/com/build/Product.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package js.com.build;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by Administrator on 2015/4/22.
7+
*/
8+
public class Product {
9+
List<String> parts = new ArrayList();
10+
public void add(String part){
11+
parts.add(part);
12+
}
13+
14+
public void show(){
15+
System.out.println("产品创建");
16+
for (int i = 0; i < parts.size(); i++) {
17+
System.out.println(parts.get(i));
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package js.com.chain_of_responsibility;
2+
3+
/**
4+
* 责任链模式
5+
* Created by Administrator on 2015/4/23.
6+
*/
7+
public class Client {
8+
public static void main(String[] args) {
9+
Handler h1 = new ConcreteHandle1();
10+
Handler h2 = new ConcreteHandle2();
11+
Handler h3 = new ConcreteHandle3();
12+
h1.setSuccessor(h2);
13+
h2.setSuccessor(h3);
14+
15+
int[] requests = new int[]{2, 5, 12, 7, 28, 23, 18, 11};
16+
17+
for(int i=0; i<requests.length; i++){
18+
h1.handleRequest(requests[i]);
19+
}
20+
21+
}
22+
}
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package js.com.chain_of_responsibility;
2+
3+
/**
4+
* Created by Administrator on 2015/4/23.
5+
*/
6+
public class ConcreteHandle1 extends Handler {
7+
8+
@Override
9+
public void handleRequest(int request) {
10+
if(request >= 0 && request < 10){
11+
System.out.println(this.getClass().getSimpleName() + " handle " + request);
12+
}else if (successor != null){
13+
successor.handleRequest(request);
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package js.com.chain_of_responsibility;
2+
3+
/**
4+
* Created by Administrator on 2015/4/23.
5+
*/
6+
public class ConcreteHandle2 extends Handler {
7+
@Override
8+
public void handleRequest(int request) {
9+
if(request >= 10 && request < 20){
10+
System.out.println(this.getClass().getSimpleName() + " handle " + request);
11+
}else if (successor != null){
12+
successor.handleRequest(request);
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package js.com.chain_of_responsibility;
2+
3+
/**
4+
* Created by Administrator on 2015/4/23.
5+
*/
6+
public class ConcreteHandle3 extends Handler{
7+
@Override
8+
public void handleRequest(int request) {
9+
if(request >= 20 && request < 30){
10+
System.out.println(this.getClass().getSimpleName() + " handle " + request);
11+
}else if (successor != null){
12+
successor.handleRequest(request);
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package js.com.chain_of_responsibility;
2+
3+
/**
4+
* Created by Administrator on 2015/4/23.
5+
*/
6+
public abstract class Handler {
7+
8+
protected Handler successor;
9+
10+
public void setSuccessor(Handler successor){
11+
this.successor = successor;
12+
}
13+
14+
public abstract void handleRequest(int request);
15+
16+
}

src/js/com/command/Client.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package js.com.command;
2+
3+
/**
4+
* 命令模式
5+
* Created by Administrator on 2015/4/23.
6+
*/
7+
public class Client {
8+
public static void main(String[] args) {
9+
Receiver r = new Receiver();
10+
Command c = new ConcreteCommand(r);
11+
Invoker i = new Invoker();
12+
i.setCommand(c);
13+
i.executeCommand();
14+
}
15+
}

0 commit comments

Comments
 (0)