DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

System Design principles

Model-View-Controller (MVC)-

MODEL
The Model contains the pure application data and pure logic describing how to present the data to a user. (Its just a data that is shipped across the application like for example from back-end server view and from front-end view to the database. In java programming, Model can be represented by the use of POJO (Plain-old-java-object) which is a simple java class.

VIEW
The View presents the model’s data to the user. The view knows how to access the model’s data, but it does not know what this data means or what the user can do to manipulate it. View just represent, displays the application’s data on screen. View page are generally in the format of .html or .jsp in java programming (which is flexible).

CONTROLLER
The Controller exists between the view and the model. It listens to events triggered by the view (or another external source) and executes the appropriate reaction to these events. In most cases, the reaction is to call a method on the model. Since the view and the model are connected through a notification mechanism, the result of this action is then automatically reflected in the view.

class Student 
{
    private String rollNo;
    private String name;

    public String getRollNo() 
    {
        return rollNo;
    }

    public void setRollNo(String rollNo) 
    {
        this.rollNo = rollNo;
    }

    public String getName() 
    {
        return name;
    }

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

class StudentView 
{
    public void printStudentDetails(String studentName, String studentRollNo)
    {
        System.out.println("Student: ");
        System.out.println("Name: " + studentName);
        System.out.println("Roll No: " + studentRollNo);
    }
}

class StudentController 
{
    private Student model;
    private StudentView view;

    public StudentController(Student model, StudentView view)
    {
        this.model = model;
        this.view = view;
    }

    public void setStudentName(String name)
    {
        model.setName(name);     
    }

    public String getStudentName()
    {
        return model.getName();  
    }

    public void setStudentRollNo(String rollNo)
    {
        model.setRollNo(rollNo);     
    }

    public String getStudentRollNo()
    {
        return model.getRollNo();    
    }

    public void updateView()
    {            
        view.printStudentDetails(model.getName(), model.getRollNo());
    } 
}

class MVCPattern 
{
    public static void main(String[] args) 
    {
        Student model = retriveStudentFromDatabase();

        StudentView view = new StudentView();

        StudentController controller = new StudentController(model, view);

        controller.updateView();

        controller.setStudentName("Vikram Sharma");

        controller.updateView();
    }

    private static Student retriveStudentFromDatabase()
    {
        Student student = new Student();
        student.setName("Lokesh Sharma");
        student.setRollNo("15UCS157");
        return student;
    }

}

Enter fullscreen mode Exit fullscreen mode

Publisher-Subscriber

The Publish/Subscribe pattern, sometimes known as pub/sub, is an architectural design pattern that enables publishers and subscribers to communicate with one another. In this arrangement, the publisher and subscriber rely on a message broker to send messages from the publisher to the subscribers. Messages (events) are sent out by the host (publisher) to a channel, which subscribers can join.

The Publish/Subscribe (Pub/Sub) model is commonly used in social networks through features such as "following." To better understand how this works, let's consider an example of a social network for sharing recipes.

Top comments (0)