简单工厂模式
1.来个实物类接口
public interface Phone {
void call();
}
2.来个华为手机
public class HuaWeiPhone implements Phone {
private static final String TAG = "HuaWeiPhone";
@Override
public void call() {
Log.e(TAG, "call: " );
}
}
3.来个小米手机
public class XiaoMiPhone implements Phone {
private static final String TAG = "XiaoMiPhone";
@Override
public void call() {
Log.e(TAG, "call: ");
}
}
4.来个手机代工厂,根据自己想要的type类型去生产不同的手机.
public class Factory {
public Phone creatPhone(String type){
if (type.equals("huawei")){
return new HuaWeiPhone();
}
if (type.equals("xiaomi")){
return new XiaoMiPhone();
}
return null;
}
}