多一点真诚、多一点套路、少一点随意--- 适配器模式Adapter
文章来源:http://blog.youkuaiyun.com/guolin_blog/article/details/9400141
package com.pattern.count;
public interface ServerPlayerCount {
String getServerName();//服务器名称
int getPlayerCount();//服务器玩家数
}
package com.pattern.countImpl;
import com.pattern.Utils.UtilsOld;
import com.pattern.count.ServerPlayerCount;
public class ServerOneCount extends UtilsOld implements ServerPlayerCount {
@Override
public String getServerName() {
return "服务器1";
}
@Override
public int getPlayerCount() {
return getOnlinePlayerCount();
}
@Override
public String toString() {
return "ServerOneCount{" + getServerName() + " " + getPlayerCount() + " }";
}
}
package com.pattern.countImpl;
import com.pattern.Utils.Utils;
import com.pattern.count.ServerPlayerCount;
public class ServerTwoCount implements ServerPlayerCount {
@Override
public String getServerName() {
return "服务器2";
}
@Override
public int getPlayerCount() {
return Utils.getOnlinePlayerCount(2);
}
@Override
public String toString() {
return "ServerTwoCount{" + getServerName() + " " + getPlayerCount() + " }";
}
}
package com.pattern.Utils;
public class Utils {
public static int getOnlinePlayerCount(int serverId) {
if (serverId == 2)
return 2000;
if (serverId == 3)
return 3000;
return -1;
}
}
package com.pattern.Utils;
public class UtilsOld {
public static int getOnlinePlayerCount() {
return 1000;
}
}
package com.pattern.main;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.pattern.count.ServerPlayerCount;
import com.pattern.countImpl.ServerOneCount;
import com.pattern.countImpl.ServerTwoCount;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ServerPlayerCount serverPlayerCount = new ServerOneCount();
Log.i(TAG, "onCreate: " + serverPlayerCount.toString());
serverPlayerCount = new ServerTwoCount();
Log.i(TAG, "onCreate: " + serverPlayerCount.toString());
}
}