简单工厂模式(Simple Factory Pattern),又称静态工厂方法,它属于类创建模式。在简单工厂模式中,可以根据参数的不同返回不同的类的实例。简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的通常具有共同的父类。
案例如:
设计步骤:
项目结构:
思路:首先编写父类,写出共同属性和方法,再写基类,其中Opration类中管理实体类,创建实体类对象,最后根据输入的车型(brand),品牌系列(type)等确定车辆,再根据租赁天数,使用多态的方式自动选择调用轿车或者客车的对应方法算出租金。
假如有如下车型出租:
输出示例:
代码附录:
model.Rent_Car
package model;
/*
* 父类,抽象类
*/
public abstract class Rent_Car {
private int perRent; //日租租金
private int days; //租赁天数
private String brand; //品牌名字
private String number; //车牌号
public abstract double rents(int days); //计算租金
Rent_Car(){} //默认构造函数
Rent_Car(String brand,String number,int rent){
this.brand=brand;
this.number=number;
this.perRent=rent;
}
public int getperRent() {
return perRent;
}
public void setperRent(int rent) {
this.perRent = rent;
}
public int getDays() {
return days;
}
public void setDays(int days) {
this.days = days;
}
public String getName() {
return brand;
}
public void setName(String name) {
this.brand = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
model.Car
package model;
/*
轿车类
*/
public class Car extends Rent_Car{
private String type; //品牌种类
public Car(){} //构造函数
public Car(String brand,String type,String number,int perRent){
super(brand,number,perRent);
this.type=type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double rents(int days) {
double money=0;
int rent=this.getperRent();
if(days<0) {
System.out.println("请输入有效天数!");
}
if(days<7) {
money=rent*days;
}
if(days>=7&days<30) {
money=rent*0.9*days;
}
if(days>=30&days<150) {
money=rent*0.8*days;
}
if(days>=150) {
money=rent*0.7*days;
}
return money;
}
}
model.Bus
package model;
/*
* 货车类
*/
public class Bus extends Rent_Car{
public int sits; //座位数
public int getSits() {
return sits;
}
public void setSits(int sits) {
this.sits = sits;
}
public Bus(){
}
public Bus(String brand,int sits,String number,int perRent){
super(brand,number,perRent);
this.sits=sits;
}
public double rents(int days) {
double money=0;
int rent =this.getperRent();
if(days<0) {
System.out.println("请输入有效天数!");
}
if(days<3) {
money=rent*days;
}
if(days>=3&days<7) {
money=rent*0.9*days;
}
if(days>=7&days<30) {
money=rent*0.8*days;
}
if(days>=30&days<150) {
money=rent*0.7*days;
}
if(days>=150) {
money=rent*0.6*days;
}
return money;
}
}
service.Opration
package service;
import model.*;
/*
* 操作实体类
*/
public class Opration {
private Rent_Car [] moto=new Rent_Car[8];
public void init() {
moto[0]=new Car("宝马","X6","云BF28856",800);
moto[1]=new Car("宝马","X5","云BF28857",700);
moto[2]=new Car("别克","林荫大道","川BF28856",800);
moto[3]=new Car("别克","TF","川BF28857",600);
moto[4]=new Bus("金杯",16,"云BF123456",800);
moto[5]=new Bus("金杯",32,"云BF200456",1200);
moto[6]=new Bus("亚星",16,"云BF200457",900);
moto[7]=new Bus("亚星",32,"云BF200458",1200);
}
/*
* 根据品牌,种类和座位数返回哪种车,和数组中对比
*/
public Rent_Car chooseMoto(String brand,String type ,int sits) {
Rent_Car motochoose=null;
for(Rent_Car carType: moto) {
if(carType instanceof Car) {
Car car=(Car)carType;
if(car.getName().equals(brand) &&car.getType().equals(type) ) {
motochoose=car;
break;
}
}else if(carType instanceof Bus) {
Bus bus=(Bus)carType;
if(bus.getName().equals(brand) &&bus.getSits()==sits ) {
motochoose=bus;
break;
}
}
}
return motochoose;
}
}
service.Manager
package service;
import java.util.*;
import model.Bus;
import model.Car;
import model.Rent_Car;
/*
* 租车系统
*/
public class Manager {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sca= new Scanner(System.in);
int x=0;
System.out.println("----------------欢迎光临汽车租赁公司----------------");
System.out.println("1:轿车 2:客车");
Opration opa=new Opration();
opa.init();
System.out.println("请选择你要租赁的汽车类型:");
x=sca.nextInt();
String brand="";
String type="";
int sits=0;
int days=0;
if(x==1) {
System.out.println("请选择你要租赁的汽车品牌:1、别克 2、宝马");
brand=( sca.nextInt()==1?"别克":"宝马");
if(brand=="别克"){
System.out.println("请选择你要租赁的汽车种类:1、林荫大道 2、TF");
type=( sca.nextInt()==1?"林荫大道":"TF");
}
else if(brand=="宝马") {
System.out.println("请选择你要租赁的汽车种类:1、X5 2、X6");
type=( sca.nextInt()==1?"X5":"X6");
}
}
else if(x==2) {
System.out.println("请选择你要租赁的汽车品牌:1、金杯 2、亚星");
brand=( sca.nextInt()==1?"金杯":"亚星");
System.out.println("请选择你要租赁的座位数字:1、16座 2、32座");
sits=( sca.nextInt()==1?16:32);
}
System.out.println("请选择你要租赁时间/天:");
days=sca.nextInt();
Rent_Car moto= opa.chooseMoto(brand, type, sits);
double money=moto.rents(days); // 计算租金,实现多态
System.out.println("分配的车牌号是:"+moto.getNumber());
System.out.println("您需要支付的租金卫:"+money+"元");
sca.close();
}
}
增加出租卡车,此时只需要写出卡车类继承Rent_Car类,并再Opration类中增加卡车对象。