[qicheng骐程]Java面向对象编程考虑和简单工厂模式

19 篇文章 0 订阅
10 篇文章 0 订阅
本文探讨了Java中的面向对象编程思想,并详细介绍了简单工厂模式的原理、优缺点。通过汽车租赁系统的例子,阐述了如何在实际场景中应用简单工厂模式,包括汽车的抽象类设计、子类(轿车、客车)的特性以及租车业务类的实现。同时,提到了简单工厂模式在代码组织和业务解耦方面的作用。
摘要由CSDN通过智能技术生成

[qicheng骐程]Java面向对象编程考虑和简单工厂模式

详见更多到公众号【骐程】

1.简单工厂模式:

简单工厂模式就是一种设计模式,设计模式是一种最佳的实践编码,是软件开发的一种问题解决方案,使用过设计模式在代码的重用性以及对人的理解都大大加强。合理选择设计模式选择有助于解决我们身边的问题,每一种模式都有其对应的原理和它对应的生活中具体的生活场景,

设计模式:(3大类23种<创建型的,结构性的,行为型的>) •创建型模式:不同的创建对象的模式(不止)
•工厂模式(简单工厂模式、工厂方法模式) • 单例模式

简单工厂模式 :
1.简单说就是不在new对象(new Object())来创建对象,根据用户的选择条件来实例化相关的类。

2.去除具体类的依赖性。只需要给出具体的实例描述给工厂,工厂就会自动返回具体的实例化对象(客户端不需要改变,因为没有new的方法来创建对象)。
在这里插入图片描述

3.生成工厂的方法不是普通的方法 ,可以定义为static静态方法,客户端的工厂创建都可以不要,直接通过类名.来调用。

在这里插入图片描述

简单工厂模式设计:工厂类、客户端类 、商品类(接口或抽象类)、具体商品子类、接口

优点:

工厂隐藏了对象创建的细节, 客户端直接使用就可以不需要知道这么创建的。

缺点:

工厂类里面的代码很多,创建各种对象,使用各种业务判断的代码,代码就冗杂,每次创建或者删除对象都要操作工厂类,类就会庞大臃肿,方法之间耦合性强,修改和增加都很麻烦。避免这种缺点可以使用工厂方法模式(解耦合)。

2.汽车租聘系统:

在这里插入图片描述

汽车:父类

轿车、客车:子类

品牌、车牌号:汽车的属性

型号 :轿车特有属性

座位数:客车特有属性

折扣:汽车的功能方法(计算租金)

设计类:

汽车设计为抽象类

设计方法:

计算租金为抽象方法

类图:
在这里插入图片描述

考虑和租车业务来筛选,不相关的就不要考虑

汽车业务类:
•汽车信息初始化 :

数组存储汽车信息(汽车(汽车类):客车、轿车)

数组类型:Vehicle
•租聘汽车:租给用户–》返回值;Vehicle 父类类型作为返回值

      用户提供的信息:客车(座位数、品牌)  、轿车(品牌、型号)

      参数 :品牌、座位数、型号(客车:(品牌、座位数、0)  、轿车:(品牌、0、型号))

       方法体:由用户提供信息遍历汽车数组 ,找到相应车给用户

租聘天数计算时使用,在测试类中使用

回顾简单工厂模式下的汽车租聘系统 :

  1. 按照汽车租聘的功能划分类,如汽车类,轿车、客车子类、汽车管理类,其他的如汽车制造、汽车这么开等等和这里没有关系的1就可以不用考虑;
  2. 设计好子类之后考虑:①汽车父类的,定义共有的属性(品牌、车牌号、日租金),方法(计算租金);②轿车、客车子类;③汽车管理类(初始化汽车库,构建租车方法);④汽车租聘系统(用户使用,调用租车方法)。
    3.具体代码见:

汽车类:

public abstract class Vehicles {
//属性:品牌、车牌、租金
	private String brand;
	private String vehicleID;
	private int vehicleRent;
	
	//定义构造方法,不为创建对象为后面的调用
	public Vehicles(){
		
	}
	public Vehicles(String brand, String vehicleID, int vehicleRent) {
		super();
		this.brand = brand;
		this.vehicleID = vehicleID;
		this.vehicleRent = vehicleRent;
	}


	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getVehicleID() {
		return vehicleID;
	}
	public void setVehicleID(String vehicleID) {
		this.vehicleID = vehicleID;
	}
	public int getVehicleRent() {
		return vehicleRent;
	}
	public void setVehicleRent(int vehicleRent) {
		this.vehicleRent = vehicleRent;
	}
	//抽象租金计算方法
	public abstract float vehiclesRent(int days) ;
}

cars

