Skip to content

JaeNuguid/Java-Design-Patterns

Repository files navigation

Java Design Patterns

A repository containing basic examples of different java design patterns

Contents

Strategy Design Pattern


Strategy Design Pattern allows the algorithm to vary independently from clients that use it. It helps on designing a flexible and reusable object-oriented software.
The ability to store and retrieve a reference to some code in a data structure is the fundamental prerequisite in order to implement Strategy Design Pattern. This is achievable by mechanisms as the native function pointer, the first-class function, classes or class instances in object-oriented programming languages, or accessing the language implementation's internal storage of code through reflection.

Observer Design Pattern


The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
In the example that we have, LottoGame is the subject, while Players are the dependents. When LottoGame draws/rolls, it notifies all the Players, and then the Players fetch the result from LottoGame.
Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically. Observer pattern falls under behavioral pattern category.




Disclaimer

All information stated above including the source codes and uml diagrams is provided in good faith for guidance and reference purposes only. It is of a general informational and educational nature, and the owner of this repository takes no legal responsibility for the accuracy of the information provided via this page.