Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal (17040771) (17040037) (17040842)"— Presentation transcript:

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

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 or Behavioral purpose Scope – Specifies whether the pattern applies primarily to the Classes or to the Objects Purpose CreationalStructuralBehavioral ScopeClassFactory MethodAdapter MethodInterpreter Template Method ObjectAbstract Factory Builder Prototype Singleton Adapter Bridge Composite Decorator Façade Flyweight Proxy Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor

4 Creational Pattern Abstract the instantiation process Abstract the instantiation process Separate the system from object creation Separate the system from object creation Encapsulate information about concrete classes in the system Encapsulate information about concrete classes in the system Hide how instances are created 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. 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 patterns describe how classes and objects can be combined to form larger structures. Structural class patterns use inheritance to compose Interfaces or Implementations. Structural class patterns use inheritance to compose Interfaces or Implementations. Structural object patterns define ways to compose objects to obtain new functionality. 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. 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 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 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. 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. 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 Calling the setWeapon() Sample Code for getting new weapon public abstract class Character { private WeaponBehaviour weapon; private WalkBehaviour walk; public void fight() { weapon.useWeapon(); // delegation of fight behaviour } public void setWeapon(WeaponBehaviour w){ weapon = w; }... abstract void display(); } public class Mona extends Character { public Mona() { weapon = new Sniper(); // The character Mona gets the Sniper weapon …} public void display() {...}}

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 They capture expertise and make it accessible to non- experts Reusing design knowledge Reusing design knowledge They form a vocabulary that helps developers communicate better 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 They help people understand a systems more quickly when it is documented with the patterns it uses Focus is on developing flexible, maintainable programs Focus is on developing flexible, maintainable programs Using design patterns in the early life of software system design prevents later refactorings 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 ?


Download ppt "Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal (17040771) (17040037) (17040842)"

Similar presentations


Ads by Google