0%

设计模式(一)

思想很重要!

浅谈单例模式代理模式工厂模式

单例模式

保证被创建一次,节省系统的开销。

1
2
3
4
5
6
7
8
9
10
11
12
// 省略 get set
public final class GlobalDataMgr {

private static final GlobalDataMgr MGR = new GlobalDataMgr();

private String userName;
private String passWord;

public static GlobalDataMgr getInstance(){
return MGR;
}
}

代理模式

就是相当于第三方,中介,比如在结婚中,我们关注的事只是结婚这一件事,但是结婚还有宴请宾客、组织会场、司仪主持等其他繁琐的事,这些事就可以交给婚介所处理,然后结婚的这个节点实际上婚介所是用我们的真实身份,也就是我们自己去完成的。所以这里的婚介所就相当于代理。(简而言之,使用代理就是为了帮我们做一些额外的事,我们只需要关注自己的事即可)

注:代理 代理的是接口。

静态代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 原始接口(真实角色,即我们)
public interface DemoInterface {
void add();

void del();

void edit();

void query();

default void defaultMethod() {
System.out.println("这是一个默认方法,实现接口时可重写,也可不重写");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 真实角色的实现
public class DemoInterfaceImpl implements DemoInterface {

@Override
public void add() {
System.out.println("增加。。");
}

@Override
public void del() {
System.out.println("删除。。");
}

@Override
public void edit() {
System.out.println("更新。。");
}

@Override
public void query() {
System.out.println("查询。。");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 代理角色,在这里的作用相当于是加了一些 日志或者说是方法执行前的说明
public class DemoInterfaceProxy implements DemoInterface {
private DemoInterfaceImpl demoInterface;

public void setDemoInterface(DemoInterfaceImpl demoInterface) {
if (null == demoInterface){
demoInterface = new DemoInterfaceImpl();
}
this.demoInterface = demoInterface;
}

@Override
public void add() {
log();
demoInterface.add();
}

@Override
public void del() {
demoInterface.del();

}

@Override
public void edit() {
demoInterface.edit();
}

@Override
public void query() {
log();
demoInterface.query();
}

private void log(){
System.out.println("增加日志功能");
}
}
1
2
3
4
5
6
7
8
9
10
//主类
public class Main {

public static void main(String[] args) {
DemoInterfaceImpl demoInterface = new DemoInterfaceImpl();
DemoInterfaceProxy proxy = new DemoInterfaceProxy();
proxy.setDemoInterface(demoInterface);
proxy.add();
}
}

动态代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class ProxyInvocationHandle implements InvocationHandler {
//被代理的接口
private DemoInterface demoInterface;

public void setDemoInterface(DemoInterface demoInterface) {
this.demoInterface = demoInterface;
}

// 生成得到代理类
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(),demoInterface.getClass().getInterfaces(),this);
}

/**
* 处理代理实例,并返回成功
* @param proxy
* @param method
* @param args
* @return
* @throws Throwable
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 动态代理的本质,使用反射机制实现
before();
Object invoke = method.invoke(demoInterface, args);
after();
return invoke;
}

private void before(){
System.out.println("执行前。。");
}

private void after(){
System.out.println("执行后。。");
}

public static void main(String[] args) {
// 真实角色
DemoInterface demoInterface = new DemoInterfaceImpl();
// 代理角色:无
ProxyInvocationHandle proxy = new ProxyInvocationHandle();
// 把真实角色赋给代理
proxy.setDemoInterface(demoInterface);
// 获得代理角色
DemoInterface demoInterfaceProxy = (DemoInterface)proxy.getProxy();
// 通过代理角色调用方法
demoInterfaceProxy.query();
}
}

工厂模式

工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式。 ——百度百科

好处:解耦代码;给代码带来更好的一个可扩展性

简单工厂

1
2
3
4
// 标准规范类(AbstractProduct),用抽象类也行
public interface Shoe {
void make();
}

两个实现

1
2
3
4
5
6
public class SportsShoeImpl implements Shoe {
@Override
public void make() {
System.out.println("生产运动鞋...");
}
}
1
2
3
4
5
6
public class LeisureShoeImpl implements Shoe {
@Override
public void make() {
System.out.println("生成休闲鞋。。");
}
}

构造工厂

1
2
3
4
5
6
7
8
9
10
11
public class ShoeFactory {
public Shoe makeShoe(String type){
if ("sports".equalsIgnoreCase(type)){
return new SportsShoeImpl();
}
if ("leisure".equalsIgnoreCase(type)){
return new LeisureShoe();
}
return null;
}
}

使用

1
2
3
4
5
6
7
8
9
10
11
12
public class Test {
public static void main(String[] args) {
ShoeFactory shoeFactory = new ShoeFactory();
// 休闲鞋
LeisureShoe leisureShoe = (LeisureShoe) shoeFactory.makeShoe("leisure");
//运动鞋
Shoe sportsShoe = shoeFactory.makeShoe("sports");

leisureShoe.make();
sportsShoe.make();
}
}