Skip to content

Commit 0e3ee75

Browse files
committed
Java设计模式,初步版本。
0 parents  commit 0e3ee75

25 files changed

+1844
-0
lines changed

DS01_SimpleFactory.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//녜蹶끓틔
2+
interface Car
3+
{
4+
public void drive();
5+
}
6+
7+
//야竟끓틔잚
8+
class Benz implements Car{
9+
public void drive() {
10+
System.out.println("Driving Benz ");
11+
}
12+
}
13+
14+
class Bmw implements Car{
15+
public void drive() {
16+
System.out.println("Driving Bmw ");
17+
}
18+
}
19+
20+
//묏낍잚실
21+
class Driver{
22+
public static Car driverCar(String s)throws Exception {
23+
if(s.equalsIgnoreCase("Benz"))
24+
return new Benz();
25+
else if(s.equalsIgnoreCase("Bmw"))
26+
return new Bmw();
27+
else throw new Exception();
28+
}
29+
30+
}
31+
32+
public class DS01_SimpleFactory{
33+
public static void main(String[] args)
34+
{
35+
try{
36+
Car car = Driver.driverCar("Benz");
37+
car.drive();
38+
}
39+
catch (Exception e){
40+
}
41+
}
42+
}

DS02_FactoryMethod.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//녜蹶묏낍
2+
interface Driver{
3+
public Car driverCar();
4+
}
5+
6+
//야竟묏낍잚
7+
class BenzDriver implements Driver{
8+
public Car driverCar(){
9+
return new Benz();
10+
}
11+
}
12+
13+
class BmwDriver implements Driver{
14+
public Car driverCar(){
15+
return new Bmw();
16+
}
17+
}
18+
19+
//녜蹶끓틔
20+
interface Car
21+
{
22+
public void drive();
23+
}
24+
25+
//야竟끓틔잚
26+
class Benz implements Car{
27+
public void drive() {
28+
System.out.println("Driving Benz ");
29+
}
30+
}
31+
32+
class Bmw implements Car{
33+
public void drive() {
34+
System.out.println("Driving Bmw ");
35+
}
36+
}
37+
38+
public class DS02_FactoryMethod
39+
{
40+
public static void main(String[] args)
41+
{
42+
try{
43+
Driver driver = new BenzDriver();
44+
Car car = driver.driverCar();
45+
car.drive();
46+
47+
}
48+
catch (Exception e){
49+
System.out.println(e.getMessage());
50+
}
51+
}
52+
}

DS03_AbstractFactory.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
//녜蹶끓틔
3+
interface ICat {
4+
5+
void eat();
6+
}
7+
8+
interface IDog {
9+
10+
void eat();
11+
}
12+
13+
//야竟끓틔
14+
class BlackCat implements ICat {
15+
16+
public void eat() {
17+
System.out.println("The black cat is eating!");
18+
}
19+
}
20+
21+
class WhiteCat implements ICat {
22+
23+
public void eat() {
24+
System.out.println("The white cat is eating! ");
25+
}
26+
}
27+
28+
class BlackDog implements IDog {
29+
30+
public void eat() {
31+
System.out.println("The black dog is eating");
32+
}
33+
}
34+
35+
class WhiteDog implements IDog {
36+
37+
public void eat() {
38+
System.out.println("The white dog is eating!");
39+
}
40+
}
41+
42+
43+
// 녜蹶묏낍
44+
interface IAnimalFactory{
45+
46+
ICat createCat();
47+
48+
IDog createDog();
49+
}
50+
51+
//야竟묏낍
52+
class BlackAnimalFactory implements IAnimalFactory {
53+
54+
public ICat createCat() {
55+
return new BlackCat();
56+
}
57+
58+
public IDog createDog() {
59+
return new BlackDog();
60+
}
61+
}
62+
63+
64+
class WhiteAnimalFactory implements IAnimalFactory {
65+
66+
public ICat createCat() {
67+
return new WhiteCat();
68+
}
69+
70+
public IDog createDog() {
71+
return new WhiteDog();
72+
}
73+
}
74+
75+
public class DS03_AbstractFactory{
76+
public static void main(String[] args) {
77+
IAnimalFactory blackAnimalFactory = new BlackAnimalFactory();
78+
ICat blackCat = blackAnimalFactory.createCat();
79+
blackCat.eat();
80+
IDog blackDog = blackAnimalFactory.createDog();
81+
blackDog.eat();
82+
83+
IAnimalFactory whiteAnimalFactory = new WhiteAnimalFactory();
84+
ICat whiteCat = whiteAnimalFactory.createCat();
85+
whiteCat.eat();
86+
IDog whiteDog = whiteAnimalFactory.createDog();
87+
whiteDog.eat();
88+
}
89+
}
90+
91+
92+
93+

