# Design Patterns for Mobile Games Based on Structural Similarity
## 4 Design Patterns Based on Structural Similarity
### Related Levels  
This design pattern may be used for games having related but not identical or similar levels. 1. Intent: This pattern lets classes with different features extend their functionality from Super Class and lets classes with minor differences extend level classes. 2. Motivation: In some games, the levels are just slight variants of preceding levels. They usually add very little or no new functionality and vary only in the values of some parameters. In most cases, the levels can be classified based on similarities in levels. 3. Implementation: Classes for differing levels will extend GameLevel, but classes for slightly differing levels will extend an appropriate similar level rather than extending GameLevel directly. For example, a game has three different levels \"Level One\", \"Level Two\", and \"Level Three\". Another level, \"Level Four\", is similar to \"Level Three\", except that it is more difficult because of the increased number of obstacles (or includes another type of obstacle). In this case, classes for Levels 1-3 will be implemented in respective classes directly extending GameLevel, but classes for Level 4 will extend the Level 3 class. A level can be extended by some other levels. Most of the class members will have protected access modifiers to ensure that subclasses can access them. There can be any number of level classes at any level of inheritance (from GameLevel). AbstractUpdateable has already been described under Common pattern. Any individual design pattern has to be substituted to a Common (top level) pattern through this interface to build a complete pattern. (4) Diagram: Figures 12-14 present class-diagrams of different variants of this design pattern. Figure 12: Related Levels pattern with default approach. Figure 13: First alternative approach for implementing _Related Levels_ pattern. * GameLevel is the abstract superclass of all the level classes. This is similar to the one discussed under Unrelated Levels pattern. * Level_i, Level_j, Level_k, Level_x, Level_y, Level_z are classes for arbitrary levels. * Sample Code: The Appendix A shows the default approach of Related Levels pattern that is a trade-off between two alternative approaches just discussed above. The GameLevel class may not be much different from the one mentioned in the last design pattern. For this reason, we skip this class. The sample code is for individual-level classes. It is clear from the sample code that no variables are declared to store enemy-related properties in LevelOne. Level three introduces enemies; therefore, LevelThree class declares variables to store enemy related information. Suppose, LevelFour does not introduce any new features, but includes all those that are present in LevelThree. For example, it just increases the number of stars and the number of enemies. LevelFour extends LevelThree in this case. Even LevelFour can add its functionality as the design does not restrict it from doing that. * Consequences: We can use any one of two approaches to refine the design. In both approaches, similar or related levels are categorized into groups. * In the first approach, a separate concrete class can be used for each group instead of using separate classes for individual levels. A level will be nothing more than an instance of the class for the respective group. The customization of levels will be done by parameters to a constructor and/or through setters. This refinement is shown in Figure 13.
* In the second approach, a separate abstract class can be used for each group. Then, each level class belonging to the group will extend that abstract class. Not to mention, each group's abstract class will extend GameLevel. This approach will make each level more independent--a plus point, but this will increase the number of classes. Figure 14 shows this approach. As we mentioned under \"Second Category--Related Levels\" in the previous section, there are two cases which can place a game in this group.