为了更好理解MVC一例

为了更好理解MVC,在这里我给出一个简单例子. 共有4个java类,你可以Run AccountFrame.java.


//AccountModel.java
package com.newmodern.mvc;

import java.util.*;

public class AccountModel extends Observable{

	private double dBalance;
    private String AccountID;

	public AccountModel(String AccountID, double dBalance){
		this.AccountID = AccountID;
        this.dBalance = dBalance;
	}

	public void deposit(double dBalance){
		this.dBalance+=dBalance;
		setChanged();
		notifyObservers(this);
	}

	public void substract(double dBalance){
		this.dBalance-=dBalance;
		setChanged();
		notifyObservers(this);
	}

    public double getBalance(){
		return dBalance;
  	}

    public void mynotifyObservers(){
        setChanged();
        notifyObservers(this);
    }

}



//AccountView.java
package com.newmodern.mvc;

import java.util.*;
import java.awt.*;

public class AccountView implements Observer{

	TextField txtBalance;
	AccountModel am;

	public  AccountView (AccountModel am){
        this.am = am;
		am.addObserver(this);
		txtBalance = new TextField(10);
	}

	public void update(Observable o,Object arg){
		txtBalance.setText(String.valueOf(am.getBalance()));
	}

    public TextField getTextField(){
		return txtBalance;
	}

}


package com.newmodern.mvc;

import java.awt.event.*;

public class AccountController implements ActionListener{
	AccountFrame accountFrame;
	public AccountController(AccountFrame af){
		this.accountFrame = af;
	}
    public void actionPerformed(ActionEvent ae)   {
        String sCommand = ae.getActionCommand();
        if (sCommand.equals("deposit")){
            System.out.println("Action: deposit");
            accountFrame.am.deposit(accountFrame.getValue());
        } else if(sCommand.equals("sub")){
            System.out.println("Action: substract");
            accountFrame.am.substract(accountFrame.getValue());
        }
	}
}



package com.newmodern.mvc;
import java.awt.*;
import java.awt.event.*;

public class AccountFrame extends Frame {

	AccountController ac;
	AccountView       av;
	AccountView       av1;
	AccountModel	  am;

	TextField txtValue;
	Button    btnDeposit;
	Button    btnSub;
	public AccountFrame (String sTitle)
        {
		super(sTitle);

		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we){
				dispose();
				System.exit(0);
			}
		});
		ac = new AccountController(this);
		am = new AccountModel(sTitle,0);
		av = new AccountView(am);

		av1 = new AccountView(am);

		setLayout(new GridLayout(4,2));
		txtValue = new TextField(10);
		btnDeposit = new Button("deposit");
		btnSub     = new Button("sub");
		add(new Label("value:"));
		add(txtValue);
		add(new Label("balance:"));
		add(av.getTextField());
		add(new Label("balance1:"));
		add(av1.getTextField());

		add(btnDeposit);
		add(btnSub);
		btnDeposit.addActionListener(ac);
		btnSub.addActionListener(ac);
		//setSize(200,200);
		pack();
		show();
                am.mynotifyObservers();
	}
        public double getValue() throws NumberFormatException {

		double value = Double.parseDouble(txtValue.getText());
		return value;
	}


	public static void main(String args[])
        {
		String id = "100";
                new AccountFrame(id);
	}

}
<淘宝热门商品:
 

27.50 元  

兼济天下 公司运营 多家实体店 毛巾 浴巾 美容巾 浴袍 毛巾被

【皇冠信誉 专业做毛巾】外贸原单★素色绣小圆点浴巾◎加厚

 

¥:21.0 

【华佗天然居】天然花草茶花茶中草药中药材

花草茶之瘦腿茶:迷迭香+柠檬草+马鞭草30包21元包快递美容减肥茶


来源:程序员网

小小豆叮

0 Responses to "为了更好理解MVC一例"

发表评论