DS04_Builder.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
interface PersonBuilder {
2+
3+
void buildHead();
4+
5+
void buildBody();
6+
7+
void buildFoot();
8+
9+
Person buildPerson();
10+
}
11+
//ConcreteBuilder
12+
13+
14+
class ManBuilder implements PersonBuilder {
15+
16+
Person person;
17+
18+
public ManBuilder() {
19+
person = new Man();
20+
}
21+
22+
public void buildBody() {
23+
person.setBody("建造男人的身体");
24+
}
25+
26+
public void buildFoot() {
27+
person.setFoot("建造男人的脚");
28+
}
29+
30+
public void buildHead() {
31+
person.setHead("建造*人的头");
32+
}
33+
34+
public Person buildPerson() {
35+
return person;
36+
}
37+
}
38+
39+
40+
//Director
41+
class PersonDirector {
42+
43+
public Person constructPerson(PersonBuilder pb) {
44+
pb.buildHead();
45+
pb.buildBody();
46+
pb.buildFoot();
47+
return pb.buildPerson();
48+
}
49+
}
50+
51+
//Product
52+
class Person {
53+
54+
private String head;
55+
56+
private String body;
57+
58+
private String foot;
59+
60+
public String getHead() {
61+
return head;
62+
}
63+
64+
public void setHead(String head) {
65+
this.head = head;
66+
}
67+
68+
public String getBody() {
69+
return body;
70+
}
71+
72+
public void setBody(String body) {
73+
this.body = body;
74+
}
75+
76+
public String getFoot() {
77+
return foot;
78+
}
79+
80+
public void setFoot(String foot) {
81+
this.foot = foot;
82+
}
83+
}
84+
85+
class Man extends Person {
86+
87+
}
88+
89+
90+
public class DS04_Builder{
91+
92+
public static void main(String[] args) {
93+
PersonDirector pd = new PersonDirector();
94+
Person person = pd.constructPerson(new ManBuilder());
95+
System.out.println(person.getBody());
96+
System.out.println(person.getFoot());
97+
System.out.println(person.getHead());
98+
}
99+
}

DS04_Singleton.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.util.HashMap;
2+
class Singleton
3+
{
4+
//用来存放对应关系
5+
private static HashMap sinRegistry = new HashMap();
6+
static private Singleton s = new Singleton();
7+
//受保护的构造函数
8+
protected Singleton()
9+
{}
10+
public static Singleton getInstance(String name)
11+
{
12+
if(name == null)
13+
name = "Singleton";
14+
if(sinRegistry.get(name)==null)
15+
{
16+
try{
17+
sinRegistry.put(name , Class.forName(name).newInstance());
18+
}catch(Exception e)
19+
{
20+
e.printStackTrace();
21+
}
22+
}
23+
return (Singleton)(sinRegistry.get(name));
24+
}
25+
26+
public void test()
27+
{
28+
System.out.println("getclasssuccess!");
29+
}
30+
}
31+
32+
class SingletonChild1 extends Singleton
33+
{
34+
public SingletonChild1(){}
35+
static public SingletonChild1 getInstance()
36+
{
37+
return (SingletonChild1)Singleton.getInstance("SingletonChild1");
38+
}
39+
public void test()
40+
{
41+
System.out.println("getclasssuccess111!");
42+
}
43+
}
44+
45+
class SingletonChild2 extends Singleton
46+
{
47+
public SingletonChild1(){}
48+
static public SingletonChild1 getInstance()
49+
{
50+
return (SingletonChild1)Singleton.getInstance("SingletonChild2");
51+
}
52+
public void test()
53+
{
54+
System.out.println("getclasssuccess222!");
55+
}
56+
}
57+
58+
public class DS04_Singleton{
59+
60+
}

DS05_Prototyte.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Prototype implements Cloneable {
2+
3+
private String name;
4+
5+
public void setName(String name) {
6+
this.name = name;
7+
}
8+
9+
public String getName() {
10+
return this.name;
11+
}
12+
13+
public Object clone(){
14+
try {
15+
return super.clone();
16+
} catch (Exception e) {
17+
e.printStackTrace();
18+
return null;
19+
}
20+
}
21+
}
22+
23+
class ConcretePrototype extends Prototype {
24+
25+
public ConcretePrototype(String name) {
26+
setName(name);
27+
}
28+
}
29+
30+
public class DS05_Prototyte {
31+
32+
public static void main(String[] args) {
33+
Prototype pro = new ConcretePrototype("prototype");
34+
Prototype pro2 = (Prototype)pro.clone();
35+
System.out.println(pro.getName());
36+
System.out.println(pro2.getName());
37+
}
38+
}

0 commit comments

Comments
 (0)