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.");
    }

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值