1 / 10

Lecture 31: Composite Pattern

Computer Science 313 – Advanced Programming Topics. Lecture 31: Composite Pattern. Pimp my File Manager. Writing a Java-based file manager Starts at drive, but drill-down into directories Print out the names of directories & files Directory structure shown during print out

upton
Download Presentation

Lecture 31: Composite Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 313 – Advanced Programming Topics Lecture 31:Composite Pattern

  2. Pimp my File Manager • Writing a Java-based file manager • Starts at drive, but drill-down into directories • Print out the names of directories & files • Directory structure shown during print out • Allow user to create & delete files • Print out files sizes as we go

  3. … Ain’t Nothing But Files & Dirs • Ideas to consider in your design • You’re very lazy (minimize code to write) • Allow future growth for new file system concepts • Take 5 minutes to draw class diagram • Discuss strengths & weaknesses of your design • Make sure you include where methods declared

  4. Pimp my File Manager • Writing a Java-based file manager • Starts at drive, but drill-down into directories • Print out the names of directories & files • Directory structure shown during print out • Allow user to create & delete files • Print out files sizes as we go

  5. Part-Whole Hierarchy

  6. Part-Whole Hierarchy • Frequently found relationship in real-world: Files– DirectoryMenu Items –MenuJComponents–JContainerLines & Figures – Pictures • Instance consists of compositions of single type • Uses component & composite types • Many objects means decorator inappropriate • Invisibility of design is a good idea, however

  7. Single Responsibility Principle • Classes should have only one reason to change • Cause errors when writing each responsiblity • Each change increases odds of introducing bug • Fixing a bug counts as a change • Ideal classes & methods have 1 purpose • Simple, straight-line code created from ideal cases • Easy to understand code with no side effects • Natural desire to create highly coupled code limited • Design work goal is to enforce SRP

  8. Very Common Mistake • Part-whole hierarchy solution violating SRP • Single class for both files & directories • Make complex Client handling 2 classes • Separate add()for Container & Component • Make adding new Components difficult

  9. Problem With Simple Solution • Use Composite Pattern to solve situation • Tree-like structure composes part-whole hierarchy • Can be invisible to client if they do not care • Clients that care can use types to see differences

  10. For Next Class • Lab available on the web • Try to plan and not wait until Thursday • Due on Friday (e.g., 1 lab period for assignment) • Gets everything set up for look at Map-Reduce algorithm • Read pages 360-380 on Composite Pattern • Will discuss implementation on Friday

More Related