07.工厂模式-抽象工厂
public interface Car {
String excute();
}
public class BenchiCar implements Car {
public String excute() {
System.out.println("奔驰车生产....");
return "OK";
}
}
public class BwmCar implements Car {
public String excute() {
System.out.println("宝马车生产....");
return "OK";
}
}
public class BydCar implements Car {
public String excute() {
System.out.println("比亚迪车生产....");
return "OK";
}
}
public interface Phone {
void call();
}
public class HuaweiPhone implements Phone{
public void call() {
System.out.println("华为手机....");
}
}
public class XiaomiPhone implements Phone{
public void call() {
System.out.println("小米手机....");
}
}
public interface Factory {
Phone getPhone(String type);
Car getCar(String carType);
}
public class CarFactory implements Factory{
public Phone getPhone(String type) {
return null;
}
public Car getCar(String carType) {
Car car = null;
if ("benchi".equals(carType)) {
car = new BenchiCar();
} else if ("bmw".equals(carType)) {
car = new BwmCar();
} else if ("byd".equals(carType)) {
car = new BydCar();
}
return car;
}
}
public class PhoneFactory implements Factory{
public Phone getPhone(String type) {
Phone phone = null;
if ("Huawei".equals(type)) {
phone = new HuaweiPhone();
} else if ("Xiaomi".equals(type)) {
phone = new XiaomiPhone();
}
return phone;
}
public Car getCar(String carType) {
return null;
}
}
public class AbstractFactory {
public static Factory getFactory(String type){
if("car".equals(type)){
return new CarFactory();
}else if ("phone".equals(type)){
return new PhoneFactory();
}
return null;
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
Car bwm = AbstractFactory.getFactory("car").getCar("bmw");
bwm.excute();
}
}
相关文章
- 从0学ARM-汇编伪指令、LDS详解
- 这么好的微前端解决方案你顶得住?
- 你需要了解的几种微前端解决方案
- 并发编程之Phaser原理与应用
- 【Apps Up最佳应用奖】迷鹿吉他尤克里里 让每个人玩音乐像喝咖啡一样简单
- 如何快速将你的应用封装成JS-SDK?
- 并发编程之Semaphore原理与应用
- 华为应用市场:如何打通开发者通往用户的“最后一公里”?
- 硬核评测 | 小程序如何一键转快应用?
- 架构师之路:微服务技术选型
- 30万款App背后的支持 融云的全球化通信之旅
- 终于有人把前台、中台、后台都讲明白了
- 代码不止|Google 助你打造优质移动端用户体验
- 低代码的兴起,程序猿要拒绝还是拥抱
- FreeRTOS及其应用,万字长文,基础入门
- 广告收入提升超 50%,快手如何在海外“异军突起”
- 实战经验:Oracle DG 的归档缺失修复
- 手把手教你用前端实现短视频App(滑动切换)
- 鸿蒙应用开发之运行HelloWorld
- 使用 Sentry 监控你的Spring Boot应用