Skip to content

Commit 6fea94e

Browse files
committed
设计模式之Factory系列,FactoryMethod, AbstractFactory,以及spring bean工厂开头
1 parent ea2001b commit 6fea94e

File tree

31 files changed

+1283
-316
lines changed

31 files changed

+1283
-316
lines changed

.idea/compiler.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 499 additions & 312 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DesignPatterns.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3-
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
44
<output url="file://$MODULE_DIR$/target/classes" />
55
<output-test url="file://$MODULE_DIR$/target/test-classes" />
66
<content url="file://$MODULE_DIR$">
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
其实单例模式也是一种工厂,
2+
静态的工厂方法,有人称之为,静态工厂
3+
4+
线程池,连接池,各种池化技术,也是一种工厂,有人称之为“多例”
5+
6+
不要咬文嚼字,不要生搬硬套,理解概念,灵活运用
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.mashibing.dp.composite;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
abstract class Node {
7+
abstract public void p();
8+
}
9+
10+
class LeafNode extends Node {
11+
String content;
12+
public LeafNode(String content) {this.content = content;}
13+
14+
@Override
15+
public void p() {
16+
System.out.println(content);
17+
}
18+
}
19+
20+
class BranchNode extends Node {
21+
List<Node> nodes = new ArrayList<>();
22+
23+
String name;
24+
public BranchNode(String name) {this.name = name;}
25+
26+
@Override
27+
public void p() {
28+
System.out.println(name);
29+
}
30+
31+
public void add(Node n) {
32+
nodes.add(n);
33+
}
34+
}
35+
36+
37+
public class Main {
38+
public static void main(String[] args) {
39+
40+
BranchNode root = new BranchNode("root");
41+
BranchNode chapter1 = new BranchNode("chapter1");
42+
BranchNode chapter2 = new BranchNode("chapter2");
43+
Node r1 = new LeafNode("r1");
44+
Node c11 = new LeafNode("c11");
45+
Node c12 = new LeafNode("c12");
46+
BranchNode b21 = new BranchNode("section21");
47+
Node c211 = new LeafNode("c211");
48+
Node c212 = new LeafNode("c212");
49+
50+
root.add(chapter1);
51+
root.add(chapter2);
52+
root.add(r1);
53+
chapter1.add(c11);
54+
chapter1.add(c12);
55+
chapter2.add(b21);
56+
b21.add(c211);
57+
b21.add(c212);
58+
59+
tree(root, 0);
60+
61+
}
62+
63+
static void tree(Node b, int depth) {
64+
for(int i=0; i<depth; i++) System.out.print("--");
65+
b.p();
66+
67+
if(b instanceof BranchNode) {
68+
for (Node n : ((BranchNode)b).nodes) {
69+
tree(n, depth + 1);
70+
}
71+
}
72+
}
73+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.mashibing.dp.decorator;
2+
3+
import java.io.*;
4+
5+
public class Main {
6+
public static void main(String[] args) throws Exception {
7+
File f = new File("c:/work/test.data");
8+
FileOutputStream fos = new FileOutputStream(f);
9+
OutputStreamWriter osw = new OutputStreamWriter(fos);
10+
BufferedWriter bw = new BufferedWriter(osw);
11+
bw.write("http://www.mashibing.com");
12+
bw.flush();
13+
bw.close();
14+
}
15+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
OutputStream
2+
OutputStreamWriter
3+
BufferedWriter

src/main/java/com/mashibing/dp/factorymethod/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class Main {
44
public static void main(String[] args) {
5-
Moveable m = new CarFactory().createCar();
5+
Moveable m = new CarFactory().create();
66
m.go();
77
}
88
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.mashibing.dp.flyweight;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.UUID;
6+
7+
class Bullet{
8+
public UUID id = UUID.randomUUID();
9+
boolean living = true;
10+
11+
@Override
12+
public String toString() {
13+
return "Bullet{" +
14+
"id=" + id +
15+
'}';
16+
}
17+
}
18+
19+
public class BulletPool {
20+
List<Bullet> bullets = new ArrayList<>();
21+
{
22+
for(int i=0; i<5; i++) bullets.add(new Bullet());
23+
}
24+
25+
public Bullet getBullet() {
26+
for(int i=0; i<bullets.size(); i++) {
27+
Bullet b = bullets.get(i);
28+
if(!b.living) return b;
29+
}
30+
31+
return new Bullet();
32+
}
33+
34+
public static void main(String[] args) {
35+
BulletPool bp = new BulletPool();
36+
37+
for(int i=0; i<10; i++) {
38+
Bullet b = bp.getBullet();
39+
System.out.println(b);
40+
}
41+
}
42+
43+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.mashibing.dp.flyweight;
2+
3+
public class TestString {
4+
public static void main(String[] args) {
5+
String s1 = "abc";
6+
String s2 = "abc";
7+
String s3 = new String("abc");
8+
String s4 = new String("abc");
9+
10+
System.out.println(s1 == s2); //true
11+
System.out.println(s1 == s3); //false
12+
System.out.println(s3 == s4);
13+
System.out.println(s3.intern() == s1);
14+
System.out.println(s3.intern() == s4.intern());
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.mashibing.dp.observer.v1;
2+
3+
/**
4+
* 披着面向对象外衣的面向过程
5+
*/
6+
7+
public class Main1 {
8+
public static void main(String[] args) {
9+
boolean cry = false;
10+
11+
while(!cry) {
12+
//进行处理
13+
}
14+
}
15+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
认识javascript event对象
2+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
认识javascript event对象
2+
3+
在很多系统中,Observer模式往往和责任链共同负责对于事件的处理,
4+
其中的某一个observer负责是否将事件进一步传递
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.mashibing.dp.observer.v2;
2+
3+
/**
4+
* 面向对象的傻等
5+
*/
6+
7+
class Child {
8+
private boolean cry = false;
9+
10+
public boolean isCry() {
11+
return cry;
12+
}
13+
14+
public void wakeUp() {
15+
System.out.println("Waked Up! Crying wuwuwuwu...");
16+
cry = true;
17+
}
18+
}
19+
20+
public class Main {
21+
public static void main(String[] args) {
22+
Child child = new Child();
23+
while(!child.isCry()) {
24+
try {
25+
Thread.sleep(1000);
26+
} catch (InterruptedException e) {
27+
e.printStackTrace();
28+
}
29+
System.out.println("observing...");
30+
}
31+
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.mashibing.dp.observer.v3;
2+
3+
/**
4+
* 加入观察者
5+
*/
6+
7+
class Child {
8+
private boolean cry = false;
9+
private Dad d = new Dad();
10+
11+
public boolean isCry() {
12+
return cry;
13+
}
14+
15+
public void wakeUp() {
16+
cry = true;
17+
d.feed();
18+
}
19+
}
20+
21+
class Dad {
22+
public void feed() {
23+
System.out.println("dad feeding...");
24+
}
25+
}
26+
27+
public class Main {
28+
public static void main(String[] args) {
29+
Child c = new Child();
30+
//do sth
31+
c.wakeUp();
32+
}
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.mashibing.dp.observer.v4;
2+
3+
/**
4+
* 加入多个观察者
5+
*/
6+
7+
class Child {
8+
private boolean cry = false;
9+
private Dad dad = new Dad();
10+
private Mum mum = new Mum();
11+
private Dog dog = new Dog();
12+
13+
14+
public boolean isCry() {
15+
return cry;
16+
}
17+
18+
public void wakeUp() {
19+
cry = true;
20+
dad.feed();
21+
dog.wang();
22+
mum.hug();
23+
}
24+
}
25+
26+
class Dad {
27+
public void feed() {
28+
System.out.println("dad feeding...");
29+
}
30+
}
31+
32+
class Mum {
33+
public void hug() {
34+
System.out.println("mum hugging...");
35+
}
36+
}
37+
38+
class Dog {
39+
public void wang() {
40+
System.out.println("dog wang...");
41+
}
42+
}
43+
44+
public class Main {
45+
public static void main(String[] args) {
46+
Child c = new Child();
47+
//do sth
48+
c.wakeUp();
49+
}
50+
}

0 commit comments

Comments
 (0)