1 / 21

Design Patterns

Design Patterns. By Archana Munnangi S R Kumar Utkarsh Batwal (17040771) (17040037) (17040842). Module Name - Object Oriented Modeling. What are Design Patterns ?.

rae-mccarty
Download Presentation

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 By Archana Munnangi S R Kumar Utkarsh Batwal (17040771) (17040037) (17040842) Module Name - Object Oriented Modeling

  2. What are Design Patterns ? • It addresses a problem that occurs repeatedly in a variety of contexts and suggests the solution to the problem. Pattern Elements • Pattern Name • Problem • Solution • Consequences

  3. Design Pattern Space Purpose – Reflects what a pattern does. Patterns can have either a Creational, Structural orBehavioral purpose Scope– Specifies whether the pattern applies primarily to the Classes or to the Objects

  4. Creational Pattern • Abstract the instantiation process • Separate the system from object creation • Encapsulate information about concrete classes in the system • Hide how instances are created Examples : Factory Method, Abstract Factory, Builder, Prototype, Singleton

  5. Prototype (Creational Pattern) • Intent - Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. • Hides complexities of making new instances from the client • In some cases copying an existing object can be more efficient than creating a new object. • Provides the option for client to generate objects whose type is not known.

  6. Example of a Prototype Pattern - Mitotic Cell Division in animal cells. • The Animal cell corresponds to the Prototype, as it has an “interface” for cloning itself • A specific instance of a cell corresponds to the ConcretePrototype • Prototype pattern in the original cell takes an active role in creating a new instance of itself • Sample code to clone an Animal cell • public class SingleCellOrganism { • public static void main(String[] args) • { • AnimalCell cell = new AnimalCell(); • // create a clone • AnimalCell newAnimalCell = (AnimalCell)cell.split(); • } • }// End of class • Here split() implements the clone() function in the Animal cell

  7. Use of Prototype – • In Complex hierarchies prototype should be considered for creating new object of many types Consequences – • Add/Remove products at runtime • Specifying new object by varying objects • Specifying new object by varying structure • Reduced subclassing Disadvantages – • Making a copy of the objects is sometimes very complicated.

  8. Structural Pattern • Structural patterns describe how classes and objects can be combined to form larger structures. • Structural class patterns use inheritance to compose Interfaces or Implementations. • Structural object patterns define ways to compose objects to obtain new functionality. Examples : Adapter , Bridge, Composite, Decorator, Facade, Flyweight, Proxy

  9. Decorator (Structural Pattern) • Intent – Attach additional responsibilities to an object dynamically. Provide a flexible alternative to subclassing for extending functionality . They are also known as Wrapper. • A set of decorator classes are used to wrap the Concrete (core) components. • Decorators change the properties of the Concrete components by adding new functionality. • Follows the Open-Close design principle

  10. Example of Decorator pattern : Use Inheritance? • Problem with Inheritance !!!! • Solution – • Design Principle – The Classes must be open for extension but closed for modification.

  11. Solution : Sample Code for Cost Implementation of CoffeaArabia with Mocha Total Cost = 59 + 39 = 98 Rupees

  12. Use of Decorator – • Add/Withdraw responsibilities to/from the individual objects dynamically • When extension by subclassing is impractical Consequences – • Flexibility • Offers a “pay as you go” approach • Decorators and its component are not identical • Lots of little objects are created Disadvantages – • May add many classes, makes package hard to understand • Like I/O streams • Creating new objects could be more complex

  13. Behavioral Pattern • Deals with the algorithms and the assignment of responsibilities between the objects • Behavioral class patterns use inheritance to describe algorithms and flow of control between classes. • Behavioral object pattern use the composition of objects to perform some task that no single object can perform alone. Examples : Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Visitor, Interpreter, Template Method

  14. Strategy (Behavioral Pattern) • Intent – Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. • The Strategy pattern is often used as an alternative to inheritance, where we can end up spreading out the way we handle specific task over many class files. • Change the behavior at runtime (by using Composition Has - A relationship)

  15. Example of Strategy pattern in a game : • Favor Composition over Inheritance • HAS-A (Composition) can be better than IS-A (Inheritance) • Allows changing behavior at run time • The Character class will delegate its Weapon Behavior instead of implementing these itself

  16. Uses of a Strategy Pattern – Strategy is used: • To configure a class with one of many behaviors • When we want to change the algorithm that we use at runtime Consequences – • Families of related algorithms • An alternative to subclassing • Strategies eliminate conditional statements • Choice of different implementations Disadvantages – • Strategy pattern can only be used when the variation in behavior is relevant to clients

  17. Patterns learned: • Prototype ~ Creational Pattern • Decorator ~ Structural Pattern • Strategy ~ Behavioral Pattern Advantages of Design Patterns • They capture expertise and make it accessible to non-experts • Reusing design knowledge • They form a vocabulary that helps developers communicate better • They help people understand a systems more quickly when it is documented with the patterns it uses • Focus is on developing flexible, maintainable programs • Using design patterns in the early life of software system design prevents later refactorings

  18. Drawbacks of Patterns • Patterns do not lead to direct code reuse • Individual Patterns are deceptively simple • Composition of different patterns can be very complex • Teams may suffer from pattern overload • Patterns are validated by experience and discussion rather than by automated testing • Integrating patterns into a software development process is a human­intensive activity

  19. CONCLUSION

  20. THANK YOU

  21. ?

More Related