Thursday, March 5, 2009

Sample of Java Pattern -- Adapter

LegacyModel.java
package adapter;

public class LegacyModel {
public void ins(String sku){
System.out.println("call ins method.");
}
}


ModernModel.java
package adapter;

public interface ModernModel {
public void save(Order order);
}


ModernModelAdapter.java
package adapter;

public class ModernModelAdapter implements ModernModel{
private LegacyModel legacyModel;

public ModernModelAdapter(LegacyModel legacyModel) {
super();
this.legacyModel = legacyModel;
}

public void save(Order order){
String sku = order.getSku();
legacyModel = new LegacyModel();
legacyModel.ins(sku);
}
}


Order.java
package adapter;

public class Order {
private String sku;

public String getSku() {
return sku;
}

public void setSku(String sku) {
this.sku = sku;
}

}


AdapterTest.java
package adapter;

public class AdapterTest {
public static void main(String[] args) {
LegacyModel legacy = new LegacyModel();
Order order = new Order();
order.setSku("sku");
ModernModelAdapter adapter = new ModernModelAdapter(legacy);
adapter.save(order);
}
}


you may download the source code.

No comments:

Post a Comment