खोज…


सरल MVP उदाहरण

एमवीपी पैटर्न के सरल उदाहरण उपयोग को समझाने के लिए, निम्नलिखित कोड पर विचार करें जो केवल एक बटन और एक लेबल के साथ एक सरल यूआई बनाता है। जब बटन पर क्लिक किया जाता है, तो बटन पर क्लिक किए जाने की संख्या के साथ लेबल अपडेट होता है।

हमारे पास 5 वर्ग हैं:

  • मॉडल - राज्य बनाए रखने के लिए पीओजेओ (एमवीपी में एम)
  • देखें - यूआई कोड के साथ वर्ग (एमवीपी में वी)
  • ViewListener - इंटरफ़ेस दृश्य में कार्यों का जवाब देने के लिए तरीके प्रदान करता है
  • प्रस्तुतकर्ता - इनपुट पर प्रतिक्रिया करता है, और दृश्य को अपडेट करता है (P MVV में)
  • अनुप्रयोग - सब कुछ एक साथ खींचने और एप्लिकेशन लॉन्च करने के लिए "मुख्य" वर्ग

एक न्यूनतम "मॉडल" वर्ग जो सिर्फ एक count चर बनाए रखता है।

/**
 * A minimal class to maintain some state 
 */
public class Model {
    private int count = 0;

    public void addOneToCount() {
        count++;
    }

    public int getCount() {
        return count;
    }
}

श्रोताओं को सूचित करने के लिए एक न्यूनतम इंटरफ़ेस:

/**
 * Provides methods to notify on user interaction
 */
public interface ViewListener {
    public void onButtonClicked();
}

दृश्य वर्ग सभी UI तत्वों का निर्माण करता है। दृश्य, और केवल दृश्य, में UI तत्वों (यानी कोई बटन, पाठ फ़ील्ड, आदि प्रस्तुतकर्ता या अन्य वर्गों में) का संदर्भ होना चाहिए।

/**
 * Provides the UI elements
 */

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

public class View {
    // A list of listeners subscribed to this view
    private final ArrayList<ViewListener> listeners;
    private final JLabel label;
    
    public View() {
        final JFrame frame = new JFrame();
        frame.setSize(200, 100);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout());

        final JButton button = new JButton("Hello, world!");

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                notifyListenersOnButtonClicked();
            }
        });
        frame.add(button);

        label = new JLabel();
        frame.add(label);

        this.listeners = new ArrayList<ViewListener>();

        frame.setVisible(true);
    }

    // Iterate through the list, notifying each listner individualy 
    private void notifyListenersOnButtonClicked() {
        for (final ViewListener listener : listeners) {
            listener.onButtonClicked();
        }
    }

    // Subscribe a listener
    public void addListener(final ViewListener listener) {
        listeners.add(listener);
    }

    public void setLabelText(final String text) {
        label.setText(text);
    }
}

सूचना तर्क को Java8 में भी इस तरह कोडित किया जा सकता है:

        ...
        final Button button = new Button("Hello, world!");
        // In order to do so, our interface must be changed to accept the event parametre
        button.addActionListener((event) -> {
            notifyListeners(ViewListener::onButtonClicked, event);
            // Example of calling methodThatTakesALong, would be the same as callying:
            // notifyListeners((listener, long)->listener.methodThatTakesALong(long), 10L)
            notifyListeners(ViewListener::methodThatTakesALong, 10L);
        });
        frame.add(button);
        ...

/**
 * Iterates through the subscribed listeneres notifying each listener individually.
 * Note: the {@literal '<T>' in private <T> void} is a Bounded Type Parametre. 
 *
 * @param <T>      Any Reference Type (basically a class).
 * 
 * @param consumer A method with two parameters and no return, 
 *                 the 1st parametre is a ViewListner, 
 *                 the 2nd parametre is value of type T.
 * 
 * @param data     The value used as parametre for the second argument of the
 *                 method described by the parametre consumer.
 */
private <T> void notifyListeners(final BiConsumer<ViewListener, T> consumer, final T data) {
    // Iterate through the list, notifying each listener, java8 style 
    listeners.forEach((listener) -> {

        // Calls the funcion described by the object consumer.
        consumer.accept(listener, data);

        // When this method is called using ViewListener::onButtonClicked
        // the line: consumer.accept(listener,data); can be read as:
        // void accept(ViewListener listener, ActionEvent data) {
        //     listener.onButtonClicked(data);
        // }
        
    });
}

कार्रवाई को एक पैरामीयर के रूप में लेने के लिए इंटरफ़ेस को फिर से सक्रिय किया जाना चाहिए:

public interface ViewListener {
    public void onButtonClicked(ActionEvent evt);
    // Example of methodThatTakesALong signature
    public void methodThatTakesALong(long );
}

यहां केवल एक सूचना-विधि की आवश्यकता है, वास्तविक श्रोता विधि और इसके पैरामीटर को पैरामीटर के रूप में पारित किया जाता है। यदि आवश्यक हो, तो इसका उपयोग वास्तविक घटना से कुछ कम निफ्टी के लिए भी किया जा सकता है, यह तब तक काम करता है जब तक कि इंटरफ़ेस में कोई विधि न हो, जैसे:

        notifyListeners(ViewListener::methodThatTakesALong, -1L);

प्रस्तुतकर्ता दृश्य में ले सकता है और एक श्रोता के रूप में खुद को जोड़ सकता है। जब दृश्य में बटन पर क्लिक किया जाता है, तो दृश्य सभी श्रोताओं (प्रस्तुतकर्ता सहित) को सूचित करता है। अब जब प्रस्तुतकर्ता को सूचित किया जाता है, तो यह मॉडल (यानी आवेदन की स्थिति) को अपडेट करने के लिए उचित कार्रवाई कर सकता है, और फिर तदनुसार दृश्य को अपडेट कर सकता है।

/**
 * Responsible to responding to user interaction and updating the view
 */
public class Presenter implements ViewListener {
    private final View view;
    private final Model model;

    public Presenter(final View view, final Model model) {
        this.view = view;
        view.addListener(this);
        this.model = model;
    }

    @Override
    public void onButtonClicked() {
        // Update the model (ie. the state of the application)
        model.addOneToCount();
        // Update the view
        view.setLabelText(String.valueOf(model.getCount()));
    }
}

सब कुछ एक साथ रखने के लिए, दृश्य बनाया जा सकता है और प्रस्तुतकर्ता में इंजेक्ट किया जा सकता है। इसी तरह, एक प्रारंभिक मॉडल बनाया और इंजेक्ट किया जा सकता है। जबकि दोनों को प्रस्तुतकर्ता में बनाया जा सकता है, उन्हें निर्माणकर्ता में इंजेक्ट करना बहुत सरल परीक्षण के लिए अनुमति देता है।

public class Application {
    public Application() {
        final View view = new View();
        final Model model = new Model();
        new Presenter(view, model);
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Application();
            }
        });
    }
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow