Design patterns

This describes what design patterns can be found in entire project (both this webpage darknessorlight.glaser.cz and the game webpage https://darknessorlight.herokuapp.com).


Top

1 What is design pattern

In software engineering, a software design pattern is some general and reusable solution to some commonly occurring problem within a given context in software design.
It is a description or template for how to solve some problem.
It is not strictly said to obbey the design pattern. The idea of the design pattern is to help the programmer to write and structure the code.

According to my game development and life programming experience, it also improves (or helps to maintain) the architecture of the entire project. more about architecture and on how to write code can be found in my video here

2 Design patterns used

Here is list of design patterns that can be found in my projects. They are listed by the design pattern name.



2.1 Creational patterns
Design pattern Project Where can be found Approved by Spacek Note by Spacek
Builder Game In inheritance chain: Player - Character - GameObject, each class allows additional setup. GameObject, for example, allows to set physics, position, collision. Character allows to set health, equipment...
no
Dependency Injection This website In presenter classes (Base presenter)
no
It would have to implemented manually
Prototype Game Instead of creating an instance of game item, one of each item kind is created and later copied, if needed. This also applies to monsters, abilities (all taken from database)
yes
Singleton Game / client side (javascript) For example MonsterDatabase (representing collection of all monsters, that is filled with data when received from server) is declared static (typescript), which makes sure that the class in the final javascript will be declared once, and above all classes that will need to reffer to it.
yes
Make it imposible to create new instance (make constructor private or whatever)
Lazy initialization Network serializables in shared code For example, in GamePacket, addCharacterDataInfoAboutAbilityUse() creates new instance of CharacterAbilityUseEventPacketData only when it is needed.
That strategy can be found in many other classes that act as network serializable packet.
Eventhough it is not classic getter, as usually shown in examples, it follows the delayed object creation principle
yes


2.2 Structural patterns
Design pattern Project Where can be found Approved by Spacek Note by Spacek
Composite Game Serializable class PlayerSpecificGamePacket which is supposed to contain data sent over network,
contains inside via composition other packets, such as EquipmentPacket.
All of them, however, inherit from SerializableBase, and therefore provide serialization, deserialization, and fuinctions for checking for data types and validity
yes
Decorator Game GameObject allows to add Physics, collision shapes to itself dynamically. All parent segments and bounding boxes are recalculated appropriatelly, if needed
no
The decorating class should be sort of wrapper, but the one that does not change the interface...
Module Game Functions
  • Functions class is singleton
  • It acts as library, where you just call functions and they return values
  • wiki link
yes


2.3 Behavioral patterns
Design pattern Project Where can be found Approved by Spacek Note by Spacek
Visitor Game - client In Circle.ts, collidesLine(l: Line) is called and Line is an argument (it is visitor). On the visitor is being called other function.
yes
Iterator Game (shared code) client and server In containers, such as HashMap, LinkedList or HashSet.
yes
Template method Game Collision class
  • Class Collision is abstract.
  • It has abstract methods collidesRectangle() and collidesCircle()
  • These methods are called from collides() function (that is the template function)
  • wiki link
yes


2.4 Concurrency patterns
Design pattern Project Where can be found Approved by Spacek Note by Spacek
Balking Game Client Considering ItemDatabase class, which is singleton and supposed to hold all item prototypes, calling method to get an item might throw an exception, if no items are loaded, and therefore, no such item exists. Moreover, calling method to use an ability on Monster (inherits from Character) will have no effect, if the character does not have such an ability allowed.
yes
Messaging Game Between the server and client. Data is being exchanged using serialization and packet classes. For example, it is defined, that client must send request for network dictionary resource, and server will send that.
yes