在Java中使用工厂模式 [英] Using Factory Pattern in Java
问题描述
我开发了两个完全相同的程序,唯一的区别是实现有问题的模式的代码。经过比较可以帮助我找到:
- 代码哪一个更好的可重用性,JVM,性能等,1或2 ?
- 是否可以做更多的最佳方式?
共享课程
艺术家
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 =新的JButton(取消);
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 =新的JButton(取消);
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 =新的JButton(取消);
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扩展JFrame {
JDesktopPane桌面;
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(){
@覆盖
public void run(){
TestSwing ts = new TestSwing();
}
});
}
}
工厂类
public抽象类AbstractFactoryJInternalFrame扩展JInternalFrame {
protected AbstractFactoryJInternalFrame(String title,int x,int y){
super ,真,真,真,真)
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);
返回框架;
}
抽象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(){
@覆盖
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);
}
抽象void makeButtons();
}
其实你的案子#由于您使用操作符新建来创建实例,所以2似乎不是工厂。
另一方面,case#1似乎是正确的,但我不知道getFrame(..)的作用是因为没有发布代码。
artistFrame = AbstractFactoryJInternalFrame.getFrame(Artist,10,10);
无论如何,您应该检查此链接工厂模式
I am studying the Factory pattern and I have some doubts about which is the best way to buil it. I developed two progamas that do exactly the same, the only difference is the code that implements the pattern in question. After comparison could help me find:
- Code which one is better for reusability, for the JVM, for performance, etc, number 1 or 2?
- Is a 3.er optimal way to do even more?
Shared Classes
Artist
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);
}
}
Track
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);
}
}
Album
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);
}
}
Case 1
Main Class
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();
}
});
}
}
Factory Class public abstract class AbstractFactoryJInternalFrame extends 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();
}
Case 2
Main Class
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();
}
});
}
}
Factory Class
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();
}
In fact, your case#2 doesn't seem to be a "factory" since you are using the operator new to create your instances. In the other hand, case#1 seems to be correct, but I don't know what getFrame(..) does because the code isn't posted.
artistFrame = AbstractFactoryJInternalFrame.getFrame("Artist",10,10);
Anyway, you should check this link Factory Pattern
这篇关于在Java中使用工厂模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!