Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

New design patterns

155 views

Published on

  • Be the first to comment

  • Be the first to like this

New design patterns

  1. 1. Strategy Pattern•Design re-usable code without using inheritance•Different behaviors required for the same method (e.g. weapon behavior forcharacters in a game)Problem
  2. 2. Strategy Pattern•Design re-usable code without using inheritance•Different behaviors required for the same method (e.g. weapon behavior forcharacters in a game)Problem•Encapsulate data and behaviors in classes•Identify behaviors and create interfaces for each•Create behavior classes that implement each behavior interfaceSolution
  3. 3. Strategy Pattern•Design re-usable code without using inheritance•Different behaviors required for the same method (e.g. weapon behavior forcharacters in a game)Problem•Encapsulate data and behaviors in classes•Identify behaviors and create interfaces for each•Create behavior classes that implement each behavior interfaceSolution•Loosely coupling objects via interfaces is more independent and easier to modify•Encapsulation hides internal behavior of objects (can change behavior withoutimpacting how it is usedConsequences (benefits)
  4. 4. Strategy Pattern•Design re-usable code without using inheritance•Different behaviors required for the same method (e.g. weapon behavior forcharacters in a game)Problem•Encapsulate data and behaviors in classes•Identify behaviors and create interfaces for each•Create behavior classes that implement each behavior interfaceSolution•Loosely coupling objects via interfaces is more independent and easier to modify•Encapsulation hides internal behavior of objects (can change behavior withoutimpacting how it is usedConsequences (benefits)•Better understanding of the power of object oriented software design•More capable of designing manageable/maintainable codeKey Learnings
  5. 5. Strategy Pattern ExampleCharacterAbstract Classfight()useWeapon()setDamage()eat()Kingfight()useWeapon()setWeaponBehavior()setFightBehavior()setEatBehavior()setDamage()eat()setWeaponBehavior()setFightBehavior()setEatBehavior()FootSoldierfight()useWeapon()setDamage()eat()setWeaponBehavior()setFightBehavior()setEatBehavior()Knightfight()useWeapon()setDamage()eat()setWeaponBehavior()setFightBehavior()setEatBehavior()<<Interface>>FightBehaviorfight()WeaponBehaviorAbstract Classint damageuseWeapon()setDamage()EatBehaviorAbstract Classint damagefloat poundsFoodString utensilString containerString foodTypeeat()AggresiveFightfight()PassiveFightfight()

×