Design Patterns
Reusable solution for common recurring problems in software design used in engineering, not ready/finished design which can be directly transformed for coding but rather a template, guideline or model for solving problems in many different situations. Design patterns can be adapted to different situations and contexts, providing flexibility in problem-solving for easier understanding, modification and reusability.
Gang of Four
The credit of Design Patterns goes to a team aka?Gang of Four comprising?Erich Gamma, Richard Helm, Ralph Johnsonand,?John Vlissides?who were?the authors of the book on Java Design Patterns.
Nutshell
The various categories include Creational, Structural, and Behavioural patterns.
Creational
Focus on the process of creating objects, enhance flexibility and efficiency in object creation, allowing systems to remain independent of how their objects are constructed, composed, and represented, helps streamline instantiation process leading to more maintainable and adaptable code.
a. Factory Method: Separate construction of an object from its implementation, object can be produced without having to define exact class.
b. Abstract Factory: Considered as another layer of abstraction over factory pattern, super-factory which creates other factories.
c. Singleton: Class has just one and only one instance, offers a way to access globally.
d. Prototype : Hide complexity of making new instances from client by copying an existing object rather than creating a new instance from scratch.
e. Builder: Separate construction of a complex object from representation, same construction process can create different representations.
Structural
Focus on how classes and objects are arranged to create larger, more complex structures, they help to organise relationships between objects, making systems more flexible, reusable, and maintainable by using these patterns.
a. Adapter: Convert interface of a class into another interface clients expect, classes work together that couldn’t otherwise because of incompatible interfaces.
b. Bridge: Allows Abstraction and Implementation to be developed independently. Client code can access only Abstraction part without being concerned about Implementation part.
c. Composite: Handles collection of items, the same way as a single instance of same type of object by “composing” objects into tree structures to represent part-whole hierarchies.
d. Decorator: Dynamically add functionality and behaviour to an object without affecting behaviour of other existing objects within the same class. Inheritance extends behavior of class at compile-time, and all instances of that class get extended behavior.
e. Fa?ade: Unified interface to a set of interfaces in a subsystem. Defines a high-level interface which makes subsystem easier to use.
f. Flyweight: Provides ways to decrease object count, improving application required objects structure, when we need to create a large number of similar objects.
g. Proxy: Surrogates, Handles, Wrappers to be used ‘in place of’, representing’ or ‘in place of’ or ‘on behalf of’.
Behavioural
Focus on how objects/ classes interact and communicate, emphasize collaboration between objects to effectively accomplish tasks and responsibilities, making system more manageable/ adaptable.
a. Chain Of Responsibility: Achieve loose coupling in software design, request from client is passed to a chain of objects to process them. Objects in the chain decide who will be processing request and whether request is to be sent to next object in the chain or not.
b. Command: Transforms request into an independent object with all of the information's request to be passed around, stored, and executed at a later time.
c. Interpreter: Defines grammatical representation for a language and provides interpreter.
d. Mediator: Enables decoupling of objects by introducing layer for interaction between objects through that layer.
e. Memento: Returns an object's state to its initial state, create checkpoints in application for returning it to previous stable state.
f. Observer: Establishes one-to-many dependency between objects, all of dependents (observers) of subject are immediately updated/ notified when subject changes.
g. State: When object modifies its behaviour according to internal state.
领英推荐
h. Strategy: Select an object's behaviour at runtime.
i. Template: Defines an algorithm as a collection of skeleton operations, with child classes handling implementation of specifics. Parent class maintains overall structure and flow of algorithm.
j. Visitor: Perform operation on a group of similar kind of objects, aids to move operational logic from objects to another class.
What ?
Design patterns are tried and tested way to solve particular design issues by various programmers in the world. Design patterns are extension of code reuse, not specifically algorithms or code snippets, but rather general guidelines and principles which describe how to structure and relate different components of a software system based on best practices and proven solutions, emerged from years of experience and research.
Why?
Can help to solve common problems/ challenges in software development such as creating objects which are flexible/ reusable, composing/ combining classes/ objects and defining/ coordinating communication between objects/ components. By applying design patterns, multi-fold improvements can be done such as readability and clarity of code, reduce complexity and redundancy, enhance maintainability and extensibility, increase reusability and portability by using consistent and familiar terminology and structure, avoiding unnecessary repetition and duplication of logic/ functionality, making it easier to modify, test and debug keeping the system more modular/ independent of specific implementations/ platforms.
How?
a. Choose/ Apply
Are not a one-size-fits-all solution to any problem, tools that can help you design and implement better software, but also have limitations and trade-offs to be considered. To choose/ apply design patterns effectively, identify and analyse the problem and context, search and select relevant design patterns, adapt/ implement them according to specific situation/ preferences. Evaluate benefits, drawbacks and implications of each design pattern, and consider how they match needs and expectations. Additionally, consider customising and modifying design patterns to fit project and code style, as well integration and combination with other design patterns/ techniques.
b. Learn/ Master
Not a task to be be completed in a single day but requires practice, experimentation, feedback to understand/ apply correctly and efficiently. Explore existing code and projects that use design patterns to gain insight into how they are implemented and applied. Practicing and experimenting with design patterns in one's own code and projects can help to understand the challenges and difficulties which one may encounter. Finally, review and evaluate code and projects to ensure they meet goals, requirements, and expectations.
To gain better understanding of design patterns read books, articles, tutorials which explain and illustrate concepts. Popular books include Design Patterns: Elements of Reusable Object-Oriented Software, Head First Design Patterns, and Refactoring to Patterns.
c. Share/ Communicate
They're not only useful for writing code, but also for sharing and communicating ideas and solutions with other developers and stakeholders. One can freely express and explain design and architecture decisions and rationale more clearly/ concisely, collaborate and cooperate more effectively/ efficiently with other developers and teams learn and improve from feedback/ suggestions of other developers and experts. To share and communicate design patterns, one can use diagrams and visual representations to illustrate/ document structure and behavior of design patterns, code comments and documentation to describe and explain purpose and functionality of design patterns, and code reviews and peer reviews to share and discuss code and design patterns with other developers.
Tools and frameworks such as Javadoc, Doxygen, and Sphinx, as well as platforms such as GitHub, GitLab, and CodeGuru, can also be used to facilitate and automate the process.
Uses ?
To speed up the development process by providing tested, proven development paradigms. Effective software design requires considering issues which may not become visible until later in implementation. Reusing design patterns helps to prevent subtle issues that can cause major problems and improves code readability for coders and architects familiar with the patterns.
Often, people only understand how to apply certain software design techniques to certain problems which are difficult to apply to a broader range of problems. Design patterns provide general solutions, documented in a format that doesn't require specifics tied to a particular problem. In addition, patterns allow developers to communicate using well-known, well understood names for software interactions. Common design patterns can be improved over time, making them more robust than ad-hoc designs.
*****************************
Creational/ Structural Design patterns
Behavioural Design patterns
References