簡單工廠模式

1.簡單工廠模式(這里只是簡單舉例 代碼不規(guī)范處還需諒解)

原則: 提供創(chuàng)建對象的功能,不需要關(guān)心具體的實現(xiàn)

//創(chuàng)建一個汽車的抽象類
public abstract class Car {

    protected String name;

    protected final static String CARSTYLE = "汽車";

    public abstract String drive();

    public String getName() {
        return this.name;
    }
}
//子類實現(xiàn)父類
public class BackCar extends Car {
    @Override
    public String drive() {
        return name+Car.CARSTYLE;
    }
}
//創(chuàng)建一個工廠類負(fù)責(zé)具體的對象的創(chuàng)建
public class CarFactory {
    public static Car newCar(String color){
        Car car=null;
        switch (color){
            case "黑色":
                car=new BackCar();
                break;
            case "紅色":
                car=new RedCar();
                break;
            case "藍色":
                car=new BuleCar();
                break;
        }
        return car;
    }
}
//在客戶的調(diào)用(根據(jù)傳入的參數(shù)類型不同 而構(gòu)建不同的對象)
 Car car = CarFactory.newCar(RED);
 tv_text.setText(car.drive());

由上可以看出 對對象的具體實現(xiàn)實現(xiàn)的隱藏

弊端:
1.不符合開閉原則 如果有新的對象生成 需要改寫工廠類
2.不符合單一原則 工廠類是根據(jù)類型來判斷創(chuàng)建哪種汽車的

二. 升級簡單工廠方法

//創(chuàng)建一個注解類
@Target(ElementType.TYPE)//用于描述類、接口(包括注解類型) 或enum聲明
@Retention(RetentionPolicy.RUNTIME)//即運行時保留
@Documented //用于描述其它類型的annotation應(yīng)該被作為被標(biāo)注的程序成員的公共API
@Inherited //修飾的annotation類型被用于一個class 則這個Annotation將被用于該class的子類。
public @interface CarColor {
    String color() default "";
}
//使用注解
@CarColor(color="BACK")
public class BackCar extends Car {
    @Override
    public String drive() {
        return name+Car.CARSTYLE;
    }
}
//創(chuàng)建工廠方法 根據(jù)對象類型來構(gòu)建對應(yīng)的對象
public class CarFactory2 {

    public static <T extends Car> T newCar(Class<T> c) {
        Car car = null;
        String identifier = null;
        try {
            Class componentClass = Class.forName(c.getName());
            if (componentClass.isAnnotationPresent(CarColor.class)) {//制定類型的注釋存在該元素之上
                CarColor component = (CarColor) componentClass.getAnnotation(CarColor.class);
                identifier = component.color();
                System.out.println(String.format("顏色:' %s '", identifier));
            } else {
                System.out.println("com.jasongj.UpperCaseComponent is not annotated by"
                        + " com.jasongj.annotation.Component");
            }
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        try {
            car = (Car) Class.forName(c.getName()).newInstance();
            car.name = identifier;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return (T) car;
    }
}
//客戶頓調(diào)用(傳入對應(yīng)的對象類型來構(gòu)建對應(yīng)的對象 其中應(yīng)用到了反射和注解)
 car= CarFactory2.newCar(BuleCar.class);
 tv_text.setText(car.drive());
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容