6. 4 The player-Role pattern · Context A role is a particular set of properties associated with an object in a particular context an object may play different roles in different contexts · Problen -How do you best model players and roles so that a player can change roles or possess multiple roles www.oseng.com O Lethbridge/Laganiere 2001 Chapter 6: Using design patterns
© Lethbridge/Laganière 2001 Chapter 6: Using design patterns 11 6.4 The Player-Role Pattern • Context: —A role is a particular set of properties associated with an object in a particular context. —An object may play different roles in different contexts. • Problem: —How do you best model players and roles so that a player can change roles or possess multiple roles?
Player-Role orces -It is desirable to improve encapsulation by capturing the information associated with each separate role in a class You want to avoid multiple inheritance You cannot allow an instance to change class Solution P|ayer》 KAbstractroley <Role) kRole2) www.lloseng.com O Lethbridge/Laganiere 2001 Chapter 6: Using design patterns
© Lethbridge/Laganière 2001 Chapter 6: Using design patterns 12 Player-Role • Forces: —It is desirable to improve encapsulation by capturing the information associated with each separate role in a class. —You want to avoid multiple inheritance. —You cannot allow an instance to change class • Solution: «Player» «Role1» «Role2» «AbstractRole»
Player-Role Example 1: Animal 0.2_HabitatRole Aty pe OfFood ha bitat Camivore Herbivore Omnivore AquaticAnimal LandAnima www.oseng.com O Lethbridge/Laganiere 2001 Chapter 6: Using design patterns 13
© Lethbridge/Laganière 2001 Chapter 6: Using design patterns 13 Player-Role Example 1: Animal HabitatRole ty peOfFood habitat Herbivore Omnivore AquaticAnimal LandAnimal 0..2 Carnivore
Player-Role Example 2: AttendanceRole Student Leve role attendance/ Alevel Full Time Student PartTime Student Graduate Student Undergraduate Student www.oseng.com O Lethbridge/Laganiere 2001 Chapter 6: Using design patterns 14
© Lethbridge/Laganière 2001 Chapter 6: Using design patterns 14 Player-Role Example 2: LevelRole attendanc e FullTimeStudent PartTimeStudent GraduateStudent UndergraduateStudent level AttendanceRole Student