英文:
Design pattern with one class and 2 subclasses but without using inheritance
问题
// 主应用程序
public class App {
public static void main(String[] args) {
MainPanel panel = new MainPanel(20);
SubPanelPerson personSubPanel = new SubPanelPerson();
SubPanelMember memberSubPanel = new SubPanelMember();
personSubPanel.changeValue(panel); // 在第一个子类中改变值
memberSubPanel.findMember(panel); // 在第二个子类中查找值
}
}
// 主面板
public class MainPanel {
int id;
public MainPanel(int id){
this.id = id;
}
}
// 人员子面板
public class SubPanelPerson {
public void changeValue(MainPanel mainPanel) {
mainPanel.id = 30;
}
}
// 成员子面板
public class SubPanelMember {
public void findMember(MainPanel mainPanel) {
int memberId = mainPanel.id;
// 使用 memberId 在数据库中查找
}
}
英文:
I need implements a class with 2 subclass, the main class contain variable id, the mainclass sends variable to 2 subclass, In first subclass it can change value of variable and the mainClass needs to know. The second subclass looks for the value id in database changed for first subclass. I can´t use inheritance because later i will use other second main class and i will need to use the two subclass.
I need to use a design pattern I don't know whick is.
Here my code example.
//Main app
public class App {
public static void main(String[] args) {
MainPanel panel = MainPanel(20);
}
}
//Main panel
public class MainPanel {
int id;
public MainPanel(int id){
this.id = id;
}
}
//Subpanel Person
public class SubPanelPerson {
int id;
public SubPanelPerson() { }
public void changeValue() {
this.id = 30;
}
}
//Subpanel member
public class SubPanelmember {
int id;
public SubPanelMember() {}
public void findMember() {
find(id);
}
}
答案1
得分: 0
我认为这个解决方案,我现在只使用一个面板,但以后会需要使用2个面板。我编写了一个有2个面板和2个子面板的解决方案。
[![在这里输入图片描述][1]][1]
[1]: https://i.stack.imgur.com/myueA.jpg
class PanelBooks {
int id;
SubPanelPerson subPanelPerson = new SubPanelPerson(this);
SubPanelMember subPanelMember = new SubPanelMember(this);
public void setId(int id) { this.id = id; }
}
class PanelVideos {
int id;
SubPanelPerson subPanelPerson = new SubPanelPerson(this);
SubPanelMember subPanelMember = new SubPanelMember(this);
public void setId(int id) { this.id = id; }
}
class SubPanelPerson {
private PanelBooks panelBooks;
private PanelVideos panelVideos;
public SubPanelPerson(PanelBooks panelBooks) {
this.panelBooks = panelBooks;
}
public SubPanelPerson(PanelVideos panelVideos) {
this.panelVideos = panelVideos;
}
public void changeValueBooks(int id) { this.panelBooks.setId(id); }
public void changeValueVideos(int id) { this.panelVideos.setId(id); }
}
class SubPanelMember {
private PanelBooks panelBooks;
private PanelVideos panelVideos;
public SubPanelMember(PanelBooks panelBooks) {
this.panelBooks = panelBooks;
}
public SubPanelMember(PanelVideos panelVideos) {
this.panelVideos = panelVideos;
}
public void findMemberBooks() { System.out.println("查找 ID:" + this.panelBooks.id); }
public void findMemberVideos() { System.out.println("查找 ID:" + this.panelVideos.id); }
}
public static void main(String args[]) {
PanelBooks panelBooks = new PanelBooks();
PanelVideos panelVideos = new PanelVideos();
}
请问这是否是一个不错的选择,还是有待改进?
英文:
I think this solution, I am Using only 1 Panel now but I will need to use 2 panel later. I write the solution whit 2 panels and 2 subPanels.
class PanelBooks {
int id;
SubPanelPerson subPanelPerson = new SubPanelPerson(this);
SubPanelMember subPanelMember = new SubPanelMember(this);
public void setId(int id) { this.id = id; }
}
class PanelVideos {
int id;
SubPanelPerson subPanelPerson = new SubPanelPerson(this);
SubPanelMember subPanelMember = new SubPanelMember(this);
public void setId(int id) { this.id = id; }
}
class SubPanelPerson {
private PanelBooks panelBooks;
private PanelVideos panelVideos;
public SubPanelPerson(PanelBooks panelBooks) {
this.panelBooks = panelBooks;
}
public SubPanelPerson(PanelVideos panelVideos) {
this.panelVideos = panelVideos;
}
public void changeValueBooks(int id) { this.panelBooks.setId(id); }
public void changeValueVideos(int id) { this.panelVideos.setId(id); }
}
class SubPanelMember {
private PanelBooks panelBooks;
private PanelVideos panelVideos;
public SubPanelMember(PanelBooks panelBooks) {
this.panelBooks = panelBooks;
}
public SubPanelMember(PanelVideos panelVideos) {
this.panelVideos = panelVideos;
}
public void findMemberBooks() { System.out.println("Find ID: " + this.panelBooks.id); }
public void findMemberVideos() { System.out.println("Find ID: " + this.panelVideos.id); }
}
public static void main(String args[]) {
PanelBooks panelBooks = new PanelBooks();
PanelVideos panelVideos = new PanelVideos();
}
Is It a good option or can It be improved?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论