930 likes | 949 Views
This lecture discusses the concept of patterns and how they can be used to solve recurring problems in software design. It covers different types of patterns and their application in various contexts.
E N D
WARNING • These slides are not optimized for printing or exam preparation. These are for lecture delivery only. • These slides are made for PowerPoint 2010. They may not show up well on other PowerPoint versions. You can download PowerPoint 2010 viewer from here. • These slides contain a lot of animations. For optimal results, watch in slideshow mode.
Explain this design in less than 10 words, without diagrams or code Command cmd = createCommand(commandString); cmd.execute(); history.add(cmd); … cmd.undo(); Add {abstract} Command execute() undo() * execute() {abstract} undo() {abstract} Delete History execute() undo() add(Command) Edit execute() undo()
Book copies in a library TV drama episodes Video copies in a rental shop
Book copies in a library TV drama episodes Video copies in a rental shop
Book copies in a library TV drama episodes Video copies in a rental shop
• Book copies in a library • TV drama episodes • Video copies in a rental shop Recurring tasks in a schedule
Déjà vu: Using patterns to solve recurring problems CS2103/T, Lecture 9, Part 2, [March, 2017]
Déjà vu: Using patterns to solve recurring problems
experience Déjà vu: Usingpatterns to solve recurring problems
experience … is what you get when you didn’t get what you wanted.
experience … is valuable.
Learning from experience … is too costly.
Learning from experience Note to self: never volunteer to be the headstand guy
Learning from experience Patterns
Context:Multiple stock items of same TV model • Problem: Some data (but not all) shared among stock items of the same model. • Solution: Represent TV model andTV stock item as different classes.
Context:Multiple stock items of same TV model • Problem: Some data (but not all) shared among stock items of the same model. • Solution: Represent TV model andTV stock item as different classes. • Stock items • Book copies in a library • TV drama episodes • …
Context: Multiple stock items of same TV model • Problem: Some data (but not all) shared among stock items of the same model. • Solution: Represent TV model andTV stock item as different classes.
Context: Multiple occurrences of some abstraction • Problem: Some data (but not all) shared among occurrences of the same abstraction. • Solution: Represent abstraction and occurrences as different classes.
Context: Multiple occurrences of some abstraction • Problem: Some data (but not all) shared among occurrences of the same abstraction. • Solution: Represent abstraction and occurrences as different classes. * <<Abstraction>> <<Occurrence>>
Context: Multiple occurrences of some abstraction • Problem: Same data (but not all) shared among occurrences of the same abstraction. • Solution: Represent abstraction and occurrences as different classes. * <<Abstraction>> <<Occurrence>>
TVModel BookTitle Lesson * * * TVStockItem BookCopy LessonDelivery * <<Abstraction>> <<Occurrence>>
TVModel BookTitle Lesson * * * TVStockItem BookCopy LessonDelivery * <<Abstraction>> <<Occurrence>>
Name: Abstraction Occurrence Pattern • Context: Multiple occurrences of some abstraction • Problem: Same data (but not all) shared among occurrences of the same abstraction. • Solution: Represent abstraction and occurrences as different classes. * <<Abstraction>> <<Occurrence>>
Name: Abstraction Occurrence Pattern • Context: Multiple occurrences of some abstraction • Problem: Same data (but not all) shared among occurrences of the same abstraction. • Solution: Represent abstraction and occurrences as different classes. * <<Abstraction>> <<Occurrence>> Pattern [An elegant solution to a recurring problem]
Name: Abstraction Occurrence Pattern • Context: Multiple occurrences of some abstraction • Problem: Same data (but not all) shared among occurrences of the same abstraction. • Solution: Represent abstraction and occurrences as different classes. * <<Abstraction>> <<Occurrence>> Pattern [An elegant solution to a recurring problem]
Name: Abstraction OccurrencePattern • Context: Multiple occurrences of some abstraction • Problem: Same data (but not all) shared among occurrences of the same abstraction. • Solution: Represent abstraction and occurrences as different classes. * <<Abstraction>> <<Occurrence>>
… for that, I used the Name: Abstraction OccurrencePattern • Context: Multiple occurrences of some abstraction • Problem: Same data (but not all) shared among occurrences of the same abstraction. • Solution: Represent abstraction and occurrences as different classes. Show off ! * * <<Abstraction>> <<Abstraction>> <<Occurrence>> <<Occurrence>>
Context: Multiple occurrences of some abstraction • Problem: Same data (but not all) shared among occurrences of the same abstraction.
Context: Multiple occurrences of some abstraction • Problem: Same data (but not all) shared among occurrences of the same abstraction. • Solution: Represent abstraction and occurrences as different classes. Anti-Pattern [A stupid solution to a recurring problem]
Context: Multiple occurrences of some abstraction • Problem: Same data (but not all) shared among occurrences of the same abstraction. • Solution: Represent abstraction and occurrences as different classes. Anti-Pattern [A stupid solution to a recurring problem]
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer
1. Abstraction occurrence 2. Singleton 3. Façade 4. Command 5. MVC 6.Observer
1. Abstraction occurrence 2. Singleton3. Façade 4. Command 5. MVC 6.Observer Context: one-and-only-one object, shared among others
1. Abstraction occurrence 2. Singleton3. Façade 4. Command 5. MVC 6.Observer Context: one-and-only-one object, shared among others Problem: how to avoid multiple objects?