第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

設(shè)計(jì)模式--工廠模式--工廠方法模式

標(biāo)簽:
Java 設(shè)計(jì)
工厂模式有2种:工厂方法模式、抽象工厂模式
    *工厂方法模式:一个产品系列
    *抽象工厂模式:一个产品族(包含多个系列)

1.工厂方法模式的实现:以华为P8系列为例

 - 定义接口
    public interface HuaWeiP8SeriesInterface {
        public void draw();
    }

 - 实现接口
    public class P8Young implements HuaWeiP8SeriesInterface {
        @Override
        public void draw(){
            System.out.println("---------------P8青春版-------------");
        }
    }

    public class P8Standard implements HuaWeiP8SeriesInterface {
        @Override
        public void draw(){
            System.out.println("---------------P8标配版-------------");
        }
    }

 - 创建工厂
    public class P8_ProducteFactory() {
        public HuaWeiP8SeriesInterface getP8ByKey(String key) {
            Map<String, String> typeClassMap = new PropertiesUtils().getProperties();
            String classPath = typeClassMap.get(key);
            try{
                HuaWeiP8SeriesInterface p8 = Class.forName(classPath).newInstance();
                return p8;
            }catch (InstantiationException e) {
        e.printStackTrace();
        } catch (IllegalAccessException e) {
        e.printStackTrace();
        } catch (ClassNotFoundException e) {
        e.printStackTrace();
        }
            return null;
        }
    }

 - 测试类
public class Test {
    public static void main(String[] args) {
        P8_ProducteFactory factory = new P8_ProducteFactory();
        HuaWeiP8SeriesInterface p8 = factory.getP8ByKey("young");
        p8.draw();
    }
}

 - 测试结果
---------------P8青春版-------------


 * 为保证项目的可拓展性,利用properties配置文件,对产品的实体类进行配置

    例如:目前P8系列涉及两个产品:P8青春版、P8标配版
        编写P8Type.properties配置文件如下:
        young=com.factory.factory_method.P8Young
        standard=com.factory.factory_method.P8Standard

*为方便程序读取配置文件,创建PropertiesUtils工具类
    public class PropertiesUtils {
        public Map<String, String> getProperties() {
            Properties properties = new Properties();
            Map<String, String> map = new HashMap<String, String>();
            InputStream in = getClass().getResourceAsStream("P8Type.properties");
            try{
                properties.load(in);
                Enumeration en = properties.propertyNames();
                while(en.hasMoreElements()){
                    String key = (String)en.nextElement();
                    String property = properties.getProperty(key);
                    map.put(key, property);    
                }
            }catch(IOException e) {
                e.printStackTrace();
            }
            return map;
        }
    }

工厂模式的好处:利于项目的后期拓展。
若后期出现P8高配版,则只需在配置文件P8Type.properties中,配置P8高配版实体类的路径;客户端的“工厂”根据P8高配版的key值,调用getP8ByKey方法,即可获得新增的P8高配版实例

點(diǎn)擊查看更多內(nèi)容
4人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消