在Java中使用Factory Pattern

时间:2013-03-04 13:24:23

标签: java swing design-patterns factory-pattern

我正在研究工厂模式,我怀疑哪种方法可以用来构建它。 我开发了两个完全相同的progama,唯一的区别是实现相关模式的代码。比较后可以帮我找到:

  1. 代码哪一个更适合可重用性,JVM,性能等等,数字1或2?
  2. 是一种更好的方式吗?
  3. 共享课程

    艺术家

        public class ArtistFrame extends AbstractFactoryJInternalFrame {
    
    public ArtistFrame(String title, int x, int y) {
        super(title, x, y);
    }
    
    @Override
    void makeButtons() {
    
        JButton addBtn = new JButton("Add");
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //to do
            }
        });
    
        JButton saveBtn = new JButton("Save");
        saveBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //to do
            }
        });
    
        JButton deleteBtn = new JButton("Delete");
        deleteBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //to do
            }
        });
    
        JButton cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //to do
            }
        });
    
        add(addBtn, BorderLayout.SOUTH);
        add(saveBtn, BorderLayout.NORTH);
        add(deleteBtn, BorderLayout.EAST);
        add(cancelBtn, BorderLayout.WEST);
    
    }
    }
    

    轨道

    public class TrackFrame extends AbstractFactoryJInternalFrame {
    
        public TrackFrame(String title, int x, int y) {
            super(title, x, y);
    
        }
    
        @Override
        void makeButtons() {
    
            JButton addBtn = new JButton("Add");
            addBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    //to do
                }
            });
    
            JButton saveBtn = new JButton("Save");
            saveBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    //to do
                }
            });
    
            JButton deleteBtn = new JButton("Delete");
            deleteBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    //to do
                }
            });
    
            JButton cancelBtn = new JButton("Cancel");
            cancelBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    //to do
                }
            });
    
            add(addBtn, BorderLayout.NORTH);
            add(saveBtn, BorderLayout.CENTER);
            add(deleteBtn, BorderLayout.EAST);
            add(cancelBtn,BorderLayout.WEST);
    
        }
    }
    

    相册

    public class AlbumFrame extends AbstractFactoryJInternalFrame {
    
        public AlbumFrame(String title, int x, int y) {
            super(title, x, y);
    
        }
    
        @Override
        void makeButtons() {
    
            JButton addBtn = new JButton("Add");
            addBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    //to do
                }
            });
    
            JButton saveBtn = new JButton("Save");
            saveBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    //to do
                }
            });
    
            JButton deleteBtn = new JButton("Delete");
            deleteBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    //to do
                }
            });
    
            JButton cancelBtn = new JButton("Cancel");
            cancelBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    //to do
                }
            });
    
            add(addBtn, BorderLayout.EAST);
            add(saveBtn, BorderLayout.WEST);
            add(deleteBtn, BorderLayout.NORTH);
            add(cancelBtn, BorderLayout.SOUTH);
    
        }
    }
    

    案例1
    主类

    public class TestSwing extends JFrame {
    
        JDesktopPane desktop;
        AbstractFactoryJInternalFrame artistFrame;
        AbstractFactoryJInternalFrame albumFrame;
        AbstractFactoryJInternalFrame trackFrame;
    
        public TestSwing() {
    
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(500,300);
    
            desktop = new JDesktopPane();
    
            artistFrame = AbstractFactoryJInternalFrame.getFrame("Artist",10,10);
            albumFrame = AbstractFactoryJInternalFrame.getFrame("Album", 20, 20);
            trackFrame = AbstractFactoryJInternalFrame.getFrame("Track", 30,30);
    
            desktop.add(artistFrame);
            desktop.add(albumFrame);
            desktop.add(trackFrame);
    
            setContentPane(desktop);
    
            setVisible(true);
    
        }
    
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    TestSwing ts = new TestSwing();
                }
            });
    
        }
    }
    

    工厂类         公共抽象类AbstractFactoryJInternalFrame扩展了JInternalFrame {

    protected AbstractFactoryJInternalFrame(String title, int x, int y) {
        super(title, true, true, true, true);
        setLocation(y,y);
    
    }
    
    public static AbstractFactoryJInternalFrame getFrame(String title, int x, int y) {
    
        AbstractFactoryJInternalFrame frame = null;
    
        if (title.equals("Artist")) {
            frame = new ArtistFrame(title, x, y);
    
        } else if (title.equals("Album")) {
            frame = new AlbumFrame(title, x, y);
    
        } else {
            frame = new TrackFrame(title, x, y);
        }
    
        frame.makeButtons();
        frame.pack();
        frame.setVisible(true);
    
        return frame;
    
    }
    
    abstract void makeButtons();
    }
    

    案例2
    主类

    public class TestSwing extends JFrame {
    
        JDesktopPane desktop;
    
        AbstractFactoryJInternalFrame artistFrame;
        AbstractFactoryJInternalFrame albumFrame;
        AbstractFactoryJInternalFrame trackFrame;
    
        public TestSwing() {
    
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(500,300);
    
            desktop = new JDesktopPane();
    
            artistFrame = new ArtistFrame("Arist",10,10);
            albumFrame = new AlbumFrame("Album", 20, 20);
            trackFrame = new TrackFrame("Track", 30,30);
    
            desktop.add(artistFrame);
            desktop.add(albumFrame);
            desktop.add(trackFrame);
    
            setContentPane(desktop);
    
            setVisible(true);
        }
    
    
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    TestSwing ts = new TestSwing();
                }
            });
    
        }
    }
    

    工厂类

        public abstract class AbstractFactoryJInternalFrame extends JInternalFrame {
    
    protected AbstractFactoryJInternalFrame(String title, int x, int y) {
        super(title, true, true, true, true);
        setLocation(y,y);
        makeButtons();
        pack();
        setVisible(true);
    
    
    }
    
    abstract void makeButtons();
    }
    

1 个答案:

答案 0 :(得分:0)

事实上,您的案例#2似乎不是“工厂”,因为您使用运算符 new 来创建实例。 另一方面,情况#1似乎是正确的,但我不知道getFrame(..)的作用,因为代码没有发布。

 artistFrame = AbstractFactoryJInternalFrame.getFrame("Artist",10,10);

无论如何,您应该查看此链接Factory Pattern