1. 策略模式与简单工厂模式的异同
同:两者都是通过多态去减少代码的耦合度。
异:策略模式传递的是特定的产品类对象生产产品;简单工厂模式传递的是特定的信息(e.g.类名)。
package com.test;
public class Client {
public static void main(String[] args) {
// 策略模式
Strategy s = new Strategy();
s.conduct(new ProductA());
s.conduct(new ProductB());
s.conduct(new ProductC());
// 简单工厂模式
SimpleFactory sf = new SimpleFactory();
sf.product(ProductA.class).operation();
sf.product(ProductB.class).operation();
sf.product(ProductC.class).operation();
}
}
abstract class Product{
void operation() {
}
}
class ProductA extends Product{
void operation() {
System.out.println("This is Production A");
}
}
class ProductB extends Product{
void operation() {
System.out.println("This is Production B");
}
}
class ProductC extends Product{
void operation() {
System.out.println("This is Production C");
}
}
// 策略模式
class Strategy{
public void conduct(Product product) {
product.operation();
}
}
// 简单工厂模式
class SimpleFactory{
public <T extends Product>T product(Class<T> clz) {
try {
T product = (T)Class.forName(clz.getName()).newInstance();
return product;
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
return null;
}
}
}
2. Android Adapter与适配器模式的联系
适配器模式可以分为类适配器(静态适配器)和对象适配器(动态适配器):
- 类适配器:通过对类的继承来实现所需要的接口。
UML图:
代码示例:
// 目标:5V
interface Target5V{
public void power5V();
}
// 被适配电压:220V
class Adaptee220V{
public void power220V() {
System.out.println("220V");
}
}
// 适配器
class Adapter extends Adaptee220V implements Target5V{
@Override
public void power5V() {
System.out.println("5V");
}
}
// 测试样例
public class Test{
public static void main(String[] args) {
Adapter adapter = new Adapter();
adapter.power5V();
adapter.power220V();
}
}
- 对象适配器:创建类的对象作为成员来实现所需要的借口。
UML图:
代码示例:
// 目标:5V
interface Target5V{
public void power5V();
}
// 被适配电压:220V
class Adaptee220V{
public void power220V() {
System.out.println("220V");
}
}
// 适配器
class Adapter implements Target5V{
private Adaptee220V adaptee220V;
public Adapter(Adaptee220V adaptee220V) {
this.adaptee220V = adaptee220V;
}
@Override
public void power5V() {
System.out.println("5V");
}
public void power220V() {
adaptee220V.power220V();
}
}
// 测试样例
public class Test{
public static void main(String[] args) {
Adapter adapter = new Adapter(new Adaptee220V());
adapter.power5V();
adapter.power220V();
}
}
至于Android中的Adapter则是将不标准的数据统一成标准的接口,e.g. RecyclerView通过使用适配器将List<String>类型的数据来实现ViewHolder的标准显示。代码示例:
package com.example.lee.recyclerviewtest;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import java.util.zip.Inflater;
/**
* Created by Lee on 2016/9/28.
*/
public class MainListAdapter extends RecyclerView.Adapter<MainListAdapter.MainListViewHolder>{
private List<String> mStringList; // 数据项
private LayoutInflater mLayoutInflater;
public MainListAdapter(Context context, List<String> strings){
mLayoutInflater = LayoutInflater.from(context);
mStringList = strings;
}
@Override
public MainListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mLayoutInflater.inflate(R.layout.item_layout_main_list, parent, false);
return new MainListViewHolder(itemView);
}
@Override
public void onBindViewHolder(MainListViewHolder holder, int position) { // 创建标准的ViewHolder接口
holder.mainTextView.setText(mStringList.get(position));
}
@Override
public int getItemCount() { // 用于获取数据项数目的标准接口
return mStringList.size();
}
public class MainListViewHolder extends RecyclerView.ViewHolder{
public TextView mainTextView;
public MainListViewHolder(View itemView){
super(itemView);
mainTextView = (TextView) itemView.findViewById(R.id.id_main_text_view);
}
}
}
参考博客:
【1】simple-android-framework-exchange
【2】14种常用设计模式