Skip to content

Commit bf5b52c

Browse files
committed
✨ Introducing new features. 设计模式--->工厂方法模式
1 parent b63325c commit bf5b52c

File tree

7 files changed

+118
-0
lines changed

7 files changed

+118
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

0 commit comments

Comments
 (0)