策略模式例子

主要枚举类:

package com.grddt.rddig.strategy;

public enum DealType {
    SIGLEP_TYPE(new Siglep()),
    DAPETURE_TYPE(new Daperture()),
    DSUFFIX_TYPE(new Dsuffix()),
    DPREFIX_TYPE(new Dprefix()),
    MATCHEDG_TYPE(new Matchg());
    private GerStatement gerStatement;

    // 构造方法初始化属性 gerStatement
    DealType(GerStatement gerStatement) {
        this.gerStatement = gerStatement;
    }

    public GerStatement get() {
        return this.gerStatement;
    }
}

接口方法

 public interface GerStatement {

  StatementInterface dealStatement(Gerber gerber, StatementInterface statement, Matcher matcher, int lineIndex, String parameterCode, String modifiers) throws IllegalPositionException, UnsupportedIndexException, IllegalReflectionException, NoLastStatementExistsException, ApertureNotDefinedException, ApertureMacroNotDefinedException, UnsupportedApertureException, UnsupportedMacroException, IllegalParameterModifiersException, UnsupportedParameterCodeException, ArithmeticExpressionUnsupportedException, IllegalParameterException;
}

实现类

public class Dprefix implements GerStatement {

    @Override
    public StatementInterface dealStatement(Gerber gerber, StatementInterface statement, Matcher matcher, int lineIndex, String parameterCode, String modifiers) throws IllegalPositionException, UnsupportedIndexException,  ApertureNotDefinedException {
        int dIndex = Integer.parseInt(matcher.group(1));
        if (statement != null) {
            gerber.add(statement);
        }
        statement = StatementFactory.createDStatement(dIndex, null, gerber);
        return statement;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的策略模式可以通过以下例子来说明: 首先,我们需要定义一个策略接口,例如: ``` public interface Strategy { int doOperation(int num1, int num2); } ``` 然后,我们可以实现不同的策略类,例如: ``` public class OperationAdd implements Strategy { public int doOperation(int num1, int num2) { return num1 + num2; } } public class OperationSubtract implements Strategy { public int doOperation(int num1, int num2) { return num1 - num2; } } public class OperationMultiply implements Strategy { public int doOperation(int num1, int num2) { return num1 * num2; } } ``` 接下来,我们需要一个Context类来使用这些策略,例如: ``` public class Context { private Strategy strategy; public Context(Strategy strategy){ this.strategy = strategy; } public int executeStrategy(int num1, int num2){ return strategy.doOperation(num1, num2); } } ``` 最后,我们可以使用这些类来实现策略模式,例如: ``` public class StrategyPatternDemo { public static void main(String[] args) { Context context = new Context(new OperationAdd()); System.out.println("10 + 5 = " + context.executeStrategy(10, 5)); context = new Context(new OperationSubtract()); System.out.println("10 - 5 = " + context.executeStrategy(10, 5)); context = new Context(new OperationMultiply()); System.out.println("10 * 5 = " + context.executeStrategy(10, 5)); } } ``` 这个例子中,我们定义了一个策略接口和三个不同的策略类,然后使用Context类来使用这些策略。在main方法中,我们可以看到不同的策略被使用来执行不同的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值