Java设计模式---工厂模式

工厂模式主要分为 :工厂模式抽象工厂模式

工厂模式

这里写图片描述


package com.facory.model;

public interface HairInterface {
        public  void getHair();
} /*这个是接口,发型分为,左中右*/
=====================================================
package com.facory.model;

import java.util.Map;

public class TestFactoryModel {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub      
        TestFactoryModel TestFactoryModel=new TestFactoryModel();
        HairInterface hair=TestFactoryModel.getHairStyleByClass("model");
        hair.getHair();
    }

    public  HairInterface getHairStyleByClass(String key){

        PropertiesReader property=new PropertiesReader();
        Map map=property.getProperties();
        HairInterface hair = null;
        try {
            hair = (HairInterface) Class.forName((String) map.get(key)).newInstance();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return hair ;
    }

}
/*这个是工厂,用于产生各种发型*/
package com.facory.model;

public class LeftHairStyle implements HairInterface {

    @Override
    public void getHair() {
        // TODO Auto-generated method stub
        System.out.println("-----------创建了一个左分的发型--------------");
    }

}
package com.facory.model;

public class ModdleHairStyle implements HairInterface {

    @Override
    public void getHair() {
        // TODO Auto-generated method stub
        System.out.println("-----------创建了一个中分的发型--------------");
    }

}
package com.facory.model;

public class RightHairStylr implements HairInterface {

    @Override
    public void getHair() {
        // TODO Auto-generated method stub
        System.out.println("-----------创建了一个右分的发型--------------");

    }

}
================================
下面这个就不是很重要了,其实就是一个工具类,读取配置文件的
package com.facory.model;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * properties文件的读取工具
 * @author Administrator
 *
 */
public class PropertiesReader {


    public Map<String, String> getProperties() {

        Properties props = new Properties();
        Map<String, String> map = new HashMap<String, String>();
        try {

            InputStream in = getClass().getResourceAsStream("type.properties");
            props.load(in);
            Enumeration en = props.propertyNames();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                String property = props.getProperty(key);
                map.put(key, property);
//              System.out.println(key + "  " + property);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
配置文件
left=com.facory.model.LeftHairStyle
model=com.facory.model.ModdleHairStyle
right=com.facory.model.RightHairStylr

抽象工厂模式

抽象工厂模型图
这里面定义了一个工厂的接口,两个工厂实现类(圣诞节工厂,新年工厂);又定义了男孩女孩接口,每个接口也是分别对象两个实现类,例如新年男孩、圣诞节男孩实现类。

package com.abstractfactory.model;

public interface FactoryInterface {
    //女孩接口
    public GirlInterface GetGirl();

    //男孩接口
    public BoyInterface GetBoy();
}
/*抽象工厂接口*/
package com.abstractfactory.model;

public class Cfactory implements FactoryInterface {

    @Override
    public GirlInterface GetGirl() {
        // TODO Auto-generated method stub
        return new CGirl() ;
    }

    @Override
    public BoyInterface GetBoy() {
        // TODO Auto-generated method stub
        return new CBoy();
    }

}//圣诞节工厂


----------
package com.abstractfactory.model;

public class Yfactory implements FactoryInterface {

    @Override
    public GirlInterface GetGirl() {
        // TODO Auto-generated method stub
        return new YGirl();
    }

    @Override
    public BoyInterface GetBoy() {
        // TODO Auto-generated method stub
        return new YBoy();
    }

}//新年工厂

然后就是两个工厂的四个实现类
package com.abstractfactory.model;

public class CBoy implements BoyInterface {

    @Override
    public void drawBoy() {
        // TODO Auto-generated method stub
            System.out.println("------------圣诞节男孩----------------");
    }
}
===========================
package com.abstractfactory.model;

public class CGirl implements GirlInterface {

    @Override
    public void drawGirl() {
        // TODO Auto-generated method stub
            System.out.println("------------圣诞节女孩---------------");
    }

}
==============================
package com.abstractfactory.model;

public class YBoy implements BoyInterface {

    @Override
    public void drawBoy() {
        // TODO Auto-generated method stub
        System.out.println("------------新年男孩----------------");

    }

}
========================
package com.abstractfactory.model;

public class YGirl implements GirlInterface {

    @Override
    public void drawGirl() {
        // TODO Auto-generated method stub
        System.out.println("------------新年女孩---------------");

    }

}


区别:与工厂模型不同的是,这里的工厂继承了接口,并且还可以有多重类的,例如新年工厂,圣诞节工厂;每个工厂里面分男女,工厂模式里面只有一个工厂,只能造左,中,右.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值