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 }
備注:起步小白,請多指教!