File tree Expand file tree Collapse file tree 7 files changed +118
-0
lines changed
src/main/java/com/crossoverjie/design/pattern/factorymethod Expand file tree Collapse file tree 7 files changed +118
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .factorymethod ;
2
+
3
+ /**
4
+ * Function:
5
+ *
6
+ * @author crossoverJie
7
+ * Date: 19/03/2018 14:29
8
+ * @since JDK 1.8
9
+ */
10
+ public abstract class Animal {
11
+
12
+ private String name ;
13
+
14
+ public String getName () {
15
+ return name ;
16
+ }
17
+
18
+ public void setName (String name ) {
19
+ this .name = name ;
20
+ }
21
+
22
+ /**
23
+ * 描述抽象方法
24
+ */
25
+ protected abstract void desc () ;
26
+ }
Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .factorymethod ;
2
+
3
+ /**
4
+ * Function:工厂方法模式
5
+ *
6
+ * @author crossoverJie
7
+ * Date: 19/03/2018 14:29
8
+ * @since JDK 1.8
9
+ */
10
+ public interface AnimalFactory {
11
+
12
+ Animal createAnimal () ;
13
+ }
Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .factorymethod ;
2
+
3
+ /**
4
+ * Function:
5
+ *
6
+ * @author crossoverJie
7
+ * Date: 19/03/2018 14:33
8
+ * @since JDK 1.8
9
+ */
10
+ public class Cat extends Animal {
11
+ @ Override
12
+ protected void desc () {
13
+ System .out .println ("Cat name is=" + this .getName ());
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .factorymethod ;
2
+
3
+ /**
4
+ * Function:
5
+ *
6
+ * @author crossoverJie
7
+ * Date: 19/03/2018 15:21
8
+ * @since JDK 1.8
9
+ */
10
+ public class CatFactory implements AnimalFactory {
11
+ @ Override
12
+ public Animal createAnimal () {
13
+ return new Cat ();
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .factorymethod ;
2
+
3
+ /**
4
+ * Function:
5
+ *
6
+ * @author crossoverJie
7
+ * Date: 19/03/2018 14:32
8
+ * @since JDK 1.8
9
+ */
10
+ public class Fish extends Animal {
11
+
12
+
13
+ @ Override
14
+ protected void desc () {
15
+ System .out .println ("fish name is=" + this .getName ());
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .factorymethod ;
2
+
3
+ /**
4
+ * Function:
5
+ *
6
+ * @author crossoverJie
7
+ * Date: 19/03/2018 15:21
8
+ * @since JDK 1.8
9
+ */
10
+ public class FishFactory implements AnimalFactory {
11
+ @ Override
12
+ public Animal createAnimal () {
13
+ return new Fish () ;
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .factorymethod ;
2
+
3
+ /**
4
+ * Function:
5
+ *
6
+ * @author crossoverJie
7
+ * Date: 19/03/2018 14:34
8
+ * @since JDK 1.8
9
+ */
10
+ public class Main {
11
+ public static void main (String [] args ) {
12
+ AnimalFactory factory = new CatFactory () ;
13
+ Animal animal = factory .createAnimal ();
14
+ animal .setName ("猫咪" );
15
+ animal .desc ();
16
+ }
17
+ }
You can’t perform that action at this time.
0 commit comments