MVC-MVP-MVVM详解

2023-04-13  本文已影响0人  面熟_gson

1 / 1

M-V-C分别是什么 代码举例

1 / 1

MVC是一种常见的架构模式,由三个主要组件组成:

以下是MVC的一个简单示例代码:

java

// Model
public class User {
    private String name;
    private String email;
    
    // Getters and Setters
}

// View
public class UserView {
    public void displayUser(String name, String email) {
        System.out.println("Name: " + name);
        System.out.println("Email: " + email);
    }
}

// Controller
public class UserController {
    private User model;
    private UserView view;
    
    public UserController(User model, UserView view) {
        this.model = model;
        this.view = view;
    }
    
    public void updateUser(String name, String email) {
        model.setName(name);
        model.setEmail(email);
    }
    
    public void displayUser() {
        String name = model.getName();
        String email = model.getEmail();
        view.displayUser(name, email);
    }
}

// Usage
public static void main(String[] args) {
    User user = new User();
    user.setName("John");
    user.setEmail("john@example.com");
    
    UserView userView = new UserView();
    UserController userController = new UserController(user, userView);
    
    // Update user information
    userController.updateUser("Jane", "jane@example.com");
    
    // Display user information
    userController.displayUser();
}

在这个例子中,Model是User类,View是UserView类,Controller是UserController类。UserController充当中介者,协调User和UserView之间的通信。在main函数中,我们创建了一个User实例,并使用UserController来更新和显示用户信息。

1 / 1

在android中M-V-P分别是什么 代码举例

1 / 1

在Android开发中,MVP是一种常见的架构模式,由三个主要组件组成:

以下是MVP的一个简单示例代码:

java

// Model
public class User {
    private String name;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

// View
public interface UserView {
    void showUser(User user);
}

// Presenter
public class UserPresenter {
    private User model;
    private UserView view;

    public UserPresenter(User model, UserView view) {
        this.model = model;
        this.view = view;
    }

    public void updateUser(String name, String email) {
        model.setName(name);
        model.setEmail(email);
        view.showUser(model);
    }
}

// Usage
public class MainActivity extends AppCompatActivity implements UserView {
    private EditText nameEditText;
    private EditText emailEditText;
    private Button saveButton;
    private UserPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize views
        nameEditText = findViewById(R.id.nameEditText);
        emailEditText = findViewById(R.id.emailEditText);
        saveButton = findViewById(R.id.saveButton);

        // Initialize presenter
        User user = new User();
        presenter = new UserPresenter(user, this);

        // Set click listener on save button
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nameEditText.getText().toString();
                String email = emailEditText.getText().toString();
                presenter.updateUser(name, email);
            }
        });
    }

    @Override
    public void showUser(User user) {
        nameEditText.setText(user.getName());
        emailEditText.setText(user.getEmail());
    }
}

在这个例子中,Model是User类,View是UserView接口,Presenter是UserPresenter类。UserPresenter充当中介者,协调User和UserView之间的通信。在MainActivity中,我们初始化了UserPresenter并将其与UserView关联。当用户单击保存按钮时,我们使用UserPresenter来更新User并将其显示在UserView中。在此示例中,UserView是MainActivity,并且实现了showUser方法,该方法用于显示User信息。

1 / 1

在android中M-V-V-M分别是什么 代码举例

1 / 1

在Android开发中,MVVM是一种常见的架构模式,由三个主要组件组成:

以下是MVVM的一个简单示例代码:

java

// Model
public class User {
    private String name;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

// View
public class MainActivity extends AppCompatActivity {
    private EditText nameEditText;
    private EditText emailEditText;
    private Button saveButton;
    private UserViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize views
        nameEditText = findViewById(R.id.nameEditText);
        emailEditText = findViewById(R.id.emailEditText);
        saveButton = findViewById(R.id.saveButton);

        // Initialize view model
        viewModel = new ViewModelProvider(this).get(UserViewModel.class);

        // Set observer on user live data
        viewModel.getUserLiveData().observe(this, new Observer<User>() {
            @Override
            public void onChanged(User user) {
                nameEditText.setText(user.getName());
                emailEditText.setText(user.getEmail());
            }
        });

        // Set click listener on save button
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nameEditText.getText().toString();
                String email = emailEditText.getText().toString();
                viewModel.updateUser(name, email);
            }
        });
    }
}

// ViewModel
public class UserViewModel extends ViewModel {
    private MutableLiveData<User> userLiveData = new MutableLiveData<>();
    private User user = new User();

    public UserViewModel() {
        userLiveData.setValue(user);
    }

    public LiveData<User> getUserLiveData() {
        return userLiveData;
    }

    public void updateUser(String name, String email) {
        user.setName(name);
        user.setEmail(email);
        userLiveData.setValue(user);
    }
}

在这个例子中,Model是User类,View是MainActivity类,ViewModel是UserViewModel类。UserViewModel充当中介者,协调User和MainActivity之间的通信。在MainActivity中,我们初始化了UserViewModel并将其与MainActivity关联。当用户单击保存按钮时,我们使用UserViewModel来更新User并将其显示在MainActivity中。在此示例中,UserLiveData是LiveData<User>对象,用于在ViewModel和View之间共享数据。当ViewModel更新User时,LiveData会自动通知View进行更新。

上一篇下一篇

猜你喜欢

热点阅读