{"id":21445262,"url":"https://github.com/bunu23/designpattern","last_synced_at":"2025-10-06T20:05:54.075Z","repository":{"id":237316207,"uuid":"758180454","full_name":"bunu23/DesignPattern","owner":"bunu23","description":"collection of Java design pattern implementations","archived":false,"fork":false,"pushed_at":"2024-11-25T14:48:18.000Z","size":704,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-17T01:17:59.910Z","etag":null,"topics":["builder","decorator-pattern","design-principles","facade","factory","singleton","strategy","template-method-pattern"],"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/bunu23.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}},"created_at":"2024-02-15T19:28:49.000Z","updated_at":"2025-01-02T22:16:56.000Z","dependencies_parsed_at":"2024-04-30T21:08:36.177Z","dependency_job_id":"809aeaa9-e7c3-435a-b980-9d5bc6a2cc3c","html_url":"https://github.com/bunu23/DesignPattern","commit_stats":null,"previous_names":["bunu23/designpattern"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bunu23/DesignPattern","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bunu23%2FDesignPattern","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bunu23%2FDesignPattern/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bunu23%2FDesignPattern/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bunu23%2FDesignPattern/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bunu23","download_url":"https://codeload.github.com/bunu23/DesignPattern/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bunu23%2FDesignPattern/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278671745,"owners_count":26025745,"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","status":"online","status_checked_at":"2025-10-06T02:00:05.630Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["builder","decorator-pattern","design-principles","facade","factory","singleton","strategy","template-method-pattern"],"created_at":"2024-11-23T02:25:20.280Z","updated_at":"2025-10-06T20:05:54.059Z","avatar_url":"https://github.com/bunu23.png","language":"Java","readme":"# Table of Contents\n\n1. [Design Principles](#design-principles)\n2. [Singleton Pattern](#singleton-pattern)\n\n   - [Eager Instantiation](#eager)\n   - [Lazy Instantiation](#lazy)\n\n3. [Builder Pattern](#builder-pattern)\n\n4. [Facade Pattern](#facade-pattern)\n\n5. [Factory Pattern](#factory-pattern)\n\n6. [Strategy Pattern](#strategy-pattern)\n\n7. [Template Method Pattern](#template-method-pattern)\n\n8. [Iterator Pattern](#iterator-pattern)\n\n9. [Decorator Pattern](#decorator-pattern)\n\n---\n\n# Design Principles\n\n- Keep it simple\n- Keep it flexible\n- Loose coupling\n- Separation of concern\n- Information hiding\n- Principle of modularity\n- DRY: Don’t repeat yourself\n- Encapsulate what varies\n- Solid\n  - Single Responsibility Principle (SRP)\n  - Open-Closed Principle (OCP)\n  - Liskov Substitution Principle (LSP)\n  - Interface Segregation Principle (ISP)\n  - Dependency Inversion Principle (DIP)\n\n---\n\n# [Singleton pattern](src/singleton)\n\nSingleton is a very simple pattern, just have one object instance.\n\n**Implementation:**\n\n    1. Make the constructor private.\n    2. Declare a private static instance of the class.\n    3. Add a static method to get an instance of the singleton class.\n\n    some example of singleton: Connection Pool, PrinterBuffer and Cache.\n\n![](images/singleton.png)\n\nWith singleton we often share the resources but making or instantiating shared resources , reading something from a property file, creating database connection takes a lot of time. so we two options with singleton 1. Eager Instantiation 2. Lazy Instantiation.\n\n### Eager\n\nWe create connection pool in the beginning before even calling static method .\n\n```java\npublic class ConnectionPool {\n​//declare a private static instance of a class\nprivate static ConnectionPool pool=new ConnectionPool();\n//this is a pool with only one connection\nprivate Connection connection=new Connection();\n\nprivate ConnectionPool() {}\n//add a static method to get an instance of a singleton class\npublic static ConnectionPool getPool() {\n​return pool;\n​}\npublic Connection getConnection() {\n​return connection;\n}\n}\n```\n\n### Lazy:\n\nWhenever we call method than we create a connection pool. Most often this we use.\n\n```java\npublic class ConnectionPool {\nprivate static ConnectionPool pool;\n//this is a pool with only one connection\nprivate Connection connection=new Connection();\nprivate ConnectionPool() {}\n//add a static method to get an instance of a singleton class\npublic static ConnectionPool getPool() {\n​if(pool==null) {\n​​pool=new ConnectionPool(); //lazy instantiation\n​}\n​}\npublic Connection getConnection() {\n​return connection;\n}\n\n}\n```\n\n---\n\n# [Builder Pattern](src/builder)\n\n- Builds a complex object using step by step approach. This pattern is helpful while creating objects with many different parameters.\n- If we want Expressive code and Immutable class\n  - solution: Bulilder pattern\n\n---\n\n# [Facade Pattern](src/facade)\n\nThe facade pattern provides a unified interface to a complex set of classes. It hides the complexity from the clients.\n\n---\n\n# [Factory Pattern](src/factory-pattern)\n\nA factory creates objects\n\n- Encapsulation of the logic to create objects\n\n\u003cimg src=\"images/with-factory.png\" width=\"600\" height=\"400\" /\u003e\n\u003cimg src=\"images/without-factory.png\" width=\"600\" height=\"400\" /\u003e\n\n#### Different types of factories:\n\n- Simple factory method\n  - Static or not static\n- Factory method pattern\n- Abstract factory pattern\n\n---\n\n# [Strategy Pattern](src/strategypattern/)\n\nThe strategy pattern extracts algorithms (strategies) from a certain class (context class) and makes a different class for every single algorithm. This gives the following advantages\n\n- We can easily add new algorithms without changing the context class\n- The strategies are better reusable\n\n\u003cimg src=\"images/strategy-demo.png\" width=\"600\" height=\"400\" /\u003e\n\n---\n\n# [Template Method Pattern](src/template-method-pattern)\n\nThe template method pattern defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.\n\n- What problem does it solve?\n\n  Whenever we have an algorithm with different steps that is used in different situations, then we can define this algorithm in one place (parent class) and let child classes implement the concrete steps. The template method can be used when you have a stable algorithm whose individual steps may vary.\n\n---\n\n# [Iterator Pattern](src/iterator-pattern)\n\nIterators are used to access the elements of an aggregate object sequentially without exposing its underlying implementation. An iterator object encapsulates the internal structure of how the iteration occurs.\n\n- External iterator: The client controls the iteration\n- Internal iterator: The iterator controls the iteration\n\n---\n\n# [Decorator Pattern](src/decorator-pattern)\n\nAllows to dynamically add new behavior to an existing object. This pattern is useful when we need to enhance the behavior of a class while adhering to the Open-Closed Principle — that is, our code should be open for extension but closed for modification.\n\n---\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbunu23%2Fdesignpattern","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbunu23%2Fdesignpattern","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbunu23%2Fdesignpattern/lists"}