私はファクトリパターンを研究していますが、どちらがそれを構築するための最良の方法であるかについていくつか疑問があります。私はまったく同じことをする2つのプログラムを開発しました。唯一の違いは、問題のパターンを実装するコードです。比較した後、私は見つけるのに役立つかもしれません:
- 再利用性、JVM、パフォーマンスなどのどちらが優れているか、1番と2番のどちらをコーディングしますか?
- 3.erはさらに多くのことを行うのに最適な方法ですか?
共有クラス
アーティスト
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();
}