设计思想:

  高度抽象---接口

  细节抽象--抽象类

  具体实现--子类

  工厂--通过传入要生产的子类字节码文件,通过method.invoke方法调用子类中的单例对外的方法,并实现解耦。

请大家参考!代码

 

package factory.base;

public interface CarInterface {

    /**
     * 汽车说明书
     * 
     * @return
     */
    String carContent();

    
    
    /**
     * 汽车总价
     * 
     * @return
     */
    double carTotalPrice();

}

 

package factory.base;

public abstract class CarAbstract implements CarInterface {

    public static Object lock = new Object();

    public String carName;

    public String carYear;

    public String carType;

    public String carColor;

    public String carHasSkyWindow;

    @Override
    public String carContent() {
        StringBuilder sb = new StringBuilder();
        sb.append("carName=" + carName);
        sb.append("carYear=" + carYear);
        sb.append("carType=" + carType);
        sb.append("carColor=" + carColor);
        sb.append("carHasSkyWindow=" + carHasSkyWindow);
        return sb.toString();
    }

    /**
     * 汽车总价格
     */
    public abstract double carTotalPrice();

}

 

package factory.sylphy;

import factory.base.CarAbstract;

public class SylphyCar extends CarAbstract {

    private double luochePrice = 13000;
    private double colorPrice = 2000;
    private static SylphyCar car;

    private SylphyCar() {
        super.carName = "轩逸";
        super.carYear = "2019";
        super.carType = "自动档";
        super.carColor = "白色";
        super.carHasSkyWindow = "无";
    }

    public static SylphyCar InstanceSingleCar() {
        synchronized (lock) {
            if (car == null) {
                car = new SylphyCar();
            }
            return car;
        }
    }

    @Override
    public double carTotalPrice() {

        return luochePrice + colorPrice;
    }

}

 

package factory.carfactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class CarFactory<CarInterface> {

    public CarInterface getCar(Class<CarInterface> clazz){
        
        Method method;
        CarInterface invoke = null;
        try {
            method = clazz.getDeclaredMethod("InstanceSingleCar");
            invoke = (CarInterface)method.invoke(null);
        } catch (NoSuchMethodException | SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        
        return invoke;
    }
}

 

package factory;

import factory.carfactory.CarFactory;
import factory.sylphy.SylphyCar;

/**
 * @author Administrator
 *
 */
public class MainTest {
    public static void main(String[] as) throws NoSuchFieldException, SecurityException {
        
        CarFactory<SylphyCar> factory = new CarFactory<SylphyCar>();
        SylphyCar car = factory.getCar(SylphyCar.class);
        System.out.println(car.carContent());
        System.out.println(car.carTotalPrice());
        
    }
}

 

输出:

carName=轩逸carYear=2019carType=自动档carColor=白色carHasSkyWindow=15000.0