简单工厂模式
遵守的原则
代码实现
//接口
public interface Car {
void name();
}
//实现接口,创建WuLing
public class WuLing implements Car {
@Override
public void name() {
System.out.println("五菱宏光");
}
}
//实现接口,创建Tesla
public class Tesla implements Car {
@Override
public void name() {
System.out.println("特斯拉");
}
}
//工厂
public class CarFactory {
public static Car getCar(String car){
if (!car.isEmpty()){
if (car.equals("五菱")){
return new WuLing();
}else if(car.equals("特斯拉")){
return new Tesla();
}else {
return null;
}
}else {
return null;
}
}
}
//消费者
public class Consumer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入车子名称");
if (scanner.hasNext()) {
String name = scanner.nextLine();
Car car = CarFactory.getCar(name);
car.name();
}
scanner.close();
}
}
抽象工厂模式
定义
提供一个创建一系列相关或者相互依赖对象的接口,无需指定他们具体的类。工厂的工厂;我自己的理解就是在一定意义提前创建了一个相关产品对象的产品集合。拙见而已,不对的地方望指正
代码实现
//手机产品接口
public interface IPhoneProduct {
void start();
void shutdown();
void call();
void sendSMS();
}
//路由产品接口
public interface IRouterProduct {
void start();
void shutdown();
void openWifi();
void setting();//设置参数
}
//通过接口实现华为手机对象的创建
public class HaWeiPhone implements IPhoneProduct {
@Override
public void start() {
System.out.println("华为手机开机了");
}
@Override
public void shutdown() {
System.out.println("华为手机开始关机");
}
@Override
public void call() {
System.out.println("华为手机开始打电话");
}
@Override
public void sendSMS() {
System.out.println("华为手机开始发送短信");
}
}
public class HuaWeiRouter implements IRouterProduct{
@Override
public void start() {
System.out.println("华为路由器开启");
}
@Override
public void shutdown() {
System.out.println("华为路由器开始关闭");
}
@Override
public void openWifi() {
System.out.println("打开华为wifi");
}
@Override
public void setting() {
System.out.println("设置参数");
}
}
//通过接口实现小米手机对象的创建
public class XiaoMiPhone implements IPhoneProduct {
@Override
public void start() {
System.out.println("小米手机开机了");
}
@Override
public void shutdown() {
System.out.println("小米手机开始关机");
}
@Override
public void call() {
System.out.println("小米手机开始打电话");
}
@Override
public void sendSMS() {
System.out.println("小米手机开始发送短信");
}
}
public class XiaoMIRouter implements IRouterProduct{
@Override
public void start() {
System.out.println("小米路由器开启");
}
@Override
public void shutdown() {
System.out.println("小米路由器开始关闭");
}
@Override
public void openWifi() {
System.out.println("打开小米wifi");
}
@Override
public void setting() {
System.out.println("设置参数");
}
}
//抽象工厂
public interface IProductFactory {
//生产手机
IPhoneProduct iPhoneProduct();
//生产路由器
IRouterProduct iRouterProduct();
}
//华为工厂
public class HuaWeiFactory implements IProductFactory{
@Override
public IPhoneProduct iPhoneProduct() {
return new HaWeiPhone();
}
@Override
public IRouterProduct iRouterProduct() {
return new HuaWeiRouter();
}
}
//小米工厂
public class XiaoMiFactory implements IProductFactory{
@Override
public IPhoneProduct iPhoneProduct() {
return new XiaoMiPhone();
}
@Override
public IRouterProduct iRouterProduct() {
return new XiaoMIRouter();
}
}
// 客户端
public class Client {
public static void main(String[] args) {
System.out.println("============小米产品=============");
XiaoMiFactory xiaoMiFactory = new XiaoMiFactory();
IPhoneProduct xiaomi = xiaoMiFactory.iPhoneProduct();
xiaomi.start();
xiaomi.call();
xiaomi.sendSMS();
xiaomi.shutdown();
IRouterProduct xiaomiRouter = xiaoMiFactory.iRouterProduct();
xiaomiRouter.start();
xiaomiRouter.setting();
xiaomiRouter.openWifi();
xiaomiRouter.shutdown();
System.out.println("======华为产品===========");
HuaWeiFactory huaWeiFactory = new HuaWeiFactory();
IPhoneProduct huawei = huaWeiFactory.iPhoneProduct();
huawei.start();
huawei.call();
huawei.sendSMS();
huawei.shutdown();
IRouterProduct huaweiRouter = huaWeiFactory.iRouterProduct();
huaweiRouter.start();
huaweiRouter.setting();
huaweiRouter.openWifi();
huaweiRouter.shutdown();
}
}
小结
违反开闭原则,每次消费不同的车子,就要改变原来的工厂。
抽象工厂在一个稳定的业务结构中很强大,反之也会违反开闭原则。