100 likes | 251 Views
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
E N D
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 • Allow user to create & delete files • Print out files sizes as we go
… 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
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
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
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
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
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
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