Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Patterns Jeremy Cronan Alliance Safety Council

Similar presentations


Presentation on theme: "Programming with Patterns Jeremy Cronan Alliance Safety Council"— Presentation transcript:

1 Programming with Patterns Jeremy Cronan Alliance Safety Council jcronan@alliancesafetycouncil.org jcronan@alliancesafetycouncil.org

2 What is a Design Pattern? A general reusable solution to a commonly occurring problem. It is a description or template for how to solve a problem that can be used in many different situations. It is not directions on how to exactly create code

3 Commonly used Patterns Observer Pattern Decorator Pattern Adapter and Facade Patterns Factory Pattern Command Pattern

4 Observer Pattern Pattern that uses Publishers and Subscribers where the subscribers register with the publisher and the publisher pushes all changes to the subscribers. One to many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. Common Everyday example: Cell Phone Notifications

5 News Site Cell 1 Cell 3 Cell 2 Each Cell Registers with News Site

6 News Site Cell 1 Cell 3 Cell 2 News Site sends to all registered Cells

7 News Site Cell 1 Cell 3 Cell 2 Cell 2 unregisters from notifications

8 News Site Cell 1 Cell 3 Cell 2 News Site sends to all registered Cells (not Cell 2 since unregistered)

9 Decorator Pattern Attaching of additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Common Everyday example: Calculating prices for drink coffee drinks with additions.

10

11

12 Adapter, and Facade Patterns Adapter Pattern: converts the interface of an object into another interface the client expects. Façade Pattern: provides a unified interface to a set of interfaces in subsystem.

13 PatternIntent DecoratorCoverts one interface to another AdapterDoesn’t alter the interface but adds responsibility FaçadeMakes an interface simpler

14

15

16 Factory Pattern Interface for creating an object, but lets subclasses decide which class to instantiate. Class defers instantiation to subclasses. Common Everyday example: Exporting different reports from a website

17 Attendance Report Expiration Report Cost Report void ExportToPDF() Each procedure has duplicate code to export the report to PDF after building grid of report

18 Attendance Report Expiration Report Cost Report void ExportToPDF() Each procedure calls a centralized procedure, passing a Factory that knows how to build the report TO CENTRALIZE CODE: void ExportReport(IFactory reqFactory) IFactory AttendanceFactory() IFactory ExpirationFactory() IFactory CostFactory()

19 Command Patterns Encapsulates a request as an object, which lets you parameterize other objects with different requests, queue or log requests, and support undo operations. Common Everyday example: Remote Controls

20

21 Other types of Patterns Singleton Pattern Template Method Pattern State Pattern Proxy Pattern Compound Patterns Bridge Patterns Etc.

22 Questions? Reference: Head First Design Patterns by Eric Freeman and Elisabeth Freeman Jeremy Cronan Alliance Safety Council jcronan@alliancesafetycouncil.org jcronan@alliancesafetycouncil.org


Download ppt "Programming with Patterns Jeremy Cronan Alliance Safety Council"

Similar presentations


Ads by Google