【发布时间】:2014-05-31 18:42:04
【问题描述】:
最后一天,我阅读了很多关于 MVC 模式的教程和博客。现在我非常了解这个概念,但在我看来,每个教程都展示了另一个概念来在 Java 中实现这种模式。因此,我决定编写自己的应用程序,然后向技能较高的程序员寻求建议。 (边做边学是我的事)。
所以我不想对我最初的想法说太多话,但我会告诉你我的代码:
主类:
public class MainClass {enter code here
public static void main(String[] args){
MainController controller = new MainController();
controller.initView();
}
}
控制器类:
public class MainController implements ActionListener{
private ExtractorStatics stat;
private MainView mainview;
private BusinessExtractor bExtractor;
private InfoboxTextPane infobox;
private BufferedImage logoGS;
public MainController(){
stat = new Statics();
model = new Model();
mainview = new MainView();
}
public void initView(){
if(mainview!=null){
mainview.setActionListener(this);
mainview.setItemListener(new ComboBoxItemListener(this));
mainview.setVisible(true);
}
}
@Override
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if(command.equalsIgnoreCase(stat.SCAN_ACTION_COMMAND)){
this.quickScanButtonAction();
}
}
private void quickScanButtonAction(){
infobox = mainview.getInfobox();
ProcessingInformation information = model.quickScan();
InputStream informationStream = information.getInformationStream();
BufferedReader infoReader = new BufferedReader(
new InputStreamReader(informationStream));
String line;
try {
while ((line = infoReader.readLine()) != null) {
infobox.appendLine(line);
}
infoReader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("End reached");
}
else
infobox.appendLine("Bitte Eingabe überprüfen.");
}
public void comboBoxChanged() {
//do some fancy stuff
}
}
class ComboBoxItemListener implements ItemListener{
MainController mc;
public ComboBoxItemListener(MainController mc){
this.mc=mc;
}
@Override
public void itemStateChanged(ItemEvent e) {
mc.comboBoxChanged();
}
}
MainView Class:
public class MainView extends JFrame {
private static final long serialVersionUID = 559229524422932258L;
private JPanel contentPane;
private JTextField txt_stichwort,txt_loc;
private JButton btn_quickscan;
private JTable table;
private JLabel label;
public InfoboxTextPane txtpn_infobox;
private String lineSep;
private final JComboBox<String> combobox;
/**
* Create the frame.
*/
public MainView() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
lineSep=System.lineSeparator();
Statics stats=new Statics();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 620);
contentPane = new JPanel();
contentPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
setContentPane(contentPane);
contentPane.setLayout(null);
txt_subject= new JTextField();
txt_stichwort.setBounds(88, 47, 318, 20);
pan_suche.add(txt_stichwort);
txt_stichwort.setColumns(10);
combobox = new JComboBox<String>();
combobox.setBounds(88, 16, 318, 20);
pan_suche.add(combobox);
combobox.setModel(new DefaultComboBoxModel(new String[] {"Item1", "Item2"}));
txt_loc = new JTextField();
txt_loc.setBounds(88, 79, 318, 20);
pan_suche.add(txt_loc);
txt_loc.setColumns(10);
btn_quickscan = new JButton("Quick Scan");
btn_quickscan.setActionCommand(stats.SCAN_ACTION_COMMAND);
btn_quickscan.setBounds(10, 23, 130, 30);
pan_dos.add(btn_quickscan);
//a few more buttons that have other action commands defined by statics
//more labels and other GUI components
}
public InfoboxTextPane getInfobox(){
return this.txtpn_infobox;
}
public String getSearchSubject(){
return this.txt_stichwort.getText();
}
public String getSearchLocation(){
return this.txt_loc.getText();
}
public String getSearchWebsite(){
return (String)this.combobox.getSelectedItem();
}
public JComboBox<String> getComboBox(){
return this.combobox;
}
public JLabel getLogoLabel(){
return this.label;
}
public void setActionListener(ActionListener al){
try {
btn_quickscan.addActionListener(al);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public void setItemListener(ItemListener il){
this.combobox.addItemListener(il);
}
}
模型类:
public class Model {
private ExecutorService exeService;
public Model(){
exeService =Executors.newCachedThreadPool();
}
public ProcessingInformation quickScan(){
QuickScanRoutine qs = new QuickScanRoutine();
Future<String> result = exeService.submit(qs);
return qs.getProcessingInformation();
}
}
例程类(可调用):
public class QuickScanRoutine implements Callable<String> {
private ProcessingInformation pi;
private BufferedWriter writer;
public QuickScanRoutine(){
pi = new ProcessingInformation();
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis;
writer = new BufferedWriter(new OutputStreamWriter(pos));
try {
pis = new PipedInputStream(pos);
pi.setInformationStream(pis);
} catch (IOException e) {
e.printStackTrace();
}
}
public ProcessingInformation getProcessingInformation(){
return this.pi;
}
@Override
public String call() throws Exception {
//ofcourse this isnt the real purpose of this Class but I want to `demonstrate writing to the stream`
for(int i =0; i<100;i++){
writer.write("Hello");
writer.newLine();
writer.flush();
}
writer.close();
return "Routine has been executed sucessfully!";
}
}
处理信息:
public class ProcessingInformation {
PipedInputStream informationStream,resultDataStream;
public void setInformationStream(PipedInputStream info) {
this.informationStream = info;
}
public void setResultDataStream(PipedInputStream data){
//I will need this stream for further tasks
this.resultDataStream = data;
}
public InputStream getInformationStream(){
if(informationStream!=null){
return informationStream;
}
else
return null;
}
public InputStream getResultDataStream(){
if(informationStream!=null){
return informationStream;
}
else
return null;
}
}
我尝试尽可能缩短代码,但尝试将其保留在上下文中,以便您理解整个结构。 我的主要问题是:
- 这是正确的 MVC 设计吗?
- 这是从模型线程到视图的合适方式吗? (管道流)
这些将是我的主要问题,但我很感激并愿意接受您的所有建议。
【问题讨论】:
-
看看我的Java Swing File Browser 文章。我说明了一种使用 MVC 模式编写 Swing 应用程序的方法。
-
感谢您提供此链接,非常感谢。
标签: java multithreading design-patterns model-view-controller