Hello! I have a task: to break the text from the book on programming (that is, besides usually the text there are also listings), using "regular", into words, paragraph. signs, sentences, paragraphs, and so on. And it is also necessary to assemble it from these parts in its original form. Well, then make additional changes to the task. A prerequisite is the implementation of the Composite pattern. Here is the snag. However, regular expressions are clear and the pattern itself is also uncomplicated. But how to apply it here. Of course, it is clear that text elements can be both nodes (sentence, paragraph), and leaves (a word (if it is not broken into symbols), paragraph. Sign). But how to design it? Probably, there will be one interface in which there will be necessary functions for adding, deleting, receiving (which will be UnsupportedOperation for leaves), well, and some function for going through the tree (or something like that). I just can't catch up with how the pattern will simplify everything. They said that the classes in the model should decrease significantly.

Without this pattern. I would stupidly in class sentences kept an ArrayList with punctuation and words. In class Paragraph ArrayList with sentences, etc.

Can anyone understand that nonsense that I wrote? Or did anyone come across a similar application of the Composite pattern? = (Thanks in advance.

    1 answer 1

    All entities in your program are very similar, so there is no point in putting them into separate classes. You will only need the 1st base class in order to store each of them.

    Suppose you have a class that stores data (a composite that implements the pattern). Which has a field whose value type is taken from enum . enum describes all possible components: text, paragraph, sentence, word, etc., etc.

    All the necessary logic you will have screwed in one method, which, depending on the type, will break the value into smaller parts.

    For example:

    1. The parse method on input receives an object of the type text. After applying the corresponding pattern, you get a list of elements that already correspond to paragraphs (you add text to the element and call the parse method for them recursively).
    2. as a result, paragraphs are broken down into sentences

    ps the next level of recursion nesting breaks sentences into words + punctuation, etc., etc.

    pss it is best to set regexp for each individual type as a parameter to the constructor enum constant

    The composite class will look something like this:

     class Entity { private EntityType type; private List<EntityType> parts = new LinkedList<EntityType>(); private String text; public Entity(EntityType type, String text) {} // call setType, setText public List<EntityType> getParts() {} // implement public void addPart(Entity e) {} // implement // implement getters and setters here } 
    • Thank you so much for not answering! Got it. True, I don’t really imagine how I implement recursion, but logically everything is very clear. Thanks again! - horcrux
    • one
      > However, I do not really imagine how I implement the recursion, but logically everything is very clear. Thanks again! very simply you call the parse method, and pass it an element that you need to take apart (at the beginning of the text for the element). the pars method with the help of the regular text breaks the text into paragraphs. you create elements and add them to the text element. then inside this method you call it in order to parse the elements that you just added - jmu
    • Yes, I figured it out yesterday. Thank you so much =))). Very good work. - horcrux