Zend Framework – MVC Tutorial Ergänzung

Wer zum [post id=1747]Zend Framework – MVC Tutorial[/post] noch eine Ergänzung braucht, der findet sie im nachfolgenden Video. Speziell wird nochmal auf die Übergabe von Parametern und Werten eingegangen sowie auf die Bedeutung des Index -und Error-Controllers.

Zend Framework – MVC Tutorial Ergänzung:

Model-View-Controller Prinzip

View.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.Observable;
import java.util.Observer;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
 
public class View implements Observer
{	// MVC Instances
	private Controller	controller;
	private Model		model;
 
	// Further local variables...
	private JButton button;
 
	// Initialization constructor
    public View(Model model)
    {
    	this.model = model;
    	this.model.addObserver(this);
    	this.controller = new Controller(this,model);
    	initGUI();
    }
 
    // Initialization of Frame and GUI Elements
    private void initGUI()
    {
    	// Frame
	        JFrame frame = new JFrame("Application Titlebar Name");
 
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        frame.setBounds(180,180,400,300);
	        frame.setLayout(null);
	        frame.setResizable(false);
	        frame.setVisible(true);
 
	    // Other GUI Elements...
	        button = new JButton("I'm a Button!");
	        button.setBounds(10,10,200,50);
	        button.setActionCommand("fromButton");
	        button.addActionListener(controller);
 
	        frame.add(button);
    }
 
    // Start the GUI in an Event-Dispatching-Thread
    public static void main(String args [])
    {
        SwingUtilities.invokeLater(new Runnable()
            {
            public void run()
                {
                new View(new Model());
                }
            });
    }
 
    // Setter for the button text
    public void setButtonText(String text)
    {
    	this.button.setText(text);
    }
 
    @Override
    // Event-Handling
    public void update(Observable arg0, Object eventCommand)
    {
        if (eventCommand.equals("done"))
        {
        	if(model.getButtonState())
        		setButtonText("I'm happy :)");
        	else
        		setButtonText("I'm sad :(");
        }
    }
}

Controller.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
public class Controller implements ActionListener
{
	// Local variables
	private View	view;
	private Model	model;
 
    public Controller(View view, Model model)
    {
    	super();
    	this.view	= view;
    	this.model	= model;
    }
 
	@Override
	public void actionPerformed(ActionEvent action)
	{
		if( action.getActionCommand().equals("fromButton") &&
		    !model.getButtonState() )
				model.setButtonState(true);
		else
				model.setButtonState(false);
	}
}

Model.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Observable;
 
public class Model extends Observable
{
	// Private variable(s)
	boolean buttonState;
 
	// Default constructor
    public Model()
    {
    	super();
    	this.buttonState = false;
    }
 
    // Setter for the button state
    public void setButtonState(boolean state)
    {
    	this.buttonState = state;
 
        this.setChanged();
        this.notifyObservers("done");
    }
 
    // Getter for the actual button state
    public boolean getButtonState()
    {
    	return this.buttonState;
    }
}