Skip to content

Commit 03d4f6a

Browse files
author
huxujun
committed
完善抽象工厂模式
1 parent 8319538 commit 03d4f6a

File tree

8 files changed

+348
-153
lines changed

8 files changed

+348
-153
lines changed

.idea/workspace.xml

Lines changed: 319 additions & 151 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/com/sigalhu/abstractfactory/AbstractProductA.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
public abstract class AbstractProductA {
44
//每个产品共有的方法
5-
public void shareMethod(){}
5+
public void shareMethod(){
6+
}
67

78
//每个产品相同方法,不同实现
89
public abstract void doSomething();

src/com/sigalhu/abstractfactory/AbstractProductB.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
public abstract class AbstractProductB {
44
//每个产品共有的方法
5-
public void shareMethod(){}
5+
public void shareMethod(){
6+
}
67

78
//每个产品相同方法,不同实现
89
public abstract void doSomething();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.sigalhu.abstractfactory;
2+
3+
public class Client {
4+
public static void main(String[] args){
5+
//定义出两个工厂
6+
AbstractCreator creator1 = new Creator1();
7+
AbstractCreator creator2 = new Creator2();
8+
//产生A1的对象
9+
AbstractProductA a1 = creator1.createProductA();
10+
a1.doSomething();
11+
//产生A2的对象
12+
AbstractProductA a2 = creator2.createProductA();
13+
a2.doSomething();
14+
//产生B1的对象
15+
AbstractProductB b1 = creator1.createProductB();
16+
b1.doSomething();
17+
//产生B2的对象
18+
AbstractProductB b2 = creator2.createProductB();
19+
b2.doSomething();
20+
}
21+
}

src/com/sigalhu/abstractfactory/ProductA1.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public class ProductA1 extends AbstractProductA {
44
@Override
55
public void doSomething() {
6+
System.out.println("产品A1的实现方法");
67
}
78
}

src/com/sigalhu/abstractfactory/ProductA2.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public class ProductA2 extends AbstractProductA {
44
@Override
55
public void doSomething() {
6+
System.out.println("产品A2的实现方法");
67
}
78
}

src/com/sigalhu/abstractfactory/ProductB1.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public class ProductB1 extends AbstractProductB {
44
@Override
55
public void doSomething() {
6+
System.out.println("产品B1的实现方法");
67
}
78
}

src/com/sigalhu/abstractfactory/ProductB2.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public class ProductB2 extends AbstractProductB {
44
@Override
55
public void doSomething() {
6+
System.out.println("产品B2的实现方法");
67
}
78
}

0 commit comments

Comments
 (0)