设计模式---工厂模式

简单工厂模式

创建实体接口 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() ;
}
  1. 有方法体
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());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值