单例设计模式
解释:在开发中,当一个类完全不需要有多个实例时,可以使用单例模式来保证一个类只有一个对象。 实际操作为 私有化构造函数
如 票务系统,要保证票数始终同步,卖票的对象可以使用单例模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class SingletonDemo1 {
private SingletonDemo1() { } private static SingletonDemo1 instance = new SingletonDemo1();
public static SingletonDemo1 getInstance(){ return instance; };
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class SingletonDemo2 { private SingletonDemo2() { } private static SingletonDemo2 instance = null;
public static SingletonDemo2 getInstance(){ if (instance == null) { instance = new SingletonDemo2(); } return instance; }
}
|
工厂设计模式
解释:像工厂一样,统一提供产品(实例对象)
工厂设计模式要素: 产品模板 做产品的设备 工厂 进行生产
产品模板
1 2 3 4 5 6
| public interface ProductModel {
ProductModel DOProductModel();
}
|
制造做产品的设备
1 2 3 4 5 6 7
| public class MakeProduct1 implements ProductModel{ @Override public ProductModel DOProductModel() { System.out.println("第一种产品"); return this; } }
|
1 2 3 4 5 6 7 8
| public class MakeProduct2 implements ProductModel{
@Override public ProductModel DOProductModel() { System.out.println("第二种产品"); return this; } }
|
1 2 3 4 5 6 7 8
| public class MakeProduct3 implements ProductModel{
@Override public ProductModel DOProductModel() { System.out.println("第三种产品"); return this; } }
|
将设备放入工厂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class ProductFactory {
public ProductModel MakeProduct(String productname){
if("product1".equals(productname)){ return new MakeProduct1().DOProductModel(); }else if("product2".equals(productname)){ return new MakeProduct2().DOProductModel(); }else if("product3".equals(productname)){ return new MakeProduct3().DOProductModel(); }else{ return null; } } }
|
客户下订单生产
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class MainTest {
public static void main(String[] args) {
ProductFactory productFactory = new ProductFactory();
ProductModel product1 = productFactory.MakeProduct("product1"); ProductModel product2 = productFactory.MakeProduct("product2"); ProductModel product3 = productFactory.MakeProduct("product3"); } }
|
代理模式
- 代理的作用1:除了当前类的功能以外,添加其它的功能
注:解决疑问,为什么不在原来的类中直接添加功能
答:java一直实行单一功能编程,功能越少,改动的几率就越小,如果在本类中进行功能的添加改变,当增加的功能需要更改时,就要在本类中进行查找,而该类添加的方法太多在修改时容易出错
- 代理的作用2:控制其它类对该类的访问,使其它类无法对基类进行更改
注:解决疑问,继承也不能对基类进行更改,为什么要出现代理呢
答:在子类中使用方法时,对象点方法名会暴露基类中有的方法,而如果使用了代理后,可以将基类的方法封装至代理类创建的方法中,在其它类 创建代理类的对象点方法名时就只能看到代理类的方法,对基类的方法就进行了保护
理解即提取基类的一部分方法放在代理类中,新类只能看到代理类的方法
静态代理和动态代理的区别是,静态代理是 有确定的要代理的类
动态代理没有确定的要代理的类,是使用泛化方式写的,相当于将创建代理这种直接交给机器处理(aop就是使用动态代理,原理就在这)
静态代理
举例1
注:例1是网上流传的静态代理,例2是java思想编程上的代理,我偏向于例2,因为例1在测试类依然会创建基类的对象,也就能得到基类的其它方法,并不能满足 对基类访问控制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class Master implements Sell{ @Override public void GiveGoods() { System.out.println("拿出货物"); }
@Override public void GetMoney() { System.out.println("收钱"); }
public void GetGoods() { System.out.println("进货"); } }
|
1 2 3 4 5
| public interface Sell { void GiveGoods(); void GetMoney(); }
|
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
| public class Worker implements Sell{ private Sell worker; public Worker(Sell master) { this.worker = master; } @Override public void GiveGoods() { System.out.println("给产品做讲解"); this.worker.GiveGoods(); }
@Override public void GetMoney() { this.worker.GetMoney(); System.out.println("做账目结算"); }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class Main {
public static void main(String[] args) { Master m = new Master(); Worker W = new Worker(m); W.GiveGoods(); W.GetMoney(); }
}
|
举例2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class SpaceShipControls { void up(int x) { System.out.println("向上移动"+x+ "m"); }; void down(int x) { System.out.println("向下移动"+x+ "m"); }; void left(int x) { System.out.println("向左移动"+x+ "m"); }; void right(int x) { System.out.println("向右移动"+x+ "m"); }; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class SpaceShipDelegation {
private SpaceShipControls control = new SpaceShipControls(); public void up(int x) { control.up(x); }; public void down(int x) { control.down(x); }; public void left(int x) { control.left(x); }; public void right(int x) { control.right(x); };
}
|
1 2 3 4 5 6 7 8 9 10
| public class Main {
public static void main(String[] args) { SpaceShipDelegation protector = new SpaceShipDelegation(); protector.up(100); }
}
|
举例3(完全理解的举例)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class RealClass implements publicInterface{
@Override public void Dosth() { System.out.println("基类做一些事情"); }
@Override public void DosthElse(String arg) { System.out.println("基类做一些其它的事"); }
}
|
1 2 3 4 5
| public interface publicInterface { void Dosth(); void DosthElse(String arg); }
|
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
| public class ProxyClass implements publicInterface{
private publicInterface piface; public ProxyClass(publicInterface Proxyiface) { this.piface = Proxyiface; }
@Override public void Dosth() { System.out.println("代理类做Dosth之前做额外的事"); piface.Dosth(); System.out.println("代理类做Dosth之后做额外的事"); }
@Override public void DosthElse(String arg) { System.out.println("代理类做DosthElse之前做额外的事"); piface.DosthElse(arg); System.out.println("代理类做DosthElse之后做额外的事"); }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class Test {
public static void consumer(publicInterface newiface) { newiface.Dosth(); newiface.DosthElse("传入的参数"); } public static void main(String[] args) { consumer(new RealClass()); consumer(new ProxyClass(new RealClass())); }
}
|
动态代理
举例(对照前面的静态代理)
注:比较于静态代理其实就是一个泛化,静态代理是程序员自己创建代理类,动态代理是自动生成,所以就应该能对所有的类有作用,所以 前面静态代理例子与下面的动态代理例子不同的地方就是处理泛化的问题。实际就是实现动态代理的接口的invoke方法
- 基类(基类和接口都是使用前面静态代理一模一样的,方便对照理解)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class RealClass implements publicInterface{
@Override public void Dosth() { System.out.println("原类做一些事情"); }
@Override public void DosthElse(String arg) { System.out.println("原类做一些其它的事"); }
}
|
1 2 3 4 5
| public interface publicInterface { void Dosth(); void DosthElse(String arg); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class DynamicProxyClass implements InvocationHandler{ private Object dynamicproxy; public DynamicProxyClass(Object proxies) { this.dynamicproxy = proxies; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("这里可以做对基类方法执行前添加其它功能,如获取时间"); Object object = method.invoke(dynamicproxy, args); System.out.println("这里可以做对基类方法执行后添加其它功能,如打印日志"); return object; }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class Test {
public static void consumer(publicInterface newiface) { newiface.Dosth(); newiface.DosthElse("传入的参数"); } public static void main(String[] args) { RealClass real = new RealClass(); consumer(real); publicInterface newiface = (publicInterface) Proxy.newProxyInstance(publicInterface.class.getClassLoader(), new Class[] {publicInterface.class}, new DynamicProxyClass(real)); consumer(newiface); }
}
|