Skip to content

Commit 1f9778d

Browse files
author
UMS-AUG
committed
设计模式测试类
1 parent af201b1 commit 1f9778d

28 files changed

+829
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.ada.software.design.adapter.login;
2+
3+
/**
4+
*
5+
* <b><code></code></b>
6+
* <p/>
7+
*
8+
* <p/>
9+
*
10+
* @Date: 2022/10/23 16:59
11+
* @Author xwn
12+
* @Version 1.0.0.1
13+
*
14+
*/
15+
public class PassportTest {
16+
public static void main(String[] args) {
17+
IPassportForThird passportForThird = new PassportForThirdAdapter();
18+
passportForThird.loginForQQ("");
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.ada.software.design.adapter.login;
2+
3+
/**
4+
*
5+
* <b><code></code></b>
6+
* <p/>
7+
*
8+
* <p/>
9+
*
10+
* @Date: 2022/10/23 16:51
11+
* @Author xwn
12+
* @Version 1.0.0.1
13+
*
14+
*/
15+
public class SigninForThirdServiceTest {
16+
public static void main(String[] args) {
17+
SigninForThirdService service = new SigninForThirdService();
18+
//不改变原来的代码,也要能够兼容新的需求
19+
//还可以再加一层策略模式
20+
service.loginForQQ("sdfgdgfwresdf9123sdf");
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.ada.software.design.adapter.power;
2+
3+
/**
4+
*
5+
* <b><code></code></b>
6+
* <p/>
7+
*
8+
* <p/>
9+
*
10+
* @Date: 2022/10/23 16:40
11+
* @Author xwn
12+
* @Version 1.0.0.1
13+
*
14+
*/
15+
public class AdapterTest {
16+
public static void main(String[] args) {
17+
DirectCurrent directCurrent = new PowerAdapter(new AlternatCurrent());
18+
directCurrent.outputDc5V();
19+
}
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.ada.software.design.chain;
2+
3+
import com.ada.software.design.chain.upgrade.HaveBreakfastFilter;
4+
import com.ada.software.design.chain.upgrade.StudyPrepareFilter;
5+
import com.ada.software.design.chain.upgrade.WashFaceFilter;
6+
import com.ada.software.design.chain.upgrade.WashHairFilter;
7+
8+
/**
9+
*
10+
* <b><code></code></b>
11+
* <p/>
12+
*
13+
* <p/>
14+
*
15+
* @Date: 2023/6/28 19:58
16+
* @Author xwn
17+
* @Version 1.0.0.1
18+
*
19+
*/
20+
public class ChainTest {
21+
public static void main(String[] args) {
22+
PreparationList preparationList = new PreparationList();
23+
preparationList.setWashFace(true);
24+
preparationList.setWashHair(false);
25+
preparationList.setHaveBreakfast(true);
26+
27+
Study study = new Study();
28+
29+
StudyPrepareFilter washFaceFilter = new WashFaceFilter();
30+
StudyPrepareFilter washHairFilter = new WashHairFilter();
31+
StudyPrepareFilter haveBreakfastFilter = new HaveBreakfastFilter();
32+
33+
FilterChain filterChain = new FilterChain(study);
34+
filterChain.addFilter(washFaceFilter);
35+
filterChain.addFilter(washHairFilter);
36+
filterChain.addFilter(haveBreakfastFilter);
37+
38+
filterChain.doFilter(preparationList, filterChain);
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.ada.software.design.decorator.batter;
2+
3+
/**
4+
*
5+
* <b><code></code></b>
6+
* <p/>
7+
*
8+
* <p/>
9+
*
10+
* @Date: 2022/10/26 20:05
11+
* @Author xwn
12+
* @Version 1.0.0.1
13+
*
14+
*/
15+
public class BattercakeTest {
16+
public static void main(String[] args) {
17+
Battercake battercake;
18+
//路边摊买一个煎饼
19+
battercake = new BaseBattercake();
20+
//煎饼有点小,想再加一个鸡蛋
21+
battercake = new EggDecorator(battercake);
22+
//再加一个鸡蛋
23+
battercake = new EggDecorator(battercake);
24+
//很饿,再加根香肠
25+
26+
battercake = new SausageDecorator(battercake);
27+
//跟静态代理最大区别就是职责不同
28+
//静态代理不一定要满足 is-a 的关系
29+
//静态代理会做功能增强,同一个职责变得不一样
30+
//装饰器更多考虑是扩展
31+
System.out.println(battercake.getMsg() + ",总价:" + battercake.getPrice());
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.ada.software.design.decorator.simple;
2+
3+
/**
4+
*
5+
* <b><code></code></b>
6+
* <p/>
7+
*
8+
* <p/>
9+
*
10+
* @Date: 2022/10/26 19:53
11+
* @Author xwn
12+
* @Version 1.0.0.1
13+
*
14+
*/
15+
public class BattercakeTest {
16+
public static void main(String[] args) {
17+
Battercake battercake = new Battercake();
18+
System.out.println(battercake.getMsg() + ",总价格:" + battercake.getPrice());
19+
Battercake battercakeWithEgg = new BattercakeWithEgg();
20+
System.out.println(battercakeWithEgg.getMsg() + ",总价格:" + battercakeWithEgg.getPrice());
21+
Battercake battercakeWithEggAndSausage = new BattercakeWithEggAndSausage();
22+
System.out.println(battercakeWithEggAndSausage.getMsg() + ",总价格:" +
23+
battercakeWithEggAndSausage.getPrice());
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.ada.software.design.delegate;
2+
3+
/**
4+
*
5+
* <b><code></code></b>
6+
* <p/>
7+
*
8+
* <p/>
9+
*
10+
* @Date: 2022/10/8 22:26
11+
* @Author xwn
12+
* @Version 1.0.0.1
13+
*
14+
*/
15+
public class DelegateTest {
16+
public static void main(String[] args) {
17+
// 客户请求(Boss)、委派者(Leader)、被被委派者(Target)
18+
//委派者要持有被委派者的引用
19+
//代理模式注重的是过程, 委派模式注重的是结果
20+
//策略模式注重是可扩展(外部扩展),委派模式注重内部的灵活和复用
21+
//委派的核心:就是分发、调度、派遣
22+
//委派模式:就是静态代理和策略模式一种特殊的组合
23+
new Boss().command("加密", new Leader());
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.ada.software.design.observer.event;
2+
3+
/**
4+
*
5+
* <b><code></code></b>
6+
* <p/>
7+
*
8+
* <p/>
9+
*
10+
* @Date: 2022/10/26 23:36
11+
* @Author xwn
12+
* @Version 1.0.0.1
13+
*
14+
*/
15+
public class MouseEventTest {
16+
public static void main(String[] args) {
17+
try {
18+
MouseEventCallback callback = new MouseEventCallback();
19+
//注册事件
20+
Mouse mouse = new Mouse();
21+
mouse.addLisenter(MouseEventType.ON_CLICK, callback);
22+
mouse.addLisenter(MouseEventType.ON_MOVE, callback);
23+
mouse.addLisenter(MouseEventType.ON_WHEEL, callback);
24+
mouse.addLisenter(MouseEventType.ON_OVER, callback);
25+
//调用方法
26+
mouse.click();
27+
//失焦事件
28+
mouse.blur();
29+
} catch (Exception e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.ada.software.design.observer.simple;
2+
3+
/**
4+
*
5+
* <b><code></code></b>
6+
* <p/>
7+
*
8+
* <p/>
9+
*
10+
* @Date: 2022/10/26 23:27
11+
* @Author xwn
12+
* @Version 1.0.0.1
13+
*
14+
*/
15+
public class ObserverTest {
16+
public static void main(String[] args) {
17+
Forum forum = Forum.getInstance();
18+
Teacher tom = new Teacher("Tom");
19+
Teacher mic = new Teacher("Mic");
20+
forum.addObserver(tom);
21+
forum.addObserver(mic);
22+
//业务逻辑代码
23+
Question question = new Question();
24+
question.setUserName("小明");
25+
question.setContent("观察者模式适用于哪些场景?");
26+
forum.publishQuestion(question);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.ada.software.design.prototype.deep;
2+
3+
/**
4+
*
5+
* <b><code></code></b>
6+
* <p/>
7+
* 深拷贝测试类
8+
* <p/>
9+
*
10+
* @Date: 2022/9/18 16:21
11+
* @Author xwn
12+
* @Version 1.0.0.1
13+
*
14+
*/
15+
public class DeepCloneTest {
16+
public static void main(String[] args) {
17+
MonkeyKing monkeyKing = new MonkeyKing();
18+
try {
19+
MonkeyKing clone = (MonkeyKing)monkeyKing.clone();
20+
System.out.println("深克隆:" + (monkeyKing.goldHoop == clone.goldHoop));
21+
} catch (Exception e) {
22+
e.printStackTrace();
23+
}
24+
25+
MonkeyKing q = new MonkeyKing();
26+
MonkeyKing n = q.shallowClone(q);
27+
System.out.println("浅克隆:" + (q.goldHoop == n.goldHoop));
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.ada.software.design.prototype.simple;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
*
8+
* <b><code></code></b>
9+
* <p/>
10+
* 浅克隆测试用例
11+
* <p/>
12+
*
13+
* @Date: 2022/9/18 15:54
14+
* @Author xwn
15+
* @Version 1.0.0.1
16+
*
17+
*/
18+
public class PrototypeTest {
19+
public static void main(String[] args) {
20+
21+
// 原先的对象
22+
ConcretePrototypeA concretePrototype = new ConcretePrototypeA();
23+
// 填充属性,方便测试
24+
concretePrototype.setAge(18);
25+
concretePrototype.setName("prototype");
26+
List hobbies = new ArrayList<String>();
27+
concretePrototype.setHobbies(hobbies);
28+
System.out.println("原先的对象:"+concretePrototype);
29+
30+
31+
Client client = new Client(concretePrototype);
32+
ConcretePrototypeA concretePrototypeClone = (ConcretePrototypeA)client.startClone(concretePrototype);
33+
34+
System.out.println("克隆后的对象:"+concretePrototypeClone);
35+
36+
System.out.println("克隆对象中的引用类型地址值:" + concretePrototypeClone.getHobbies());
37+
System.out.println("原对象中的引用类型地址值:" + concretePrototype.getHobbies());
38+
System.out.println("对象地址比较:"+(concretePrototypeClone.getHobbies() == concretePrototype.getHobbies()));
39+
}
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.ada.software.design.proxy.cglib;
2+
3+
import org.springframework.cglib.core.DebuggingClassWriter;
4+
5+
/**
6+
*
7+
* <b><code></code></b>
8+
* <p/>
9+
*
10+
* <p/>
11+
* <b>Creation Time:</b> 2022/9/28 10:25
12+
* @author xiewn
13+
* @version 1.0.0.1
14+
*
15+
* @since 1.0.0.1
16+
*/
17+
public class CglibTest {
18+
public static void main(String[] args) throws Exception {
19+
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY,"E://cglib_proxy_classes");
20+
21+
Customer obj = (Customer) new GglibMatchmaker().getInstance(Customer.class);
22+
System.out.println(obj);
23+
obj.findLove();
24+
}
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.ada.software.design.proxy.db;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
6+
import com.ada.software.design.proxy.db.dynamic.OrderServiceDynamicProxy;
7+
8+
/**
9+
*
10+
* <b><code></code></b>
11+
* <p/>
12+
*
13+
* <p/>
14+
* <b>Creation Time:</b> 2022/9/27 14:31
15+
* @author xiewn
16+
* @version 1.0.0.1
17+
*
18+
* @since 1.0.0.1
19+
*/
20+
public class DbRouteProxyTest {
21+
public static void main(String[] args) throws Exception {
22+
Order order = new Order();
23+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
24+
Date date = sdf.parse("2017/02/01");
25+
order.setCreateTime(date.getTime());
26+
//静态代理类
27+
//IOrderService orderService = (IOrderService) new OrderServiceStaticProxy(new OrderServiceImpl());
28+
//orderService.createOrder(order);
29+
30+
//动态代理
31+
IOrderService orderService = (IOrderService) new OrderServiceDynamicProxy().getInstance(new OrderServiceImpl());
32+
orderService.createOrder(order);
33+
}
34+
}

0 commit comments

Comments
 (0)