工厂模式主要分为 :工厂模式和抽象工厂模式
工厂模式
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("------------新年女孩---------------");
}
}
区别:与工厂模型不同的是,这里的工厂继承了接口,并且还可以有多重类的,例如新年工厂,圣诞节工厂;每个工厂里面分男女,工厂模式里面只有一个工厂,只能造左,中,右.