Skip to content

Commit 34a82de

Browse files
原型模式
1 parent c0f12f8 commit 34a82de

File tree

6 files changed

+138
-420
lines changed

6 files changed

+138
-420
lines changed

.idea/workspace.xml

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

src/main/java/prototype/Client.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package prototype;
2+
3+
public class Client {
4+
5+
private Prototype prototype;
6+
public Client(Prototype prototype){
7+
this.prototype=prototype;
8+
}
9+
10+
public void operation(Prototype example){
11+
Prototype copyPrototype=(Prototype) example.clone();
12+
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package prototype;
2+
3+
/**
4+
* 具体原型角色
5+
*/
6+
public class ConcretePrototype1 implements Prototype{
7+
/**
8+
* 最简单的克隆,新建一个自身对象,由于没有属性就不再复制值
9+
* @return
10+
*/
11+
@Override
12+
public Prototype clone() {
13+
Prototype prototype=new ConcretePrototype1();
14+
return prototype;
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package prototype;
2+
3+
public class ConcretePrototype2 implements Prototype {
4+
5+
@Override
6+
public Prototype clone() {
7+
Prototype prototype=new ConcretePrototype2();
8+
return prototype;
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package prototype;
2+
3+
/**
4+
* 抽象原型角色
5+
*/
6+
public interface Prototype {
7+
/**
8+
* 克隆自身的方法
9+
* @return
10+
*/
11+
public Object clone();
12+
}

src/main/java/strategy/Client.java

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

33
/**
4-
* 策略模式。针对不同的客户,采用不同的算法
4+
* 策略模式。针对不同等级的客户,采用不同的算法
5+
* 策略模式,可以避免大量的if和else.
56
* 参考博客:http://www.cnblogs.com/java-my-life/archive/2012/05/10/2491891.html
67
*/
78
public class Client {

0 commit comments

Comments
 (0)