翻譯TIPatterns–簡單化(Simplifying Idioms)

來源:互聯網
上載者:User

    小布同學這一小節寫的很不爽,翻起來一頭霧水。我想他大概的意思是說,如果方法需要傳遞比較多的參數,那最好把這些參數包成一個類。

 

簡單化(Simplifying Idioms)

 

    在研究複雜技術之前,瞭解一下使代碼簡單明了的基本方法是很有協助的。

 

信使(Messenger)

    最普通的方法就是通過信使(messenger),它簡單的將資訊打包到一個用於傳送的對象,而不是將這些資訊片段單獨傳送。注意,如果沒有信使(messenger),translate()的代碼讀起來會相當混亂。

//: simplifying:MessengerDemo.java
package simplifying;
import junit.framework.*;
class Point { // A messenger
 public int x, y, z; // Since it's just a carrier
 public Point(int x, int y, int z) {
  this.x = x;
  this.y = y;
  this.z = z;
 }
 public Point(Point p) { // Copy-constructor
  this.x = p.x;
  this.y = p.y;
  this.z = p.z;
 }
 public String toString() {
  return "x: " + x + " y: " + y + " z: " + z;
 }
}
class Vector {
 public int magnitude, direction;
 public Vector(int magnitude, int direction) {
  this.magnitude = magnitude;
  this.direction = direction;
 }
}
class Space {
  public static Point translate(Point p, Vector v) {
    p = new Point(p); // Don't modify the original
    // Perform calculation using v. Dummy calculation:
     p.x = p.x + 1;
    p.y = p.y + 1;
    p.z = p.z + 1;
    return p;
   }
}
public class MessengerDemo extends TestCase {
  public void test() {
    Point p1 = new Point(1, 2, 3);
    Point p2 = Space.translate(p1, new Vector(11, 47));
    String result = "p1: " + p1 + " p2: " + p2;
    System.out.println(result);
    assertEquals(result,
      "p1: x: 1 y: 2 z: 3 p2: x: 2 y: 3 z: 4");
   }
  public static void main(String[] args) {
    junit.textui.TestRunner.run(MessengerDemo.class);
   }
} ///:~

    因為messenger只是用來傳送資料,它所傳送的資料通常聲明為公有的(public),以便於存取。但是,你可以根據自己的需要把它們聲明成私人的(private)。

 

集合型參數???(collecting parameter)

    collecting parameter 是messenger的兄弟,messenger傳參數給某個方法,而collecting parameter 從這個方法擷取資訊。一般說來,這通常會用在collecting parameter傳給多個方法(multiple methods)的情況下,就像一隻傳粉的蜜蜂。

    容器是一種特別有用的collecting parameter,因為它本來就是用來動態添加對象的。

 

//: simplifying:CollectingParameterDemo.java
package simplifying;
import java.util.*;
import junit.framework.*;
class CollectingParameter extends ArrayList {}
class Filler {
 public void f(CollectingParameter cp) {
  cp.add("accumulating");
 }
 public void g(CollectingParameter cp) {
  cp.add("items");
 }
 public void h(CollectingParameter cp) {
  cp.add("as we go");
 }
}
public class CollectingParameterDemo extends TestCase {
 public void test() {
  Filler filler = new Filler();
  CollectingParameter cp = new CollectingParameter();
  filler.f(cp);
  filler.g(cp);
  filler.h(cp);
  String result = "" + cp;
  System.out.println(cp);
  assertEquals(result,"[accumulating, items, as we go]");
 }
 public static void main(String[] args) {
  junit.textui.TestRunner.run(
   CollectingParameterDemo.class);
 }
} ///:~

    Collecting parameter 必須支援通過某些方法設定或者插入一些值。根據這個定義,信使可以當作collecting parameter 來用,前提是collecting parameter 是由它所傳遞給的方法來修改的。

 

目錄

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.