思想很重要!
浅谈单例模式、代理模式、工厂模式。
单例模式
保证被创建一次,节省系统的开销。
1 2 3 4 5 6 7 8 9 10 11 12
| 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); }
@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
| 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(); } }
|