码迷,mamicode.com
首页 > 其他好文 > 详细

工厂方法模式

时间:2016-10-19 01:50:57      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

 工厂方法模式 就是在 简单工厂模式的基础上 将工厂也抽象出来 并具体实现,也是创建者类型。 即一个抽象工厂以及对应的多个实现类,一个抽象产品以及对应的实现类。;

 例子在简单工厂模式基础上扩展

 /*******************抽象产品(登入)***********************/

package functionFactoryType;

/**
 * 登入接口,以供各种登入方式实现接口
 * (在工厂模式中,实现该登入接口的相当于各种对应产品)
 * @author dxx
 *
 */
public interface LoginInteface {
    /**
     *
     * @param name  用户名
     * @param password 密码
     * @return 正确返回true,否则返回false
     */
    public boolean  verifyLogin(String name,String password);

}

/****************登入实现类(密码登入)*****************************/

package functionFactoryType;

/**
 * 通过密码的登入验证
 * @author dxx
 *
 */
public class PasswordLogin implements LoginInteface {
    
    /**
     * 通过密码等验证进行登入
     */
    public boolean verifyLogin(String name, String password) {
        System.out.println("密码登入验证中间操作");
        return true;
    }

}

/**********************登入实现类(密码登域名*********************************/

package functionFactoryType;

/**
 * 域名登入
 * @author dxx
 *
 */
public class DomainLogin  implements LoginInteface{
    
    /**
     * 域名登入验证
     */
    public boolean verifyLogin(String name, String password) {
        System.out.println("执行域名登入的验证,执行相关中间操作");
        return true;
    }

}

/*****************抽象工厂*******************************/

package functionFactoryType;

/**
 * 抽象工厂
 * @author dxx
 *
 */
public interface FactoryInteface {
    /**
     * 创建对应的产品工厂
     * @return
     */
    public  LoginInteface createFactory();

}

/****************密码登入工厂***************************/

package functionFactoryType;

/**
 * 实现工厂接口,创建某一种工厂
 * @author dxx
 *
 */
public class PasswordFactory  implements FactoryInteface{
    /**
     * 生产某一种产品的工厂
     */
    public LoginInteface createFactory() {
        return new PasswordLogin();
    }

}

/*******************域名登入工厂***************************/

package functionFactoryType;


/**
 * 创建具体的工厂
 * @author dxx
 *
 */
public class DomainFactory implements FactoryInteface{
    /**
     * 实现工厂接口,生产指定一种产品
     */
    public LoginInteface createFactory() {
        return new PasswordLogin();
    }
}

 

然后实例化对应的工厂 就可以生产指定的产品, 不用将一堆的判断逻辑放在 工厂中实现某一种产品的实例

 

工厂方法模式

标签:

原文地址:http://www.cnblogs.com/tcdxx/p/5975469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!