一.适配器模式(Adapter)
适配器模式属于结构型设计模式,用于转换接口。适配器模式可以使不兼容的代码间相互协作。
适配器模式用于将某个类的接口转换为客户端所期望的另一个接口表示,目的是为了消除由于接口不匹配所造成的类的兼容性问题。主要分为三类:类的适配器模式,对象的适配器模式,接口的适配器模式。
角色及职责:
目标接口Target:我们相用适配器转换过去的新的目标接口。
被适配者Adaptee:适配器所应用的对象。
适配器Adapter:转换接口的类。
适配器应用的场景:
(1)系统中遗留的代码与新代码之间的兼容性问题,用于将旧的接口转换为新的接口,以便可以相互协调。
(2)多个系统或模块间的兼容性处理,用于将各个模块间不兼容的接口转换为通用的接口,以便相互工作。
1.类的适配器模式
核心思想:有一个Source类,有一个方法,待适配,有一个Targetable接口,为了使得类和接口间能相互工作,引入Adapter类,用于继承Source类实现Targetable接口。
public class Source {
public void method1() {
System.out.println("this is original method!");
}
}
public interface Targetable {
/* 与原类中的方法相同 */
public void method1();
/* 新的方法 */
public void method2();
}
//类Source和接口Targetable因为不兼容,导致不能在一起工作
//适配器Adapter则可以在不改变源代码的基础上解决这个问题
//这样Targetable接口的实现类Adapter的对象即使Targetable类型,也能访问到Source中的方法
public class Adapter extends Source implements Targetable {
public void method2() {
System.out.println("this is the targetable method!");
}
}
//测试类 这样Targetable接口的实现类就具有了Source类的功能。
public class AdapterTest {
public static void main(String[] args) {
Targetable target = new Adapter();
target.method1();
target.method2();
}
}
2.对象的适配器模式
与类的适配器模式基本思路相似,只是将Adapter类进行修改,不再继承Source类而是实现Source类的实例。
public class Source {
public void method1() {
System.out.println("this is original method!");
}
}
public interface Targetable {
/* 与原类中的方法相同 */
public void method1();
/* 新的方法 */
public void method2();
}
//只需要修改Adapter类的源码即可:
public class Adapter implements Targetable {
private Source source;
public Adapter(Source source){
this.source = source;
}
public void method2() {
System.out.println("this is the targetable method!");
}
public void method1() {
source.method1();
}
}
//测试类 输出与第一种情况一样,只是使用的适配方法不同而已。
public class AdapterTest {
public static void main(String[] args) {
Source source = new Source();
Targetable target = new Adapter(source);
target.method1();
target.method2();
}
}
3.接口的适配器模式
有时并不想实现一个接口中的所有方法,此时就可使用接口的适配器模式。
三种适配器模式的总结:
类的适配器模式:
当希望把一个类转化成满足另一个新接口的类时,可以使用类的适配器模式创建一个新类,继承原有类,实现接口。
对象的适配器模式:
当希望把一个对象转换成满足另一个新接口的对象时,可以使用对象的适配器模式,创建一个新类,持有原有类的实例对象,在新类的方法中调用该对象即可。
接口的适配器模式:
当不希望实现一个接口中的所有方法时,可以创建一个抽象类实现所有方法,在新类中继承该抽象类即可。
二.单例模式
只适合单线程:
public class Singleton{
private static Singleton instance=null;
private Singleton(){
}
public static Singleton getInstance(){
if(instance==null){
instance=new Singleton():
}
return instance;
}
}
多线程情况可用:
public class Singleton{
private static Singleton instance=null;
private Singleton(){
}
public static synchorized Singleton getInstance(){
if(instance==null){
instance=new Singleton();
}
return instance;
}
}
加同步锁前后两次判断实例是否存在:
public class Singleton{
private static Singleton instance=null;
if(instance==null){
synchorized(Singleton.class){
if(instance==null){
instance=new Singleton();
}
}
}
return instance;
}
}
利用静态构造函数:
public class Singleton{
private static Singleton instance=new Singleton();
private Singleton(){
}
public static Singleton getInstance(){
return instance;
}
}
实现按需创建实例:
public class Singleton{
private Singleton(){
}
public static class SingletonHolder{
private final static Singleton instance=new Singleton();
}
public static Singleton getInstance(){
return SingletonHolder.instance;
}
}
推荐最后两种。
三.装饰者模式
装饰者模式属于结构型设计模式,通过对象组合的方式给对象动态增加某些行为。装饰者模式要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例。这里的动态指的是用户可以根据自己的需求把之前定好的功能任意组合。JDK中的IO流部分就是使用了装饰者模式。
角色及职责:
Component:装饰者模式应用的目标组件的抽象。
Concrete Component:Component的具体实现。
Decorator:装饰器的抽象。
Concrete Decorator:装饰器的实现类。
适用场景:
想通过对象组合的方式动态的给对象增加行为。