{"id":29100606,"url":"https://github.com/elis-vasconcelos/observer-pattern","last_synced_at":"2025-10-25T08:45:31.770Z","repository":{"id":301601311,"uuid":"1009777058","full_name":"Elis-Vasconcelos/observer-pattern","owner":"Elis-Vasconcelos","description":"Implementation of the Observer pattern in Java","archived":false,"fork":false,"pushed_at":"2025-06-27T17:47:22.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-27T18:37:41.054Z","etag":null,"topics":["java","observer","observer-design-pattern","observer-pattern","poo"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Elis-Vasconcelos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-27T17:36:00.000Z","updated_at":"2025-06-27T17:47:57.000Z","dependencies_parsed_at":"2025-06-27T18:51:02.167Z","dependency_job_id":null,"html_url":"https://github.com/Elis-Vasconcelos/observer-pattern","commit_stats":null,"previous_names":["elis-vasconcelos/observer-pattern"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Elis-Vasconcelos/observer-pattern","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elis-Vasconcelos%2Fobserver-pattern","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elis-Vasconcelos%2Fobserver-pattern/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elis-Vasconcelos%2Fobserver-pattern/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elis-Vasconcelos%2Fobserver-pattern/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Elis-Vasconcelos","download_url":"https://codeload.github.com/Elis-Vasconcelos/observer-pattern/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elis-Vasconcelos%2Fobserver-pattern/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262477881,"owners_count":23317570,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","observer","observer-design-pattern","observer-pattern","poo"],"created_at":"2025-06-28T18:37:12.331Z","updated_at":"2025-10-25T08:45:31.647Z","avatar_url":"https://github.com/Elis-Vasconcelos.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Observer Pattern Implementation in Java\n\nA comprehensive implementation of the Observer design pattern in Java, demonstrating how to create a financial monitoring system where different services (Google, UOL) can subscribe to receive notifications when financial instruments (Títulos) are updated.\n\n## 🎯 What is the Observer Pattern?\n\nThe Observer pattern is a behavioral design pattern that defines a one-to-many dependency between objects. When one object (the subject) changes state, all of its dependents (observers) are notified and updated automatically.\n\n### Key Benefits:\n- **Loose Coupling**: Subjects and observers are loosely coupled\n- **Dynamic Relationships**: Observers can be added or removed at runtime\n- **Broadcast Communication**: One subject can notify multiple observers\n- **Open/Closed Principle**: Easy to add new observer types without modifying existing code\n\n## 📁 Project Structure\n\n```\nobserver-pattern/\n├── Subject.java              # Subject interface defining subscription methods\n├── Listener.java             # Observer interface defining update method\n├── Titulo.java               # Concrete subject (financial instrument)\n├── Google.java               # Concrete observer implementation\n├── Uol.java                  # Concrete observer implementation\n├── ExemploMonitoramento.java # Main class demonstrating the pattern\n└── README.md                 # This file\n```\n\n## 🏗️ Architecture Overview\n\n### Interfaces\n\n#### Subject Interface\n```java\npublic interface Subject {\n    public void notifyListener(Listener l);\n    public void subscribeListener(Listener l);\n    public void unsubscribeListener(Listener l);\n}\n```\n\n#### Listener Interface (Observer)\n```java\npublic interface Listener {\n    public void update(Titulo titulo);\n}\n```\n\n### Concrete Implementations\n\n#### Titulo (Concrete Subject)\nRepresents a financial instrument with the following properties:\n- `codigo` - Instrument code\n- `valorCompra` - Purchase price\n- `valorVenda` - Sale price  \n- `tipo` - Instrument type (Stock, Bond, Option, etc.)\n- `dataVencimento` - Expiration date\n- `dataLancamento` - Launch date\n\n**Key Features:**\n- Maintains a list of subscribers (observers)\n- Automatically notifies all subscribers when any property changes\n- Implements all Subject interface methods\n\n#### Google \u0026 UOL (Concrete Observers)\nTwo different notification services that implement the Listener interface:\n- **Google**: Receives and displays notifications from subscribed financial instruments\n- **UOL**: Another news service that can monitor financial instruments\n\n## 🚀 How to Run\n\n### Prerequisites\n- Java 8 or higher\n- Any Java IDE or command line\n\n### Compilation and Execution\n\n1. **Compile all Java files:**\n```bash\njavac *.java\n```\n\n2. **Run the example:**\n```bash\njava ExemploMonitoramento\n```\n\n### Expected Output\n```\nUpdating titulo1 valorCompra...\nGoogle notified: Titulo{codigo='T1', valorCompra=110.0, valorVenda=105.0, tipo='Stock', dataVencimento=java.text.SimpleDateFormat@..., dataLancamento=java.text.SimpleDateFormat@...}\n\nUpdating titulo2 tipo...\nUol notified: Titulo{codigo='T2', valorCompra=200.0, valorVenda=205.0, tipo='Modified Bond', dataVencimento=java.text.SimpleDateFormat@..., dataLancamento=java.text.SimpleDateFormat@...}\n\nUpdating titulo3 codigo...\nGoogle notified: Titulo{codigo='T3-Updated', valorCompra=300.0, valorVenda=305.0, tipo='Option', dataVencimento=java.text.SimpleDateFormat@..., dataLancamento=java.text.SimpleDateFormat@...}\nUol notified: Titulo{codigo='T3-Updated', valorCompra=300.0, valorVenda=305.0, tipo='Option', dataVencimento=java.text.SimpleDateFormat@..., dataLancamento=java.text.SimpleDateFormat@...}\n```\n\n## 📋 Example Usage\n\nThe `ExemploMonitoramento` class demonstrates three scenarios:\n\n1. **Single Observer**: `titulo1` notifies only Google\n2. **Single Observer**: `titulo2` notifies only UOL  \n3. **Multiple Observers**: `titulo3` notifies both Google and UOL\n\n```java\n// Create financial instruments\nTitulo titulo1 = new Titulo(\"T1\", 100.0, 105.0, \"Stock\", sdf, sdf);\n\n// Create observers\nListener google = new Google();\nListener uol = new Uol();\n\n// Subscribe observers\ntitulo1.subscribeListener(google);\n\n// Any property change triggers notifications\ntitulo1.setValorCompra(110.0); // Google gets notified\n```\n\n## 🔧 Extending the Implementation\n\n### Adding New Observer Types\nTo add a new observer (e.g., Bloomberg):\n\n```java\nclass Bloomberg implements Listener {\n    @Override\n    public void update(Titulo titulo) {\n        System.out.println(\"Bloomberg received update: \" + titulo.getCodigo());\n    }\n}\n```\n\n### Adding New Subject Types\nTo create new types of financial instruments, extend or create new classes that implement the `Subject` interface.\n\n## 🎓 Learning Objectives\n\nThis implementation helps understand:\n\n1. **Interface Segregation**: Clean separation between Subject and Observer responsibilities\n2. **Polymorphism**: Different observer implementations can be treated uniformly\n3. **Event-Driven Programming**: Objects react to state changes automatically\n4. **Subscription Management**: Dynamic addition/removal of observers\n5. **Notification Patterns**: Broadcast communication in software systems\n\n## 📚 Related Patterns\n\n- **Model-View-Controller (MVC)**: Observer pattern is fundamental to MVC architecture\n- **Mediator Pattern**: Both deal with communication between objects\n- **Command Pattern**: Can be combined with Observer for undo/redo functionality\n\n## 🤝 Contributing\n\nFeel free to fork this repository and submit pull requests for improvements:\n- Add more observer implementations\n- Implement filtering mechanisms\n- Add error handling\n- Improve the toString() method formatting\n- Add unit tests\n\n## 📄 License\n\nThis project is for educational purposes as part of Software Engineering studies at UFBA.\n\n---\n\n*This implementation was created as part of Software Engineering coursework, demonstrating the practical application of the Observer design pattern in Java.*\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felis-vasconcelos%2Fobserver-pattern","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felis-vasconcelos%2Fobserver-pattern","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felis-vasconcelos%2Fobserver-pattern/lists"}