访问者模式软件架构报告

Homework 4.2-1: Visitor pattern

 

作业要求:Study the following figure 1, figure 2 and figure 3 about the visitor solution of the sport-shoe store problem. Also you can read the source code about the implementation of the problem. Then, 

 

1) Write code for ShoeInfoVisitor to implement all the methods in this class to get descriptions of all the kind of shoes

2) Also in the client class SportsShoesGUI, add necessary methods to get the information about the selected shoes

 访问者模式软件架构报告

 Figure 1. SportShoes hierarchy

 

 访问者模式软件架构报告

Figure 2. visitor hierarchy

 

 

 访问者模式软件架构报告

Figure 3. the visitor pattern solution

 

 

 

 

 

 

 

  1 import java.awt.*;
  2 import java.awt.event.*;
  3 import javax.swing.*;
  4 
  5 public class SportsShoesGUI extends JFrame implements ItemListener{
  6   private JScrollPane dataPane,textPane,checkBoxPane;
  7   private JSplitPane  bigSplitPane,upSplitPane;
  8   private JPanel      checkBoxPanel, btnPanel, choicePanel;
  9   private JButton     submitBtn, exitBtn;
 10   private JTextArea   txtAreaPrice, txtAreaData;
 11   private Dimension   minimumSize;
 12   private JCheckBox sport, walking, nike, mephisto, northFace,
 13                     running, adidas, ponyMexico, salomon,
 14                     skating,globeBlitz, globeAppleyard,dCShoesRover;
 15 
 16   public final int SELECTED = ItemEvent.SELECTED;
 17   public final int DESELECTED = ItemEvent.DESELECTED;
 18   private int[] states;
 19   private float total=0;
 20 
 21   public SportsShoesGUI(){
 22       super("Visitor Pattern Example with JCheckbox");
 23       minimumSize = new Dimension(130, 100);
 24 
 25       states = new int[20];
 26       setUpChoicePanel();
 27       setUpScrollPanes();
 28   }
 29 
 30   private void setUpChoicePanel(){
 31       submitBtn = new JButton("Submit");
 32       submitBtn.addActionListener( new ButtonActionListener());
 33 
 34       exitBtn = new JButton("Exit");
 35       exitBtn.addActionListener( new ButtonActionListener());
 36 
 37       JPanel btnPanel =new JPanel();
 38       btnPanel.add(submitBtn);
 39       btnPanel.add(exitBtn);
 40 
 41       //Create the check boxes.
 42       sport  = new JCheckBox("SportShoes");
 43       walking  = new JCheckBox("Walking");
 44       nike  = new JCheckBox("Nike");
 45       mephisto  = new JCheckBox("Mephisto");
 46       northFace  = new JCheckBox("NorthFace");
 47       running   = new JCheckBox("Running");
 48       adidas  = new JCheckBox("Adidas");
 49       ponyMexico  = new JCheckBox("PonyMexico");
 50       salomon  = new JCheckBox("Salomon");
 51       skating   = new JCheckBox("Skating");
 52       globeBlitz = new JCheckBox(" GlobeBlitz");
 53       globeAppleyard  = new JCheckBox("GlobeAppleyard");
 54       dCShoesRover  = new JCheckBox("DCShoesRover");
 55 
 56       sport.setMnemonic(KeyEvent.VK_C);
 57       walking.setMnemonic(KeyEvent.VK_C);
 58       nike.setMnemonic(KeyEvent.VK_C);
 59       mephisto.setMnemonic(KeyEvent.VK_C);
 60       northFace.setMnemonic(KeyEvent.VK_C);
 61       running.setMnemonic(KeyEvent.VK_C);
 62       adidas.setMnemonic(KeyEvent.VK_C);
 63       ponyMexico.setMnemonic(KeyEvent.VK_C);
 64       salomon.setMnemonic(KeyEvent.VK_C);
 65       skating.setMnemonic(KeyEvent.VK_C);
 66       globeBlitz.setMnemonic(KeyEvent.VK_C);
 67       globeAppleyard.setMnemonic(KeyEvent.VK_C);
 68       dCShoesRover.setMnemonic(KeyEvent.VK_C);
 69 
 70       //Register a listener for the check boxes.
 71       sport.addItemListener(this);
 72       walking.addItemListener(this);
 73       nike.addItemListener(this);
 74       mephisto.addItemListener(this);
 75       northFace.addItemListener(this);
 76       running.addItemListener(this);
 77       adidas.addItemListener(this);
 78       ponyMexico.addItemListener(this);
 79       salomon.addItemListener(this);
 80       skating.addItemListener(this);
 81       globeBlitz.addItemListener(this);
 82       globeAppleyard.addItemListener(this);
 83       dCShoesRover.addItemListener(this);
 84 
 85       //Set up the picture label
 86       checkBoxPanel = new JPanel();
 87 
 88       // Use gridbag layout to place all buttons
 89       GridBagLayout gridbag = new GridBagLayout();
 90       checkBoxPanel.setLayout(gridbag);
 91       GridBagConstraints gbc = new GridBagConstraints();
 92 
 93       checkBoxPanel.add(sport);
 94       checkBoxPanel.add(walking);
 95       checkBoxPanel.add(nike);
 96       checkBoxPanel.add(mephisto);
 97       checkBoxPanel.add(northFace);
 98       checkBoxPanel.add(running);
 99       checkBoxPanel.add(adidas);
100       checkBoxPanel.add(ponyMexico);
101       checkBoxPanel.add(salomon);
102       checkBoxPanel.add(skating);
103       checkBoxPanel.add(globeBlitz);
104       checkBoxPanel.add(globeAppleyard);
105       checkBoxPanel.add(dCShoesRover);
106 
107       gbc.insets.top = 0;
108       gbc.insets.bottom = 0;
109       gbc.insets.left = 0;
110       gbc.insets.right = 0;
111 
112       gbc.anchor = GridBagConstraints.WEST;
113 
114       gbc.gridx = 0;
115       gbc.gridy = 0;
116       gridbag.setConstraints(sport, gbc);
117 
118       gbc.gridx = 1;
119       gbc.gridy = 1;
120       gridbag.setConstraints(walking, gbc);
121       gbc.gridx = 2;
122       gbc.gridy = 2;
123       gridbag.setConstraints(nike, gbc);
124       gbc.gridx = 2;
125       gbc.gridy = 3;
126       gridbag.setConstraints(mephisto, gbc);
127       gbc.gridx = 2;
128       gbc.gridy = 4;
129       gridbag.setConstraints(northFace, gbc);
130       gbc.gridx = 1;
131       gbc.gridy = 5;
132       gridbag.setConstraints(running, gbc);
133       gbc.gridx = 2;
134       gbc.gridy = 6;
135       gridbag.setConstraints(adidas, gbc);
136       gbc.gridx = 2;
137       gbc.gridy = 7;
138       gridbag.setConstraints(ponyMexico, gbc);
139       gbc.gridx = 2;
140       gbc.gridy = 8;
141       gridbag.setConstraints(salomon, gbc);
142       gbc.gridx = 1;
143       gbc.gridy = 9;
144       gridbag.setConstraints(skating, gbc);
145       gbc.gridx = 2;
146       gbc.gridy = 10;
147       gridbag.setConstraints(globeBlitz, gbc);
148       gbc.gridx = 2;
149       gbc.gridy = 11;
150       gridbag.setConstraints(globeAppleyard, gbc);
151       gbc.gridx = 2;
152       gbc.gridy = 12;
153       gridbag.setConstraints(dCShoesRover, gbc);
154 
155       choicePanel = new JPanel();
156       choicePanel.setMinimumSize(new Dimension(500, 300));
157       choicePanel.setLayout(new BorderLayout());
158 
159       choicePanel.add(checkBoxPanel, "Center");
160       choicePanel.add(btnPanel, "South");
161   }
162 
163    // All the scroll panes are set up
164    private void setUpScrollPanes(){
165        txtAreaPrice = new JTextArea(3,10);
166        txtAreaData = new JTextArea(3,10);
167        dataPane = new JScrollPane(txtAreaData);
168        dataPane.setMinimumSize(minimumSize);
169        checkBoxPane = new JScrollPane(choicePanel);
170          checkBoxPane.getViewport().setBackground(Color.green);
171          upSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
172          upSplitPane.setLeftComponent(checkBoxPane);
173          upSplitPane.setRightComponent(dataPane);
174          textPane = new JScrollPane(txtAreaPrice);
175        textPane.setMinimumSize(new Dimension(100, 100));
176          upSplitPane.setDividerLocation(300);
177          upSplitPane.setPreferredSize(new Dimension(500, 200));
178          bigSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upSplitPane, textPane);
179          bigSplitPane.setDividerLocation(350);
180 
181        getContentPane().add(bigSplitPane);
182          setSize(new Dimension(500, 300));
183        setVisible(true);
184     }
185 
186   //==========================================================
187   // ͬѧ����Ҫ��Ȿ���������漰��createObjects(ActionEvent e)
188   // ����������ֻ��Ҫ���2�д��뼴�������ҵ��
189   //==========================================================
190   class ButtonActionListener implements ActionListener{
191     public void actionPerformed(ActionEvent e) {
192         total=0;
193         txtAreaPrice.append("\n============New Order ==============\n");
194         createObjects(e);
195     }
196   }
197 
198    public void itemStateChanged(ItemEvent e){
199         Object source = e.getItemSelectable();
200         int state = e.getStateChange();
201 
202         if (source == sport) {
203             if(state == SELECTED){
204                walking.setSelected(true);
205                running.setSelected(true);
206                skating.setSelected(true);
207             }
208             else if (state == DESELECTED){
209                walking.setSelected(false);
210                running.setSelected(false);
211                skating.setSelected(false);
212             }
213             states[0]=state;
214         }
215         else if (source == walking) {
216             if(state == SELECTED){
217                nike.setSelected(true);
218                mephisto.setSelected(true);
219                northFace.setSelected(true);
220             }
221             else if (state == DESELECTED){
222                nike.setSelected(false);
223                mephisto.setSelected(false);
224                northFace.setSelected(false);
225             }
226             states[1]=state;
227         }
228         else if (source == nike)
229             states[2]=state;
230         else if (source == mephisto)
231             states[3]=state;
232         else if (source == northFace)
233             states[4] = state;
234         else if (source == running) {
235             if (state == SELECTED) {
236                adidas.setSelected(true);
237                ponyMexico.setSelected(true);
238                salomon.setSelected(true);
239             }
240             else if (state == DESELECTED){
241                adidas.setSelected(false);
242                ponyMexico.setSelected(false);
243                salomon.setSelected(false);
244             }
245             states[5]=state;
246         }
247         else if (source == adidas)
248             states[6]=state;
249         else if (source == ponyMexico)
250             states[7]=state;
251         else if (source == salomon)
252             states[8]=state;
253         else if (source == skating) {
254             if(state == SELECTED) {
255                globeBlitz.setSelected(true);
256                globeAppleyard.setSelected(true);
257                dCShoesRover.setSelected(true);
258             }
259             else if (state == DESELECTED){
260                globeBlitz.setSelected(false);
261                globeAppleyard.setSelected(false);
262                dCShoesRover.setSelected(false);
263             }
264             states[9]=state;
265         }
266         else if (source == globeBlitz)
267             states[10]=state;
268         else if (source == globeAppleyard)
269             states[11]=state;
270         else if (source == dCShoesRover)
271             states[12]=state;
272   }
273 
274   private void createObjects(ActionEvent e){
275       PriceVisitor pv = new PriceVisitor();
276       ShoeInfoVisitor sv = new ShoeInfoVisitor();
277       SportShoes shoes = null;
278       int len = states.length;
279 
280       if (e.getActionCommand().equals("Submit")) {
281          for(int m = 0; m < len; m++ ){
282             if ((m==0) && (states[0] == SELECTED)) {
283                  txtAreaPrice.append("/" + "Sport shoes; includes all type \n");
284               }
285               else if ((m==1) && (states[1] == SELECTED)) {
286                  txtAreaPrice.append("/" + "Walking Sheos \n");
287               }
288               else if ((m==2) && (states[2] == SELECTED)) {
289                     txtAreaPrice.append("/" + "Nike");
290                     shoes = new Nike();
291               }
292               else if  ((m==3) && (states[3] == SELECTED)) {
293                txtAreaPrice.append("/" + "Mephisto");
294                  shoes = new Mephisto();
295               }
296               else if ((m==4) && (states[4] == SELECTED)) {
297                  txtAreaPrice.append("/" + "NorthFace");
298                  shoes = new NorthFace();
299               }
300               else if ((m==5) && (states[5] == SELECTED)) {
301                txtAreaPrice.append("/" + "Running Shoes: \n");
302               }
303               else if ((m==6) && (states[6] == SELECTED)) {
304                  txtAreaPrice.append("/" + "Adidas");
305                  shoes = new Adidas();
306               }
307               else if ((m==7) && (states[7] == SELECTED)){
308                  txtAreaPrice.append("/" + "Pony Mexico");
309                  shoes = new PonyMexico();
310             }
311             else if ((m==8) && (states[8] == SELECTED)){
312                txtAreaPrice.append("/" + "Salomon");
313                shoes = new Salomon();
314             }
315             else if ((m==9) && (states[9] == SELECTED)) {
316                 txtAreaPrice.append("/" + "Skating shoes include 3 items: \n");
317             }
318             else if ((m==10) && (states[10] == SELECTED)) {
319                 txtAreaPrice.append("/" + "GlobeBlitz");
320                 shoes = new GlobeBlitz();
321             }
322             else if ((m==11) && (states[11] == SELECTED)){
323                   txtAreaPrice.append("/" + "Globe Appleyard");
324                 shoes = new GlobeAppleyard();
325             }
326             else if ((m==12) && (states[12] == SELECTED)){
327                 txtAreaPrice.append("/" + "DC Shoes Rover ");
328                 shoes = new DCShoesRover();
329             }
330 
331             if(shoes != null){
332                 shoes.accept(pv);
333                 shoes.accept(sv);
334                 txtAreaPrice.append(" Price: " + pv.getShoesPrice()+"\n");
335                 shoes = null;
336             }
337          }  //end for loop
338 
339           total = pv.getPriceTotal();
340           txtAreaPrice.append("\n Total Price: " + total);
341 
342  
343       }
344       else if (e.getActionCommand().equals("Exit")) {
345                   System.exit(1);
346         }
347     }
348 
349   //--------------------------------------------------------------
350   public static void main(String args[]){
351     try {
352         UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
353     }
354     catch (Exception evt) {}
355 
356     SportsShoesGUI frame = new SportsShoesGUI();
357     frame.addWindowListener(new WindowAdapter(){
358       public void windowClosing(WindowEvent e){
359         System.exit(0);
360       }
361     });
362     frame.setSize(500, 600);
363     frame.setVisible(true);
364   }
365 }
1 abstract class SportShoes{
2 
3    public abstract void accept(Visitor vis);
4    public abstract double getPrice();
5    public abstract String getDescription();
6 }
1 public abstract class WalkingSheos extends SportShoes{
2     public abstract double getPrice();
3     public abstract String getDescription();
4        public abstract void accept(Visitor v);
5 }
1 public abstract class RunningShoes extends SportShoes{
2 
3     public abstract double getPrice();
4     public abstract String getDescription();
5     public abstract void accept(Visitor v);
6 }
1 public abstract class SkatingShoes extends SportShoes{
2 
3     public abstract double getPrice();
4     public abstract String getDescription();
5     public abstract void accept(Visitor v);
6 
7 }
 1 public class Nike  extends WalkingSheos{
 2 
 3    public double getPrice(){
 4       return 44.98;
 5    }
 6 
 7    public String getDescription(){
 8           return "Nike, produced in US";
 9    }
10 
11    public void accept(Visitor v){
12       System.out.println("Nike has been visited.");
13       v.visitNike(this);
14    }
15 }
 1 public class Mephisto  extends WalkingSheos{
 2 
 3     public double getPrice(){
 4        return 384.98;
 5     }
 6 
 7     public String getDescription(){
 8        return "Mephisto, produced in US";
 9    }
10 
11     public void accept(Visitor v){
12        System.out.println("Mephisto has been visited.");
13        v.visitMephisto(this);
14     }
15 }
 1 public class NorthFace  extends WalkingSheos{
 2 
 3    public double getPrice(){
 4       return 99.95;
 5    }
 6 
 7    public String getDescription(){
 8       return "NorthFace, produced in US";
 9    }
10 
11    public void accept(Visitor v){
12       System.out.println("NorthFace has been visited.");
13       v.visitNorthFace(this);
14    }
15 }
 1 public class Adidas extends RunningShoes{
 2 
 3    public double getPrice(){
 4       return 93.53;
 5    }
 6 
 7    public String getDescription(){
 8       return "Adidas, made in US.";
 9 
10    }
11 
12    public void accept(Visitor v){
13       System.out.println("Adidas has been visited.");
14       v.visitAdidas(this);
15    }
16 }
 1 public class PonyMexico  extends RunningShoes{
 2 
 3    public double getPrice(){
 4       return 33.64;
 5    }
 6 
 7    public String getDescription(){
 8          return "PonyMexico, produced in US";
 9    }
10 
11    public void accept(Visitor v){
12       System.out.println("PonyMexico has been visited.");
13       v.visitPonyMexico(this);
14    }
15 }
 1 public class Salomon extends RunningShoes{
 2 
 3     public double getPrice(){
 4        return 103.95;
 5     }
 6 
 7     public String getDescription(){
 8        return "Salomon, produced in US";
 9     }
10 
11     public void accept(Visitor v){
12        System.out.println("Salomon has been visited.");
13        v.visitSalomon(this);
14     }
15 }
 1 public class GlobeBlitz extends SkatingShoes{
 2 
 3    public double getPrice(){
 4       return  60.00;
 5    }
 6 
 7    public String getDescription(){
 8          return "GlobeBlitz, produced in US";
 9    }
10 
11    public void accept(Visitor v){
12       System.out.println("GlobeBlitz has been visited.");
13       v.visitGlobeBlitz(this);
14    }
15 }
 1 public class GlobeAppleyard  extends SkatingShoes{
 2 
 3    public double getPrice(){
 4       return 69.99;
 5    }
 6 
 7    public String getDescription(){
 8       return "GlobeAppleyard, produced in US";
 9    }
10 
11    public void accept(Visitor v){
12       System.out.println("GlobeAppleyard has been visited.");
13       v.visitGlobeAppleyard(this);
14    }
15 }
 1 public class DCShoesRover extends SkatingShoes{
 2 
 3    public double getPrice(){
 4       return 99.95;
 5    }
 6 
 7    public String getDescription(){
 8          return "DCShoesRover, produced in US";
 9    }
10 
11    public void accept(Visitor v){
12       System.out.println("DCShoesRover has been visited.");
13       v.visitDCShoesRover(this);
14    }
15 }
 1 public abstract class Visitor
 2 {
 3    public abstract void visitAdidas(Adidas e);
 4    public abstract void visitSalomon(Salomon e);
 5    public abstract void visitPonyMexico(PonyMexico e);
 6    public abstract void visitNorthFace (NorthFace e);
 7    public abstract void visitNike(Nike e);
 8    public abstract void visitMephisto(Mephisto e);
 9    public abstract void visitGlobeBlitz (GlobeBlitz e);
10    public abstract void visitGlobeAppleyard(GlobeAppleyard e);
11    public abstract void visitDCShoesRover(DCShoesRover e);
12 }
 1 public class PriceVisitor extends Visitor{
 2     private float total;
 3     private double shoesPrice;
 4 
 5     public PriceVisitor(){
 6        total = 0;
 7     }
 8 
 9     public void visitAdidas(Adidas e){
10        shoesPrice = e.getPrice();
11        total += shoesPrice;
12     }
13     public void visitSalomon(Salomon e){
14        shoesPrice = e.getPrice();
15        total += shoesPrice;
16     }
17     public void visitPonyMexico(PonyMexico e){
18        shoesPrice = e.getPrice();
19        total += shoesPrice;
20     }
21     public void visitNorthFace (NorthFace e){
22        shoesPrice = e.getPrice();
23        total += shoesPrice;
24     }
25     public void visitNike(Nike e){
26        shoesPrice = e.getPrice();
27        total += shoesPrice;
28     }
29     public void visitMephisto(Mephisto e){
30        shoesPrice = e.getPrice();
31        total += shoesPrice;
32     }
33     public void visitGlobeBlitz (GlobeBlitz e){
34        shoesPrice = e.getPrice();
35        total += shoesPrice;
36     }
37     public void visitGlobeAppleyard(GlobeAppleyard e){
38        shoesPrice = e.getPrice();
39        total += shoesPrice;
40     }
41     public void visitDCShoesRover(DCShoesRover e){
42        shoesPrice = e.getPrice();
43        total += shoesPrice;
44     }
45 
46     public double getShoesPrice(){
47        return shoesPrice;
48     }
49     public float getPriceTotal(){
50            return total;
51     }
52 }
 1 import java.util.Vector;
 2 
 3 public class ShoeInfoVisitor extends Visitor{
 4 
 5    private String[] s = new String[9];
 6 
 7    public String describeShoes() {
 8        String infoStr ="\n";
 9        for(int n=0; n< 9; n++){
10           if(s[n] != null){
11              infoStr = infoStr + s[n]+"\n";
12           }
13        }
14        return infoStr;
15     }
16 
17     public void visitAdidas(Adidas e){
18        // write code here. ��s[0]��
19     }
20     public void visitSalomon(Salomon e){
21         // write code hereֵ
22     }
23     public void visitPonyMexico(PonyMexico e){
24         // write code here
25     }
26     public void visitNorthFace (NorthFace e){
27         // write code here
28     }
29     public void visitNike(Nike e){
30         // write code here
31     }
32     public void visitMephisto(Mephisto e){
33         // write code here
34     }
35     public void visitGlobeBlitz (GlobeBlitz e){
36         // write code here
37     }
38     public void visitGlobeAppleyard(GlobeAppleyard e){
39         // write code here
40     }
41     public void visitDCShoesRover(DCShoesRover e){
42         // write code here����s[8]��ֵ
43     }
44 }

 

Report Part

 

1. Answer the following

Which class have you written code for?

What code have you added to what class to show the shoe information to the screen?

 

 

 

 

 

 

 

 

 

 

 

 

2. Do the following

a) Explain how method accept(Visitor v) works.

b) How a node class is visited?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3. (测试) Typical input and output from running your finished program  

 

上一篇:学习笔记(13)- decaNLP训练WikiSQL


下一篇:采用位运算对状态进行表示