工厂模式

上代码

package com.bfh.timeline.Factory;

/**
 * Created by bfh on 2015/11/15.
 */
public interface Jobs {

    String getMyjob(int i );
}
View Code
package com.bfh.timeline.Factory.impl;

import com.bfh.timeline.Factory.Jobs;

/**
 * Created by bfh on 2015/11/15.
 */
public class Myjob  implements Jobs{
    @Override
    public String getMyjob(int i ) {
        if (i==1) {
            return "sb1";
        }else if (i==2) {
            return "sb1";
        }else if (i==3) {
            return "sb1";
        }else  if (i==4) {
            return "sb1";
        }else{
            return "sb2";
        }
    }
}
View Code
package com.bfh.timeline.Factory;

import com.bfh.timeline.Factory.impl.Myjob;

/**
 * Created by bfh on 2015/11/15.
 */
public class Factory {

    public String CreateMyjob(int  i ){
        return  new Myjob().getMyjob(i);
    }
}
View Code
package com.bfh.timeline.Factory.test;

import com.bfh.timeline.Factory.Factory;

/**
 * Created by bfh on 2015/11/15.
 */
public class Test01 {

    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        for (int i = 0; i <6 ; i++) {
            System.out.println(Factory.class.newInstance().CreateMyjob(i));
        }
    }
}
View Code

 

posted on 2015-11-15 11:48  盖世大将军  阅读(62)  评论(0)    收藏  举报

导航