Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7a06e7a

Browse files
author
deeper
committed
add sampleFactory
1 parent 0599f11 commit 7a06e7a

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.ruoxu.pattern.simple_factory;
2+
3+
/**
4+
* 简单工程模式(静态工厂模式)不属于23种设计模式
5+
* @author Administrator
6+
*/
7+
public class Demo {
8+
9+
public static void main(String[] args) {
10+
Operation operation = OperationFactory.create("-");
11+
System.out.println(operation.calculate(3,4));
12+
}
13+
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.ruoxu.pattern.simple_factory;
2+
3+
public interface Operation {
4+
5+
int calculate(int numberA, int numberB);
6+
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.ruoxu.pattern.simple_factory;
2+
3+
public class OperationAdd implements Operation{
4+
5+
@Override
6+
public int calculate(int numberA,int numberB) {
7+
int result = numberA + numberB;
8+
return result;
9+
}
10+
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.ruoxu.pattern.simple_factory;
2+
3+
public class OperationFactory {
4+
public static Operation create(String oper){
5+
Operation operation = null;
6+
switch (oper) {
7+
case "+":
8+
operation = new OperationAdd();
9+
break;
10+
case "-":
11+
operation = new OperationSub();
12+
break;
13+
default:
14+
break;
15+
}
16+
return operation;
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.ruoxu.pattern.simple_factory;
2+
3+
public class OperationSub implements Operation{
4+
5+
@Override
6+
public int calculate(int numberA, int numberB) {
7+
return numberA - numberB;
8+
}
9+
10+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /