/** @Lincoln Liu */
  public static void main(String[] args) {
    Operation operAdd = OperationFactory.createOperation('+');
    Operation operSub = OperationFactory.createOperation('-');

    operAdd.setA(11);
    operAdd.setB(22);
    System.out.println(operAdd.getResult());

    operSub.setA(11);
    operSub.setB(22);
    System.out.println(operSub.getResult());
  }
  public void testDeleteOperationCloning() {
    DeleteOperation op = ofact.delete(TEST_KEY, genericCallback);

    DeleteOperation op2 = cloneOne(DeleteOperation.class, op);
    assertEquals(TEST_KEY, op2.getKeys().iterator().next());
    assertCallback(op2);
  }
  public void testSingleGetsOperationCloning() {
    GetsOperation.Callback callback =
        (GetsOperation.Callback) mock(GetsOperation.Callback.class).proxy();
    GetsOperation op = ofact.gets(TEST_KEY, callback);

    GetsOperation op2 = cloneOne(GetsOperation.class, op);
    assertKey(op2);
    assertSame(callback, op.getCallback());
  }
示例#4
0
  public static void main(String[] args) {
    /*
     * 普通面向对象的实现方式:
     * 1、需要明确指定需要实例化类
     * 2、不利于扩展
     *
     * 下面是使用简单工厂模式实现方式
     * 这样添加其他操作,只需要增加相应的实现类并修改工厂类,符合开放-封闭原则中的开放原则,
     * 但是,扩展时仍需要修改工厂类,所以不符合封闭原则
     */

    System.out.println("---计算器程序---");
    System.out.println("---输入第一个操作数---");
    Scanner scanner = new Scanner(System.in);
    String strNum1 = scanner.nextLine();

    System.out.println("---输入运算符---");
    String oper = scanner.nextLine();

    System.out.println("---输入第二个操作数---");
    String strNum2 = scanner.nextLine();

    double num1 = Double.parseDouble(strNum1);
    double num2 = Double.parseDouble(strNum2);

    scanner.close();

    // 2、进行计算
    double result = 0;
    Operation operation = null;

    if ("+".equals(oper)) {
      OperationFactory addOpFactory = new AddOperationFactory();
      operation = addOpFactory.getOperatin();
    } else if ("-".equals(oper)) {
      OperationFactory subtractOpFactory = new SubtractOperationFactory();
      operation = subtractOpFactory.getOperatin();
    } else if ("*".equals(oper)) {
      OperationFactory multiplyOpFactory = new MultiplyOperationFacoty();
      operation = multiplyOpFactory.getOperatin();
    } else if ("/".equals(oper)) {
      OperationFactory dividOpFactory = new DividOperationFactory();
      operation = dividOpFactory.getOperatin();
    } else {
      System.out.println("非法操作符");
    }
    operation.setNum1(num1);
    operation.setNum2(num2);
    result = operation.getResult();

    // 3、返回结果
    System.out.println("---计算结果---");
    System.out.println(strNum1 + oper + strNum2 + "=" + result);
  }
  // These are harder cases as they fan out.
  public void testMultipleGetOperationCloning() {
    Collection<String> keys = Arrays.asList("k1", "k2", "k3");
    GetOperation.Callback callback =
        (GetOperation.Callback) mock(GetOperation.Callback.class).proxy();
    GetOperation op = ofact.get(keys, callback);

    Collection<Operation> ops = ofact.clone(op);
    assertEquals(3, ops.size());

    Collection<String> mutableKeys = new ArrayList<String>(keys);
    int i = 3;
    for (Operation o : ops) {
      assertEquals(i, mutableKeys.size()); // Starting size
      GetOperation go = (GetOperation) o;
      mutableKeys.removeAll(go.getKeys());
      // Verify we matched and removed 1
      assertEquals(--i, mutableKeys.size());
    }
  }
  public void testConcatenationOperationPrependCloning() {
    long casId = 82757248;
    ConcatenationOperation op =
        ofact.cat(ConcatenationType.prepend, casId, TEST_KEY, testData, genericCallback);

    ConcatenationOperation op2 = cloneOne(ConcatenationOperation.class, op);
    assertKey(op2);
    assertSame(ConcatenationType.prepend, op2.getStoreType());
    assertCallback(op2);
  }
  public void testCASOperationCloning() {
    CASOperation op =
        ofact.cas(StoreType.set, "someKey", 727582, 8174, 7175, testData, genericCallback);

    CASOperation op2 = cloneOne(CASOperation.class, op);
    assertKey(op2);
    assertEquals(727582, op2.getCasValue());
    assertEquals(8174, op2.getFlags());
    assertEquals(7175, op2.getExpiration());
    assertBytes(op2.getBytes());
    assertCallback(op2);
  }
  public void testStoreOperationSetCloning() {
    int exp = 823862;
    int flags = 7735;
    StoreOperation op = ofact.store(StoreType.set, TEST_KEY, flags, exp, testData, genericCallback);

    StoreOperation op2 = cloneOne(StoreOperation.class, op);
    assertKey(op2);
    assertEquals(exp, op2.getExpiration());
    assertEquals(flags, op2.getFlags());
    assertSame(StoreType.set, op2.getStoreType());
    assertCallback(op2);
  }
  public void testMutatorOperationDecrCloning() {
    int exp = 823862;
    long def = 28775;
    int by = 7735;
    MutatorOperation op = ofact.mutate(Mutator.decr, TEST_KEY, by, def, exp, genericCallback);

    MutatorOperation op2 = cloneOne(MutatorOperation.class, op);
    assertKey(op2);
    assertEquals(exp, op2.getExpiration());
    assertEquals(def, op2.getDefault());
    assertEquals(by, op2.getBy());
    assertSame(Mutator.decr, op2.getType());
    assertCallback(op2);
  }
  public void testMultipleGetOperationFanout() {
    Collection<String> keys = Arrays.asList("k1", "k2", "k3");
    Mock m = mock(GetOperation.Callback.class);
    OperationStatus st = new OperationStatus(true, "blah");
    m.expects(once()).method("complete");
    m.expects(once()).method("receivedStatus").with(same(st));
    m.expects(once()).method("gotData").with(eq("k1"), eq(1), isA(byte[].class));
    m.expects(once()).method("gotData").with(eq("k2"), eq(2), isA(byte[].class));
    m.expects(once()).method("gotData").with(eq("k3"), eq(3), isA(byte[].class));

    GetOperation.Callback callback = (GetOperation.Callback) m.proxy();
    GetOperation op = ofact.get(keys, callback);

    // Transition each operation callback into the complete state.
    Iterator<String> ki = keys.iterator();
    int i = 0;
    for (Operation o : ofact.clone(op)) {
      GetOperation.Callback cb = (GetOperation.Callback) o.getCallback();
      cb.gotData(ki.next(), ++i, new byte[3]);
      cb.receivedStatus(st);
      cb.complete();
    }
  }
示例#11
0
 public static void main(String[] args) {
   // 1.接受控制台输入
   System.out.println("---计算器程序---");
   System.out.println("输入第一个操作数");
   Scanner scanner = new Scanner(System.in);
   String strNum1 = scanner.nextLine();
   System.out.println("输入运算符");
   String oper = scanner.nextLine();
   System.out.println("输入第二个操作数");
   String strNum2 = scanner.nextLine();
   double result = 0;
   double num1 = Double.parseDouble(strNum1);
   double num2 = Double.parseDouble(strNum2);
   // 2.进行运算
   if ("+".equals(oper)) {
     OperationFactory factory = new AddOperationFactory();
     Operation operation = factory.getOperation();
     operation.setNum1(num1);
     operation.setNum2(num2);
     result = operation.getResult();
   }
   // 3.返回结果
   System.out.println(strNum1 + oper + strNum2 + "=" + result);
 }
 protected <T> T cloneOne(Class<T> c, KeyedOperation t) {
   return assertOne(c, ofact.clone(t));
 }