Lesson 5 | Why use design patterns? |
Objective | Learn how design patterns assist programmers. |
Design Pattern Benefits and how they assist Programmers
Question: Why would you use design patterns?
Answer:The answer is so simple it is almost a tautology: Design patterns are used because they make your job easier.
Design patterns let you write better code more quickly.
However, patterns are not a panacea. Of the five phases of software development, design patterns do almost nothing in the analysis, testing,
or documentation phases. Design patterns, as the name implies, have their biggest impact in the design phase of a project.
Five phases of Software Development
Software development (as opposed to mere coding) can be divided into five stages:
- Analysis: understand the problem to be solved; gather user requirements
- Design: choose the data structures,
- algorithms,
- classes, and
- patterns
that will model the problem; possibly prototype the system, especially if it has a large user interface component
- Code: implement the design in code
- Test: debug and correct mistakes
- Document: write the user documentation for the system (should ideally occur throughout the development process)
A number of years ago, each of these stages took roughly 20% of the total time in a project, though today the actual writing of code is likely to be the shortest of the phases. One common source of project overrun is only accounting for what is to most programmers the most interesting phase, writing code. Many efforts have been made to make the writing of code more efficient.However, relatively little effort has gone into increasing efficiency in the other four stages.
To be more precise:
- Design patterns help you analyze the more abstract areas of a program by providing concrete, well-tested solutions.
- Design patterns help you write code faster by providing a clearer picture of how you are implementing the design.
- Design patterns encourage code reuse and accommodate change by supplying well-tested mechanisms for
delegation[1] and composition[2], and other non-inheritance based reuse techniques.
- Design patterns encourage more legible and maintainable code by following well-understood paths.
- Design patterns increasingly provide a common language and jargon for programmers.
Understanding Object-Oriented Design Through Design Patterns
Alright, let's dive right into it. You have probably heard the buzz about C# and Java being strikingly similar. And guess what? This resemblance isn't just cosmetic, it runs deep, affecting how developers approach problems and craft solutions. Now, when we talk about these languages, we can't ignore the elephant in the room: design patterns. These are like the secret sauce that spices up the coding experience, making things smoother, clearer, and, dare I say, more enjoyable. Now, here's the deal: if you are looking to get a solid grasp on object-oriented design, studying design patterns is akin to finding a treasure map. Let's explore why.
- Blueprints for Problem Solving: Imagine you're building a Lego structure. Design patterns are like those handy instruction booklets, guiding you step by step. They offer tried-and-true solutions to common challenges, ensuring you don't get lost in the coding wilderness. By studying them, you get a direct line into understanding the principles of object-oriented design: encapsulation, inheritance, polymorphism, and abstraction. It's like learning the ABCs of OOP!
- Spotlight on Best Practices: There's coding, and then there is efficient coding. Design patterns are the culmination of collective wisdom from seasoned developers. They embody best practices in object-oriented design. So, by diving into patterns, you are not just learning to code, you are learning to code correctly. It is like getting tips straight from the pros.
- Enhanced Code Reusability: Remember the joy of recycling Lego pieces to create something new? That's what design patterns offer. They emphasize modular and reusable code. By understanding patterns, you will find yourself writing code that's more adaptable and can be reused in various scenarios.
- Boosted Communication: Here's a game-changer: design patterns give you a shared vocabulary. Instead of saying, "I've set up this system where objects can handle a request or pass it on to another object," you can just say, "I've implemented the Chain of Responsibility pattern." It's like having a secret handshake with fellow developers. Studying patterns, therefore, is not just about coding, it is also about enhancing collaboration and clarity.
- Predictable System Design: With design patterns, there is an element of predictability. When you recognize a pattern in play, you get a good sense of the system's behavior and structure. It's akin to recognizing a familiar melody in a song. By diving deep into patterns, you're training your brain to spot these melodies, making it easier to navigate and understand object-oriented systems.
- Enhancing Adaptability: Design patterns are all about flexibility. They ensure your system can evolve and adapt without monumental overhauls. By internalizing patterns, you learn the art of crafting systems that can weather changes. It's like building a ship that not only sails smoothly but can also handle stormy seas.
In conclusion, if you're on a quest to master object-oriented design, design patterns are your trusty compass. Whether you're coding in C#, Java, or any other OOP language, patterns light the way, ensuring you craft systems that are efficient, adaptable, and, most importantly, elegant. So, roll up those sleeves, dive into patterns, and watch as the intricate world of object-oriented design unfolds before you!
Ad Gang of Four Patterns
Design Patterns Purpose - Quiz
[1]Delegation: This pattern is one where a given object provides an interface to a set of operations. However, the actual work for those operations is performed by one or more other objects.
[2]composition: Creating objects with other objects as members. Composition should be used when a 'has-a' relationship applies.