Java实验项目三——简单工厂模式
Program:
请采用采用简单工厂设计模式,为某个汽车销售店设计汽车销售系统,接口car至少有方法print(),
三个汽车类:宝马、奥迪、大众 (属性:品牌,价格),在测试类中根据客户要求购买的汽车品牌,
通过接口car为客户提供相应的汽车对象。
Description:通过java反射机制和Properties类的结合使用,实现工厂模式。代码如下:
1、首先是entity包中的一个接口和三个实体类
汽车接口:
1 /* 2 *Description:定义汽车接口 3 * */ 4 5 6 package entity; 7 8 public interface Car { 9 10 public void printInfo(); //打印汽车信息 11 }
三个实体类:
1 /* 2 * Description:定义奥迪类,并实现接口 3 * 4 * */ 5 6 package entity; 7 8 public class AoDi implements Car{ 9 10 private String brand = "奥迪"; //品牌 11 private double price = 10000000; //价格 12 13 14 public AoDi(){} 15 16 public AoDi(String brand,double price) { 17 18 this.brand = brand; 19 this.price = price; 20 } 21 22 public String getBrand() { 23 return brand; 24 } 25 26 public void setBrand(String brand) { 27 this.brand = brand; 28 } 29 30 public double getPrice() { 31 return price; 32 } 33 34 public void setPrice(double price) { 35 this.price = price; 36 } 37 38 @Override 39 public String toString() { 40 return "AoDi [brand=" + brand + ", price=" + price + "]"; 41 } 42 43 public void printInfo() { //实现接口方法 44 45 System.out.println( "我是奥迪:" ); 46 System.out.println( this.toString() ); 47 } 48 49 50 }
1 /* 2 * Description:定义宝马类,实现接口 3 * 4 * */ 5 6 package entity; 7 8 public class BaoMa implements Car{ 9 10 private String brand = "宝马"; //品牌 11 private double price = 1000000; //价格 12 13 public BaoMa(){} 14 15 public BaoMa(String brand,double price) { 16 17 this.brand = brand; 18 this.price = price; 19 } 20 21 public String getBrand() { 22 return brand; 23 } 24 25 public void setBrand(String brand) { 26 this.brand = brand; 27 } 28 29 public double getPrice() { 30 return price; 31 } 32 33 public void setPrice(double price) { 34 this.price = price; 35 } 36 37 @Override 38 public String toString() { 39 return "Baoma [brand=" + brand + ", price=" + price + "]"; 40 } 41 42 public void printInfo() { //实现接口方法 43 44 System.out.println( "我是宝马:" ); 45 System.out.println( this.toString() ); 46 } 47 48 49 }
1 /* 2 * Description:定义大众类,并实现接口 3 * 4 * */ 5 6 7 package entity; 8 9 public class DaZhong implements Car{ 10 11 private String brand = "大众"; //品牌 12 private double price = 100000; //价格 13 14 15 public DaZhong(){} 16 17 public DaZhong(String brand,double price) { 18 19 this.brand = brand; 20 this.price = price; 21 } 22 23 public String getBrand() { 24 return brand; 25 } 26 27 public void setBrand(String brand) { 28 this.brand = brand; 29 } 30 31 public double getPrice() { 32 return price; 33 } 34 35 public void setPrice(double price) { 36 this.price = price; 37 } 38 39 @Override 40 public String toString() { 41 return "DaZhong [brand=" + brand + ", price=" + price + "]"; 42 } 43 44 public void printInfo() { //实现接口方法 45 46 System.out.println( "我是大众:" ); 47 System.out.println( this.toString() ); 48 } 49 50 51 }
2、以下是工具包tools中的类
初始化Properties类型文件
1 /* 2 * Description:该类完成属性文件的初始化 3 * 4 * 5 * */ 6 7 package tools; 8 import java.io.File; 9 import java.io.FileInputStream; 10 import java.io.FileOutputStream; 11 import java.util.Properties; 12 13 public class Init { 14 15 public static Properties getProperty() { 16 17 Properties pro = new Properties(); //声明对象 18 19 //实例化File类对象 20 File file = new File( "D:" + File.separator + "init.properties" ); 21 22 try { 23 if( file.exists() ) { //属性文件存在 24 pro.load( new FileInputStream(file) ); //加载文件 25 }else { 26 27 //文件不存在,编辑文件内容 28 pro.setProperty("大众","entity.DaZhong" ); 29 pro.setProperty("宝马", "entity.BaoMa"); 30 pro.setProperty("奥迪", "entity.AoDi"); 31 32 //进行存储 33 pro.store(new FileOutputStream(file), "The information of the car"); 34 } 35 }catch(Exception e) { 36 37 e.printStackTrace(); 38 } 39 40 return pro; 41 } 42 43 }
定义工厂类
1 /* 2 * Description:定义工厂类,通过工厂模式,和反射机制的应用,取得实例化对象,并实例化接口对象,返回接口类型 3 * 4 * 5 * */ 6 7 8 package tools; 9 10 import entity.Car; 11 12 public class Factory { 13 14 public static Car getInstance(String carName) { //此处传递过来的参数是 包.类名 15 16 Car car = null; 17 18 try { 19 car = (Car)Class.forName(carName).newInstance(); //实例化对象 20 21 }catch(Exception e) { 22 23 e.printStackTrace(); 24 } 25 return car; 26 } 27 }
定义操作类,接收用户输入
1 /* 2 * Description:定义Operator类,取得用户的输入值 3 * 4 * */ 5 6 package tools; 7 import java.util.Scanner; 8 9 public class Operate { 10 11 public static String getInput() { 12 13 Scanner scan = new Scanner(System.in); 14 String carName = ""; 15 System.out.println( "用户输入车的名称:" ); 16 carName = scan.next(); 17 return carName; //返回用户的输入值 18 } 19 20 }
main方法,测试整个工程
1 /* 2 * Description:类的接口的运用 3 * 4 * Written By:Cai 5 * 6 * Date Writen:2017-09-25 7 * 8 * 9 * */ 10 /* 11 * Description:java反射机制,属性文件,实现工厂模式 12 * 13 * Written By:Cai 14 * 15 * Date Written:2017-09-24 16 * 17 * 18 * */ 19 20 21 package main; 22 23 import java.util.Properties; //引入属性文件 24 import tools.Factory; //引入自定义的类 25 import tools.Operate; 26 import entity.Car; 27 import tools.Init; 28 29 public class DemoThree4 { 30 31 public static void main(String args[]) { 32 33 Car car = null; 34 String carName = Operate.getInput(); //取得用户的输入内容(此处没有加数据的合法验证) 35 Properties pro = Init.getProperty(); //数理化Properties对象 36 37 car = Factory.getInstance( pro.getProperty(carName) ); //通过属性文件取得实例化对象,并实例化Car对象 38 car.printInfo(); //调用被实例对象覆写的方法 39 40 } 41 }
备注:起步小白,请多指教!
初学小白,请多指教!