Answer the question
In order to leave comments, you need to log in
Android development: what should be the scalable code?
Good day, colleagues.
I am a beginner android developer and have no experience in commercial projects yet.
My own projects contain a small number of activities/screens.
Tell me please, where can I read about the "rules of etiquette" for applications with 100+ screens?
Those. What is the difference between highly scalable code and badly scalable code in Android?
Answer the question
In order to leave comments, you need to log in
In order to write a scalable one, you need to know not only GOF patterns,
but also architectural ones: MVC, MVP, MVVM, MVI. It is important to study the advantages and disadvantages of each.
The principles of SOLID are also at the forefront, below I will give small examples:
1. Single Responsibility principle - the principle of single responsibility. A method/class should only perform one task. For example, a method designed to download data from the network should not deal with error handling.
I'll write in pseudocode:
public void loadData(String url){
repository.fetchProducts().get(products->{
view.showProducts(products);
}, throwable ->{
if(throwable instanceof IOexception){
view.showNoNetwork();
}else(throwable instanceof HTTPException){
HTTPException exception = (HTTPException)throwable;
switch(exception.getCode()){
case 400:
view.showError(exception.getMessage());
break;
case 401:
view.showUnauthorized();
break;
...
}
}
...
});
}
public void loadData(String url){
repository.fetchProducts().get(products-> view.showProducts(products),
throwable -> ErrorUtil.handleError(throwable, view));
}
public class ErrorUtil{
public static void handleError(Throwable throwable, View view){
if(throwable instanceof IOexception){
view.showNoNetwork();
}else(throwable instanceof HTTPException){
HTTPException exception = (HTTPException)throwable;
switch(exception.getCode()){
case 400:
view.showError(exception.getMessage());
break;
case 401:
view.showUnauthorized();
break;
...
}
}
...
}
}
public class ToolbarManager{
public void showToolbar(int type){
switch(type){
case MAIN:
....
//огромный кусок кода для показа тулбара для главного экрана
....
break;
case PROFILE:
...
//огромный кусок кода для показа тулбара для экрана профиля
...
...
}
}
}
public interface ToolbarManager{
void showToolbar();
}
public class MainToolbarManager implements ToolbarManager{
public void showToolbar(){
....
//огромный кусок кода для показа тулбара для главного экрана
....
}
}
public class ProfileToolbarManager implements ToolbarManager{
public void showToolbar(){
....
//огромный кусок кода для показа тулбара для экрана профиля
....
}
}
public interface Car {
void turnOnEngine(); //запустить двигатель
void accelerate(); //подать газ
}
public class MotorCar implements Car {
private Engine engine;
public void turnOnEngine() {
//вруби мотор!
engine.on();
}
public void accelerate() {
//поезжай вперед!
engine.powerOn(1000);
}
}
public class ElectricCar implements Car {
public void turnOnEngine() {
throw new AssertionError("А у меня вообще нет двигателя");
}
public void accelerate() {
//ускорение сумасшедшее!
}
}
public interface Engineful {
void turnOnEngine();
}
public interface Acceleratable {
void accelerate();
}
public class MotorCar implements Engineful, Acceleratable {
private Engine engine;
public void turnOnEngine() {
//вруби мотор!
engine.on();
}
public void accelerate() {
//поезжай вперед!
engine.powerOn(1000);
}
}
public class ElectricCar implements Acceleratable {
public void accelerate() {
//ускорение сумасшедшее!
}
}
public interface TextWatcher{
public void beforeTextChanged(CharSequence s, int start, int count, int after);
public void onTextChanged(CharSequence s, int start, int before, int count);
public void afterTextChanged(Editable s);
}
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//метод, который нам нужен
//какие-то полезные действия
}
});
public class Repository{
private final NetworkManager networkManager;
private final AppDataBase appDataBase;
public Repository(){
this.networkManager = new NetworkManager();
this.AppDataBase = new AppDataBase();
}
}
public interface RemoteDataSource{}
public interface LocalDataSource{}
public class NetworkManager implements RemoteDataSource{}
public class AppDataBase implements LocalDataSource{}
public class Repository{
private final RemoteDataSource remoteDataSource;
private final LocalDataSource localDataSource;
public Repository(RemoteDataSource remoteDataSource, LocalDataSource localDataSource){
this.remoteDataSource = remoteDataSource;
this.localDataSource = localDataSource;
}
}
public class FakeNetworkManager implements RemoteDataSource{}
public class FakeAppDataBase implements LocalDataSource{}
Poorly scalable is when you have girls in the same class dancing and cows grazing. Google 'android application architecture', mvvm, mvp. In a nutshell, the division of the application into 'layers' allows you to scale and maintain the application normally, you will find examples of such division by googling the above
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question