Skip to content

Commit 39c8c76

Browse files
authored
抽象工厂模式
抽象工厂模式
1 parent f81e318 commit 39c8c76

File tree

10 files changed

+122
-0
lines changed

10 files changed

+122
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.java.design.abstractfactory;
2+
3+
4+
/**
5+
* 抽象工厂模式 ----- > 就是对一组具有相同主题的工厂进行封装(维基百科解释的很到位);
6+
*
7+
* 例如:生产一台PC机,使用工厂方法模式的话,一般会有cpu工厂,内存工厂,显卡工厂...但是使用抽象工厂模式的话,只有一个工厂就是PC工厂,
8+
* 但是一个PC工厂涵盖了cpu工厂,内存工厂,显卡工厂等要做的所有事;
9+
*
10+
* @author Administrator
11+
*
12+
*/
13+
public class AbstractFactoryPattern {
14+
15+
public static void main(String[] args) {
16+
17+
IAnimalFactory blackAnimalFactory = new BlackAnimalFactory();
18+
ICat blackCat = blackAnimalFactory.createCat();
19+
IDog blackDog = blackAnimalFactory.createDog();
20+
blackCat.eat();
21+
blackDog.eat();
22+
23+
IAnimalFactory whiteAnimalFactory = new WhiteAnimalFactory();
24+
ICat whiteCat = whiteAnimalFactory.createCat();
25+
IDog whiteDog = whiteAnimalFactory.createDog();
26+
whiteCat.eat();
27+
whiteDog.eat();
28+
29+
}
30+
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.java.design.abstractfactory;
2+
3+
public class BlackAnimalFactory implements IAnimalFactory {
4+
5+
@Override
6+
public ICat createCat() {
7+
return new BlackCat();
8+
}
9+
10+
@Override
11+
public IDog createDog() {
12+
return new BlackDog();
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.java.design.abstractfactory;
2+
3+
public class BlackCat implements ICat {
4+
5+
@Override
6+
public void eat() {
7+
8+
System.out.println("Black Cat eating...");
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.java.design.abstractfactory;
2+
3+
public class BlackDog implements IDog {
4+
5+
@Override
6+
public void eat() {
7+
8+
System.out.println("Black Dog eating...");
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.abstractfactory;
2+
3+
public interface IAnimalFactory {
4+
5+
ICat createCat();
6+
7+
IDog createDog();
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.abstractfactory;
2+
3+
public interface ICat {
4+
5+
void eat();
6+
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.java.design.abstractfactory;
2+
3+
public interface IDog {
4+
5+
void eat();
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.java.design.abstractfactory;
2+
3+
public class WhiteAnimalFactory implements IAnimalFactory {
4+
5+
@Override
6+
public ICat createCat() {
7+
return new WhiteCat();
8+
}
9+
10+
@Override
11+
public IDog createDog() {
12+
return new WhiteDog();
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.java.design.abstractfactory;
2+
3+
public class WhiteCat implements ICat {
4+
5+
@Override
6+
public void eat() {
7+
8+
System.out.println("White Cat eating...");
9+
}
10+
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.java.design.abstractfactory;
2+
3+
public class WhiteDog implements IDog {
4+
5+
@Override
6+
public void eat() {
7+
8+
System.out.println("White Dog eating...");
9+
}
10+
}

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