Factory_Pattern

package factory_pattern;

public abstract class Worker {
    public abstract void work();

    public abstract void eat();
}

package factory_pattern;

public class SuperWorker extends Worker {
    public void work() {
        System.out.println("Work hard and efficient.");
    }

    public void eat() {
        System.out.println("Eat nutritional food.");
    }
}

package factory_pattern;

public class CommonWorker extends Worker {

    public void work() {
        System.out.println("Work hard.");
    }

    public void eat() {
        System.out.println("Eat food.");
    }

}

扫描二维码关注公众号,回复: 3402021 查看本文章

package factory_pattern;

public interface IFactory {
    public Worker CreateWorker();
}

package factory_pattern;

public class SuperFactory implements IFactory {
    public Worker CreateWorker() {
        return new SuperWorker();
    }
}

package factory_pattern;

public class CommonFactory implements IFactory {
    public Worker CreateWorker() {
        return new CommonWorker();
    }
}

package factory_pattern;

public class Main {
    public static void main(String args[]) {
        // IFactory factory = new CommonFactory();
        IFactory factory = new SuperFactory();
        Worker sam = factory.CreateWorker();
        sam.eat();
        sam.work();

    }
}
/***
 * If we want to understand the factory_pattern,we have to talk a lot
 * about the simple_factory_pattern so that realize the "no simple" of
 * the factory_pattern.
 * As we have known that the simple_factory encapsulates the sentence of
 * "switch-case",which works with object'polymorphisim,making the client
 * gets what they need through  giving the simple_factory different parameters.
 * By comparison,the factory_pattern gets further abstraction in the aspect of
 * different factory's implementation.The advantage is leave the "switch-case"
 * out.The only thing we need to do is to choice the different concrete factories
 * for changing different concrete objects.The relation between concrete factory
 * and concrete object is one-to-one,which also means there so many concrete 
 * factories we have to construct.So we can concluded the feature that 
 * factory_pattern is the factory'spolymorphism.
 */

This is a general introduction to the 23 design patterns:
https://blog.csdn.net/GZHarryAnonymous/article/details/81567214

猜你喜欢

转载自blog.csdn.net/GZHarryAnonymous/article/details/82763400
今日推荐