设计模式子普通工厂模式

在需要不同的条件产生具有多态性质的一组类时,可以考虑使用工厂类来生成.
优点就是职责简单实现单一,实现具体代码和实现解耦,工厂类则是关键里面包含了具体的逻辑判断,缺点也很明显就是如果产品类多的话一堆逻辑判断还是比较恶心的。看示例代码

package com.demo.stragetry.


public class FactoryTest {
    static interface Operation{
        public void calculate();
    }
    public static class AddOperation implements Operation{

        @Override
        public void calculate() {
            // TODO Auto-generated method stub
            System.out.println("add operation");
        }

    }
    public static class SubOperation implements Operation{

        @Override
        public void calculate() {
            // TODO Auto-generated method stub
            System.out.println("sub operation");
        }

    }
    public static class MulOperation implements Operation{

        @Override
        public void calculate() {
            // TODO Auto-generated method stub
            System.out.println("mul operation");
        }

    }
    public static class DivOperation implements Operation{

        @Override
        public void calculate() {
            // TODO Auto-generated method stub
            System.out.println("div operation");
        }

    }
    public static class FaltOperation implements Operation{

        @Override
        public void calculate() {
            // TODO Auto-generated method stub
            System.out.println("Falt operation");
        }

    }
    public static class OperationFactory{
        public Operation calculate(String operation){
            if("add".equals(operation)){
                return new AddOperation();
            }else if("sub".equals(operation)){
                return new SubOperation();
            }else if("mul".equals(operation)){
                return new MulOperation();
            }else if("div".equals(operation)){
                return new DivOperation();
            }else{
                return new FaltOperation();
            }
        }
    }

    public static void main(String[] args) {
        OperationFactory factory = new OperationFactory();
        Operation operation= factory.calculate("add");
        operation.calculate();
    }
}

设计模式之多工厂模式
该模式是对普通工厂模式的改进,在普通工厂模式中如果字符串参数出错,则不能正确的创建对象,而多工厂模式则是针对不同对象分别创建对象,废话不多说了看代码吧。。。

package com.demo.stragetry;


public class FactoryTest {
    static interface Operation{
        public void calculate();
    }
    public static class AddOperation implements Operation{

        @Override
        public void calculate() {
            // TODO Auto-generated method stub
            System.out.println("add operation");
        }

    }
    public static class SubOperation implements Operation{

        @Override
        public void calculate() {
            // TODO Auto-generated method stub
            System.out.println("sub operation");
        }

    }
    public static class MulOperation implements Operation{

        @Override
        public void calculate() {
            // TODO Auto-generated method stub
            System.out.println("mul operation");
        }

    }
    public static class DivOperation implements Operation{

        @Override
        public void calculate() {
            // TODO Auto-generated method stub
            System.out.println("div operation");
        }

    }
    public static class FaltOperation implements Operation{

        @Override
        public void calculate() {
            // TODO Auto-generated method stub
            System.out.println("Falt operation");
        }

    }
    public static class OperationFactory{

        public  Operation addOperation(){
            return new AddOperation();
        }
        public Operation subOperation(){
            return new SubOperation();
        }
        public Operation mulOperation(){
            return new MulOperation();
        }
        public Operation divOperation(){
            return new DivOperation();
        }
    }

    public static void main(String[] args) {
        OperationFactory factory = new OperationFactory();
//      Operation operation= factory.calculate("add");
        Operation operation = factory.addOperation();
        operation.calculate();
    }
}

设计模式之静态工厂模式
该模式是针对多工厂模式而言,若多个对象则创建比较麻烦,将类和方法都改为static 直接调用而不需要新建代码就不放了。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值