经典的23种设计模式是由Erich Gamma、Richard Helm、Ralph Johnson和John Vlissides(合称“Gang of Four”)在他们的书《设计模式:可复用面向对象软件的基础》中定义的。以下是这些设计模式的分类和简要介绍。
这些设计模式在Android开发中也有很多应用,可以帮助开发者设计更好的架构和解决常见问题。尽管有些模式在不同环境中有特定的实现方式,但核心思想依然适用。
在 Android 开发中,虽然23种经典设计模式都有可能用到,但一些模式在实际开发中更为常见和实用。以下是Android开发中常用的设计模式及其应用场景:
应用场景:
示例:
public class NetworkManager {
private static NetworkManager instance;
private NetworkManager() {}
public static synchronized NetworkManager getInstance() {
if (instance == null) {
instance = new NetworkManager();
}
return instance;
}
}
应用场景:
示例:
public abstract class Fragment {
public static Fragment newInstance(String type) {
if (type.equals("home")) {
return new HomeFragment();
} else if (type.equals("settings")) {
return new SettingsFragment();
} else {
return null;
}
}
}
应用场景:
示例:
public class UserViewModel extends ViewModel {
private MutableLiveData user = new MutableLiveData<>();
public LiveData getUser() {
return user;
}
public void setUser(User user) {
this.user.setValue(user);
}
}
应用场景:
示例:
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
initialize();
}
protected abstract int getLayoutId();
protected abstract void initialize();
}
应用场景:
示例:
public class MyAdapter extends RecyclerView.Adapter {
private List data;
public MyAdapter(List data) {
this.data = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(data.get(position));
}
@Override
public int getItemCount() {
return data.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
}
}
}
应用场景:
示例:
public interface ApiService {
@GET("users/{user}")
Call getUser(@Path("user") String user);
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService service = retrofit.create(ApiService.class);
应用场景:
示例:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (onInterceptTouchEvent(ev)) {
return onTouchEvent(ev);
}
return super.dispatchTouchEvent(ev);
}
应用场景:
示例:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_view, parent, false);
}
// Bind data to the view
return convertView;
}
应用场景:
示例:
public class BorderDecorator extends View {
private View view;
public BorderDecorator(View view) {
super(view.getContext());
this.view = view;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Draw border
view.draw(canvas);
}
}
应用场景:
示例:
List list = new ArrayList<>();
for (String item : list) {
// Process item
}
这些设计模式在Android开发中常见且实用,它们可以帮助我们开发者创建更结构化、可维护和扩展性强的应用程序。