1 / 20

Design Patterns

Design Patterns. A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms Example: Abstract Factory Structural: Simplify design by identifying a simple way to realize relationships between entities. Example: Bridge

december
Télécharger la présentation

Design Patterns

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. Design Patterns A General reusable solution to a commonly occurring problem in software design. • Creational: Deal with object creation mechanisms • Example: Abstract Factory • Structural: Simplify design by identifying a simple way to realize relationships between entities. • Example: Bridge • Behavioral: Identify common communication patterns between objects and realize these patterns. • Example: Strategy

  2. Abstract Factory

  3. Abstract Factory (Creational) • Applying context: A client or application may want to use different types of objects with common behavior and easily change between those types before or during execution. • Problem: Changing types of objects through the application is difficult because instantiation requires to know the type of object to be declared. Also different types might have different methods to use their functionality.

  4. Abstract Factory (Creational) • Solution: Isolate the client code from object creation by having client just ask for (and handle) a factory object. This factory object is an abstract data type which can only be used through an interface. • Discussion: By having the client dependant just on the interface of the Abstract Type Product provided by the Abstract Factory. Each Concrete Product must be able to work under the interface that the Abstract Productprovides. The client is able to change the Concrete Type Productby changing the Concrete Factorythat is being used. This change is usually done by changing one line in one file.

  5. Factory Classes

  6. Product Classes

  7. Client

  8. Bridge Design Pattern

  9. Problem • The use of subclasses to provide additional implementation has caused the “hardening of the software arteries.” • This limits the ability to independently extend the abstraction and the implementation.

  10. UML Class Diagram

  11. Purpose • To “decouple abstraction from its implementation.” • To “publish interface in an inheritance hierarchy, and bury implementation within it’s own inheritance hierarchy.” http://sourcemaking.com/design_patterns/bridge

  12. http://java.dzone.com/articles/design-patterns-bridge

  13. Disadvantages • Although it provides more flexibility, it increases the complexity of the software. • Might have performance issues due to longer communication path between the abstraction and implementation. http://java.dzone.com/articles/design-patterns-bridge

  14. Strategy Context: Your system has lots of different behaviors that can change in runtime and not easily manageable.

  15. “Strategy”’s Strategy • Separate behavior from implementation (encapsulate behavior). http://images1.wikia.nocookie.net/__cb20090908183029/finalfantasy/images/f/ff/Ff13-paradigm.jpg http://www.netobjectives.com/PatternRepository/index.php?title=TheStrategyPattern

  16. http://www.lepus.org.uk/ref/companion/Strategy.xml http://en.wikipedia.org/wiki/File:Strategy.JPG

  17. classStrategyExample{ publicstaticvoid main(String[]args){ Contextcontext; context =newContext(newConcreteStrategyAdd()); intresultA=context.executeStrategy(3,4); context =newContext(newConcreteStrategySubtract()); intresultB=context.executeStrategy(3,4); context =newContext(newConcreteStrategyMultiply()); intresultC=context.executeStrategy(3,4);} } interface Strategy {int execute(int a, int b);} classConcreteStrategyAddimplements Strategy { publicint execute(int a, int b){ System.out.println("Called ConcreteStrategyAdd's execute()"); return a + b;} } classConcreteStrategySubtractimplements Strategy { publicint execute(int a, int b){ System.out.println("Called ConcreteStrategySubtract's execute()"); return a - b;} } classConcreteStrategyMultiplyimplements Strategy { publicint execute(int a, int b){ System.out.println("Called ConcreteStrategyMultiply's execute()");return a * b;} } classContext{ private Strategy strategy; publicContext(Strategy strategy){this.strategy= strategy;} publicintexecuteStrategy(int a, int b){ returnstrategy.execute(a, b);} } http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Strategy#Java

  18. Strategy v Bridge http://en.wikipedia.org/wiki/Strategy_pattern#Strategy_versus_Bridge

More Related