public class Cars extends Vehicles{
//特有属性:型号
	private String type;
	
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}

	public Cars() {
		
	}

	public Cars(String brand, String vehicleID, int vehicleRent,String type) {
		super(brand, vehicleID, vehicleRent);
		this.type = type;
	}
	//计算租金
	@Override
	public float vehiclesRent(int days) {
		float price = this.getVehicleRent()*days;
		//折扣
		//折扣
			if(days>7&&days<=30) {
				price = price*0.9f;
			}else if(days<=150) {
				price = price*0.8f;
			}else  {
				price = price*0.7f;
			}
		return price;
	}
}

  ***bus***         

```java
public class Bus extends Vehicles{
//特有属性:型号
	private int seatCount;

	
	public int getSeatCount() {
		return seatCount;
	}

	public void setSeatCount(int seatCount) {
		this.seatCount = seatCount;
	}

	public Bus() {
		
	}

	public Bus(String brand, String vehicleID, int vehicleRent,int seatCount) {
		super(brand, vehicleID, vehicleRent);
		this.seatCount = seatCount;
	}
	//计算租金
	@Override
	public float vehiclesRent(int days) {
		float price = this.getVehicleRent()*days;
		//折扣
		//折扣
			if(days>3&&days<=7) {
				price = price*0.9f;
			}else if(days<=30) {
				price = price*0.8f;
			}else if(days<150) {
				price = price*0.7f;
			}else {
				price = price*0.6f;
			}
		return price;
	}

}

package bookingcaragain;

汽车管理类

public class VehicleOperation {
	//保存车的数组
	Vehicles[] vehicles = new Vehicles[8];
	//汽车信息初始化
		public void init() {//轿车:String brand, String cehicleId, int perRent,String type
			//客车:String brand, String cehicleId, int perRent,int seatCount
			vehicles[0] = new Cars("宝马","京N85764",800,"X6");
			vehicles[1] = new Cars("宝马","京L79654",600,"550i");
			vehicles[2] = new Cars("别克","京Y96584",400,"林荫大道");
			vehicles[3] = new Cars("别克","京Y85754",800,"GL8");
			vehicles[4] = new Bus("金龙","京N85764",1000,34);
			vehicles[5] = new Bus("金龙","京U88884",800,16);
			vehicles[6] = new Bus("金杯","京T86694",1200,34);
			vehicles[7] = new Bus("金杯","京P87873",700,16);
		}
		//租车
		//品牌、座位数、型号
		public Vehicles rentVehicle(String brand, String type, int seatCount) {
			Vehicles v = null;
			//遍历汽车信息
			for(Vehicles vehicle:vehicles) {
				if(vehicle instanceof Cars) {
					Cars cars = (Cars)vehicle;
					//品牌和型号
					if(cars.getBrand().equals(brand)&&cars.getType().equals(type)) {
						v = cars;
						break;
					}else if(vehicle instanceof Bus) {
						Bus bus = (Bus)vehicle;
						//品牌和型号
						if(bus.getBrand().equals(brand)&&bus.getSeatCount() == seatCount) {
							v = bus;
							break;
					}
				}
			}
		}
			return v;
	}
}

租聘系统

public class RentVehicle {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		VehicleOperation vo = new VehicleOperation();
		System.out.println("********欢迎来到云卷骐程汽车租聘(手动狗头)*********");
		System.out.println("请输入你要租车的类型:1.轿车,2.客车");
		int choose =  input.nextInt();
		//输入品牌,座位数,型号
				String brand = "";
				int seatCount = 0;
				String type = "";
			switch (choose) {
			case 1:
				System.out.println("请输入你要租车的品牌:1.宝马,2.别克");
				int brandVeh = input.nextInt();
				if(brandVeh == 1) {
					brand = "宝马";
					System.out.println("请输入你要租车的型号:1.X6,2.550i");
					int type1 = input.nextInt();
					switch (type1) {
					case 1:
						type = "X6";
						break;
					case 2:
						type = "550i";
						break;
					}
				}else {
					System.out.println("请输入你要租车的型号:1.林荫大道,2.GL8");
					int type2 = input.nextInt();
				}
				break;
			case 2:
				System.out.println("请输入你要租车的品牌:1.金龙,2.金杯");
				brand = (input.nextInt() == 1)?"金龙":"金杯";
				System.out.println("请输入你要租车的座位数:1.16,2.34");
				seatCount = (input.nextInt() == 1)?16:34;
			}
			vo.init();
			//租车
			Vehicles v = vo.rentVehicle(brand, type, seatCount);
			System.out.println("请输入你要租的天数:");
			int days = input.nextInt();
			float price = v.vehiclesRent(days);
			System.out.println("您好!分配给你的车为:"+v.getVehicleID()+"\n"+"租金为:"+price+"元");
			
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值