MOOC浙江大学オブジェクト指向プログラミング-Javaノート(5)

------------ウェンカイ氏
が5番目のコースデザインの原則を教えましたコースの
コア部分は、面倒なものから簡潔なものまで、プログラムの最適化に反映されています。

  1. 大量のコードのコピーを回避する
  2. 多くのif-elseを避ける
  3. クラスのメンバー変数をプライベートアクセス属性に設定する

コードの重複を排除:

1.関数のカプセル化

  System.out.println("你在" + currentRoom);
  System.out.print("出口有: ");
  if(currentRoom.northExit != null)
      System.out.print("north ");
  if(currentRoom.eastExit != null)
      System.out.print("east ");
  if(currentRoom.southExit != null)
      System.out.print("south ");
  if(currentRoom.westExit != null)
      System.out.print("west ");
  System.out.println();
  ----------------------------------
  public void showPrompt(Room cRoom) {
    	System.out.println("现在你在" + cRoom);
    	System.out.print("出口有: ");
	    if(currentRoom.northExit != null)
      		System.out.print("north ");
  		if(currentRoom.eastExit != null)
      		System.out.print("east ");
  		if(currentRoom.southExit != null)
      		System.out.print("south ");
  		if(currentRoom.westExit != null)
      		System.out.print("west ");
	    System.out.println();
    	}
    }

2.親
コードの
カップリングと集約カップリング:クラス間の距離を維持する必要があります。
集約:クラスとメソッドの場合、プログラムの単一のユニットが実行するタスクの数とタイプに対応します。メソッドは、明確に定義された1つのことのみを担当します。

スケーラビリティ

問題:プログラムにハードコーディングされている場合、ハードコードの多くのタイプは、端末を介した文字インターフェース、入出力に基づいています。
解決策:プログラムからフレームワークとデータを特定し、コードを使用してフレームワークを実装し、一部の関数をデータとしてロードします(コンテナー)。コンテナーを使用して問題を解決し、柔軟性を得るのが得意です

public class Room {
    public String description;
    public Room northExit;
    public Room southExit;
    public Room eastExit;
    public Room westExit;

    public Room(String description) 
    {
        this.description = description;
    }
}

    private void goRoom(String direction) 
    {
        Room nextRoom = null;
        if(direction.equals("north")) {
            nextRoom = currentRoom.northExit;
        }
        if(direction.equals("east")) {
            nextRoom = currentRoom.eastExit;
        }
        if(direction.equals("south")) {
            nextRoom = currentRoom.southExit;
        }
        if(direction.equals("west")) {
            nextRoom = currentRoom.westExit;
        }

        if (nextRoom == null) {
            System.out.println("那里没有门!");
        }
        else {
            currentRoom = nextRoom;
            System.out.println("你在" + currentRoom);
            System.out.print("出口有: ");
            if(currentRoom.northExit != null)
                System.out.print("north ");
            if(currentRoom.eastExit != null)
                System.out.print("east ");
            if(currentRoom.southExit != null)
                System.out.print("south ");
            if(currentRoom.westExit != null)
                System.out.print("west ");
            System.out.println();
        }
    }

変換後

public class Room {
    private String description;
    private HashMap<String, Room> exits = new HashMap<String, Room>();
Public String getExits () {
    	StringBuffer sb = new StringBuffer();
    	for(String dir: exits.keySet()) {
    		sb.append(dir);
    		sb.append(' ');
    	}
    	return sb.toString();
    }
public void showPrompt(Room cRoom) {
    System.out.println("现在你在" + cRoom);
    System.out.print("出口有: ");
    ------
    String exits = cRoom.getExits();
    System.out.println(exits);
    -------
    System.out.println();
 }

おすすめ

転載: www.cnblogs.com/yuecheng/p/12714029.html