简体   繁体   中英

Design pattern with one class and 2 subclasses but without using inheritance

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);
    }
}

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM