Skip to content

Commit 4842fc4

Browse files
authored
工厂方法
工厂方法
1 parent f873122 commit 4842fc4

File tree

7 files changed

+73
-0
lines changed

7 files changed

+73
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.java.design.factorymethod;
2+
3+
/**
4+
* 工厂方法 ------> 具体的工厂对象负责生产具体的产品对象
5+
*
6+
* @author Administrator
7+
*
8+
*/
9+
public class FactoryMethodPattern {
10+
11+
public static void main(String[] args) {
12+
13+
StudentWorkFactory studentWorkFactory = new StudentWorkFactory();
14+
studentWorkFactory.getWork().doWork();
15+
16+
TeacherWorkFactory teacherWorkFactory = new TeacherWorkFactory();
17+
teacherWorkFactory.getWork().doWork();
18+
}
19+
20+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.java.design.factorymethod;
2+
3+
public interface IWorkFactory {
4+
5+
Work getWork();
6+
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.java.design.factorymethod;
2+
3+
public class StudentWork implements Work {
4+
5+
@Override
6+
public void doWork() {
7+
System.out.println("学生在做作业!");
8+
9+
}
10+
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.java.design.factorymethod;
2+
3+
public class StudentWorkFactory implements IWorkFactory {
4+
5+
@Override
6+
public Work getWork() {
7+
return new StudentWork();
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.java.design.factorymethod;
2+
3+
public class TeacherWork implements Work {
4+
5+
@Override
6+
public void doWork() {
7+
System.out.println("老师在批改作业!");
8+
}
9+
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.java.design.factorymethod;
2+
3+
public class TeacherWorkFactory implements IWorkFactory {
4+
5+
@Override
6+
public Work getWork() {
7+
return new TeacherWork();
8+
}
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.java.design.factorymethod;
2+
3+
public interface Work {
4+
5+
void doWork();
6+
7+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy