简单工厂模式
创建实体接口 WeaPon
package SimpleFactory;
public interface WeaPon {
}
SimpleFactory .java
package SimpleFactory;
public class SimpleFactory {
public WeaPon getWeapon(String weaPonName) {
if ("刀".equals(weaPonName)) {
return new WeaPonDao();
} else if ("剑".equals(weaPonName)) {
return new WeaponJian();
} else if ("平底锅".equals(weaPonName)) {
return new WeaponPindiguo();
}
return null;
}
}
实体类 WeaPonDao.java
package SimpleFactory;
public class WeaPonDao implements WeaPon{
}
实体类 WeaponJian.java
package SimpleFactory;
public class WeaponJian implements WeaPon {
}
实体类 WeaponPindiguo.java
package SimpleFactory;
public class WeaponPindiguo implements WeaPon {
}
创建新的类,只需要在新增实体类,并在SimpleFactory 工厂中修改即可。
工厂方法模式
接口
package FuncFactory;
import SimpleFactory.WeaPon;
public interface WeaponFuncFactory {
WeaPon getWeapon() ;
}
工厂实现
package FuncFactory;
import SimpleFactory.WeaPon;
import SimpleFactory.WeaponJian;
public class WeaponJianFuncFactory implements WeaponFuncFactory
{
public WeaPon getWeapon(){
return new WeaponJian();
}
}
测试
import SimpleFactory.SimpleFactory;
public class ProxyTest
{
public static void main(String[] args) {
//工厂方法
WeaponFuncFactory weaponFuncFactory = new WeaponJianFuncFactory();
System.out.println(weaponFuncFactory.getWeapon());
}
}
抽象工厂
抽象工厂定义
1.没有方法体
package AbstractWeaponFactory;
import SimpleFactory.WeaPon;
public abstract class AbstractWeaponFactory {
public abstract WeaPon getDao() ;
public abstract WeaPon getJian() ;
}
- 有方法体
package AbstractWeaponFactory;
import SimpleFactory.WeaPon;
public class WeaponAbstractFactory {
public WeaPon getDao() { return null; }
public WeaPon getJian() { return null;} ;
public WeaPon getPindiguo() { return null; } ;
}
package AbstractWeaponFactory;
import FuncFactory.WeaponDaoFuncFactory;
import FuncFactory.WeaponJianFuncFactory;
import SimpleFactory.WeaPon;
import SimpleFactory.WeaPonDao;
public class WeaponAbstractFactoryInst extends WeaponAbstractFactory {
@Override
public WeaPon getDao(){
return new WeaPonDao();
};
@Override
public WeaPon getJian() {
return new WeaponJianFuncFactory().getWeapon();
}
@Override
public WeaPon getPindiguo(){
return new WeaponDaoFuncFactory().getWeapon();
}
}
抽象工厂实现
package AbstractWeaponFactory;
import FuncFactory.WeaponDaoFuncFactory;
import FuncFactory.WeaponJianFuncFactory;
import SimpleFactory.WeaPon;
import SimpleFactory.WeaPonDao;
public class WeaponAbstractFactoryInst extends WeaponAbstractFactory {
@Override
public WeaPon getDao(){
return new WeaPonDao();
};
@Override
public WeaPon getJian() {
return new WeaponJianFuncFactory().getWeapon();
}
@Override
public WeaPon getPindiguo(){
return new WeaponDaoFuncFactory().getWeapon();
}
}
抽象工厂使用
import AbstractWeaponFactory.WeaponAbstractFactory;
import AbstractWeaponFactory.WeaponAbstractFactoryInst;
import FuncFactory.WeaponFuncFactory;
import FuncFactory.WeaponJianFuncFactory;
public class ProxyTest
{
public static void main(String[] args) {
//抽象工厂
WeaponAbstractFactory waf = new WeaponAbstractFactoryInst();
System.out.println (waf. getDao());
System.out.println (waf. getJian());
}
}