Skip to content

Commit f8f1091

Browse files
committed
add design pattern
1 parent 810b15a commit f8f1091

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+383
-77
lines changed

src/com/baidu/PrintAB.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static void main(String[] args) {
66
}
77

88
/**
9-
* 这个解法自己想的有点弱智了!
9+
* 这个解法自己想的有点弱智了!
1010
*/
1111
// public void printAB(){
1212
// if(new PrintAB(){
@@ -22,7 +22,7 @@ public static void main(String[] args) {
2222
// }
2323

2424
/**
25-
* 借鉴的解法
25+
* 借鉴的解法
2626
*/
2727
public void printAB(){
2828
if(System.out.append("a")==null){
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.duwei.designpattern.absfactory;
2+
3+
public abstract class AbstractFactory {
4+
public abstract Color getColor(String color);
5+
public abstract Shape getShape(String shape) ;
6+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.duwei.designpattern.absfactory;
2+
3+
public class AbstractFactoryPatternDemo {
4+
public static void main(String[] args) {
5+
6+
//获取形状工厂
7+
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
8+
9+
//获取形状为 Circle 的对象
10+
Shape shape1 = shapeFactory.getShape("CIRCLE");
11+
12+
//调用 Circle 的 draw 方法
13+
shape1.draw();
14+
15+
//获取形状为 Rectangle 的对象
16+
Shape shape2 = shapeFactory.getShape("RECTANGLE");
17+
18+
//调用 Rectangle 的 draw 方法
19+
shape2.draw();
20+
21+
//获取形状为 Square 的对象
22+
Shape shape3 = shapeFactory.getShape("SQUARE");
23+
24+
//调用 Square 的 draw 方法
25+
shape3.draw();
26+
27+
//获取颜色工厂
28+
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
29+
30+
//获取颜色为 Red 的对象
31+
Color color1 = colorFactory.getColor("RED");
32+
33+
//调用 Red 的 fill 方法
34+
color1.fill();
35+
36+
//获取颜色为 Green 的对象
37+
Color color2 = colorFactory.getColor("Green");
38+
39+
//调用 Green 的 fill 方法
40+
color2.fill();
41+
42+
//获取颜色为 Blue 的对象
43+
Color color3 = colorFactory.getColor("BLUE");
44+
45+
//调用 Blue 的 fill 方法
46+
color3.fill();
47+
}
48+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.duwei.designpattern.absfactory;
2+
3+
public class Blue implements Color {
4+
5+
@Override
6+
public void fill() {
7+
System.out.println("Inside Blue::fill() method.");
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.duwei.designpattern.absfactory;
2+
3+
public class Circle implements Shape {
4+
5+
@Override
6+
public void draw() {
7+
System.out.println("Inside Circle::draw() method.");
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.duwei.designpattern.absfactory;
2+
3+
public interface Color {
4+
void fill();
5+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.duwei.designpattern.absfactory;
2+
3+
public class ColorFactory extends AbstractFactory {
4+
5+
@Override
6+
public Shape getShape(String shapeType){
7+
return null;
8+
}
9+
10+
@Override
11+
public Color getColor(String color) {
12+
if(color == null){
13+
return null;
14+
}
15+
if(color.equalsIgnoreCase("RED")){
16+
return new Red();
17+
} else if(color.equalsIgnoreCase("GREEN")){
18+
return new Green();
19+
} else if(color.equalsIgnoreCase("BLUE")){
20+
return new Blue();
21+
}
22+
return null;
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.duwei.designpattern.absfactory;
2+
3+
public class FactoryProducer {
4+
public static AbstractFactory getFactory(String choice){
5+
if(choice.equalsIgnoreCase("SHAPE")){
6+
return new ShapeFactory();
7+
} else if(choice.equalsIgnoreCase("COLOR")){
8+
return new ColorFactory();
9+
}
10+
return null;
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.duwei.designpattern.absfactory;
2+
3+
public class Green implements Color {
4+
5+
@Override
6+
public void fill() {
7+
System.out.println("Inside Green::fill() method.");
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.duwei.designpattern.absfactory;
2+
3+
public class Rectangle implements Shape {
4+
5+
@Override
6+
public void draw() {
7+
System.out.println("Inside Rectangle::draw() method.");
8+
}
9+
}

0 commit comments

Comments
 (0)