-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from ES2-UFPI/eryckkawa#64
add observer design pattern in this project #64
- Loading branch information
Showing
8 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
public interface IObserver | ||
{ | ||
void OnNotify(object sender, object eventData); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public interface ISubject | ||
{ | ||
void RegisterObserver(IObserver observer); | ||
void UnregisterObserver(IObserver observer); | ||
void NotifyObservers(object eventData); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Diagnostics; | ||
using UnityEngine; | ||
|
||
public class Observer : MonoBehaviour, IObserver | ||
{ | ||
public void OnNotify(object sender, object eventData) | ||
{ | ||
// Handle the event | ||
UnityEngine.Debug.Log($"Received event from {sender} with data: {eventData}"); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Collections.Generic; | ||
|
||
public class Subject : ISubject | ||
{ | ||
private List<IObserver> observers = new List<IObserver>(); | ||
|
||
public void RegisterObserver(IObserver observer) | ||
{ | ||
if (!observers.Contains(observer)) | ||
{ | ||
observers.Add(observer); | ||
} | ||
} | ||
|
||
public void UnregisterObserver(IObserver observer) | ||
{ | ||
if (observers.Contains(observer)) | ||
{ | ||
observers.Remove(observer); | ||
} | ||
} | ||
|
||
public void NotifyObservers(object eventData) | ||
{ | ||
foreach (var observer in observers) | ||
{ | ||
observer.OnNotify(this, eventData); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.