设计模式之工厂

普通工厂类图:

设计模式之工厂_第1张图片

java语言实现:

设计模式之工厂_第2张图片

package com.mysql.demo.simpleFactory;

/**
 * 职责
 * @author Mr Huang
 */
public interface Dutys {

     /**
      * 做什么
      * @param o 项目
      */
     public void doWhat(Object o);

     /**
      * 说什么
      * @param o 项目
      */
     public void sayWhat(Object o);

}
package com.mysql.demo.simpleFactory;

/**
 * 待遇
 * @author Mr Huang
 */
public interface Treatment {

    /**
     * 环境待遇
     * @param o 项目
     * @return 待遇
     */
    public String environmentalScience(Object o);

    /**
     * 奖金
     * @param o 项目
     * @return 奖金
     */
    public String bonus(Object o);

}
package com.mysql.demo.simpleFactory;

/**
 * 工程部
 * @author Mr Huang
 */
public class EngineeringDepartment implements Dutys {


    @Override
    public void doWhat(Object o) {

    }

    @Override
    public void sayWhat(Object o) {

    }
}

 

package com.mysql.demo.simpleFactory;

/**
 * 方案部
 * @author Mr Huang
 */
public class ProgramDepartment implements Dutys {


    @Override
    public void doWhat(Object o) {

    }

    @Override
    public void sayWhat(Object o) {

    }
}

 

package com.mysql.demo.simpleFactory;

/**
 * 研发部
 * @author Mr Huang
 */
public class ResearchAndDevelopmentDepartment implements Dutys,Treatment {


    @Override
    public void doWhat(Object o) {

    }

    @Override
    public void sayWhat(Object o) {

    }

    @Override
    public String environmentalScience(Object o) {
        return null;
    }

    @Override
    public String bonus(Object o) {
        return null;
    }
}
package com.mysql.demo.simpleFactory;

/**
 * 公司工厂
 * @author Mr Huang
 */
public class CompanyFactory {

    public static final int TYPE_RANDDDEPARTMENT =0;

    public static final int TYPE_PROGRAMDEPARTMENT = 1;

    public static final int TYPE_ENGINEERINGDEPARTMENT = 2;

    /**
     * 创建职责
     * @param type 部门
     * @return 职责
     */
    public static Dutys createDutys(int type){

        switch (type){

            case TYPE_ENGINEERINGDEPARTMENT:

                return new EngineeringDepartment();

            case TYPE_PROGRAMDEPARTMENT:

                return new ProgramDepartment();

            case TYPE_RANDDDEPARTMENT:

                return new ResearchAndDevelopmentDepartment();

        }

        return null;

    }

    /**
     * 创建奖励
     * @param type 部门
     * @return 奖励
     */
    public static Treatment createTreatment(int type){

        switch (type){

            case TYPE_ENGINEERINGDEPARTMENT:

                return null;

            case TYPE_PROGRAMDEPARTMENT:

                return null;

            case TYPE_RANDDDEPARTMENT:

                return new ResearchAndDevelopmentDepartment();

        }

        return null;

    }

}

方法或者抽象工厂类图:

设计模式之工厂_第3张图片

java语言实现:

 设计模式之工厂_第4张图片

package com.mysql.demo.functionFactory;

/**
 * 职责
 * @author Mr Huang
 */
public interface Dutys {

     /**
      * 做什么
      * @param o 项目
      */
     public void doWhat(Object o);

     /**
      * 说什么
      * @param o 项目
      */
     public void sayWhat(Object o);

}

 

package com.mysql.demo.functionFactory;

/**
 * 待遇
 * @author Mr Huang
 */
public interface Treatment {

    /**
     * 环境待遇
     * @param o 项目
     * @return 待遇
     */
    public String environmentalScience(Object o);

    /**
     * 奖金
     * @param o 项目
     * @return 奖金
     */
    public String bonus(Object o);

}
package com.mysql.demo.functionFactory;

/**
 * 公司工厂
 * @author Mr Huang
 */
public abstract class ICompanyFactory {

    /**
     * 创建职责对象(这个对象是所有部门工厂公有的)
     * @return 职责对象
     */
    public abstract Dutys createDutys();

    /**
     * 创建待遇对象(这个对象是所有部门工厂共有的)
     * @return 待遇对象
     */
    public abstract Treatment createTreatment();

}

 

package com.mysql.demo.functionFactory;

/**
 * 工程部职责
 * @author  Mr Huang
 */
public class EngineeringDepartment implements Dutys {


    @Override
    public void doWhat(Object o) {

    }

    @Override
    public void sayWhat(Object o) {

    }
}
package com.mysql.demo.functionFactory;

/**
 * 工程部工厂
 * @author Mr Huang
 */
public class EngineeringDepartmentFactory extends ICompanyFactory {
    @Override
    public Dutys createDutys() {
        return new EngineeringDepartment();
    }

    @Override
    public Treatment createTreatment() {
        return null;
    }
}
package com.mysql.demo.functionFactory;

/**
 * 方案部职责
 * @author Mr Huang
 */
public class ProgramDepartment implements Dutys {


    @Override
    public void doWhat(Object o) {

    }

    @Override
    public void sayWhat(Object o) {

    }
}

 

package com.mysql.demo.functionFactory;

/**
 * 方案部工厂
 * @author Mr Huang
 */
public class ProgramDepartmentFactory extends ICompanyFactory {
    @Override
    public Dutys createDutys() {
        return new ProgramDepartment();
    }

    @Override
    public Treatment createTreatment() {
        return null;
    }
}
package com.mysql.demo.functionFactory;

/**
 * 研发部职责、待遇
 * @author Mr Huang
 */
public class ResearchAndDevelopmentDepartment implements Dutys, Treatment {


    @Override
    public void doWhat(Object o) {

    }

    @Override
    public void sayWhat(Object o) {

    }

    @Override
    public String environmentalScience(Object o) {
        return null;
    }

    @Override
    public String bonus(Object o) {
        return null;
    }
}
package com.mysql.demo.functionFactory;

/**
 * 研发部工厂
 * @author Mr Huang
 */
public class ResearchAndDevelopmentDepartmentFactory extends ICompanyFactory {
    @Override
    public Dutys createDutys() {
        return new ResearchAndDevelopmentDepartment();
    }

    @Override
    public Treatment createTreatment() {
        return new ResearchAndDevelopmentDepartment();
    }
}

以上工厂设计方法都有优缺点;

你可能感兴趣的:(设计模式)