Design Principle vs Design Pattern
Design Principle vs Design Pattern in C#
357
11-Jun-2024
Updated on 12-Jun-2024
Ashutosh Kumar Verma
12-Jun-2024C# Design Principle vs Design Pattern
In software development in C#, design principles and design models play an important role in creating robust, maintainable and scalable applications but serve different purposes and operate at different levels of abstraction. Let’s explore the difference between design principles and design patterns in C#.
Design Principles
Design principles are high-level guidelines aimed at improving the design and design of software systems. No programming language is designed specifically but is used universally in object-oriented programming. Here are some basic design principles.
Solid principles
Single Responsibility Principle (SRP)- A class should have only one reason to change, ie. it must have only one role or responsibility.
Open/Closed Principle (OCP)- Software organizations should be open to extension but closed to change.
Liskov Substitution Principle (LSP)- Objects of a superclass can be substituted for objects of a subclass without affecting the validity of the program.
Interface Segmentation Principle (ISP)- No customer should be forced to rely on unused channels. This means creating small, specific networks rather than large, general ones.
Dependency Inversion Principle (DIP)- A higher-level module should not depend on a lower-level module. Both must be based on abstractions. Summaries should not depend on details.
Details may depend on abstraction.
DRY (do not repeat)
Avoid duplication of code. Instead, make sure every piece of knowledge has a single, unambiguous, authoritative representation within the system.
KISS (Keep It Simple, Stupid)
Simplicity should be the primary design goal, and unnecessary complexity should be avoided.
YAGNI (You Aren't Gonna Need It)
Do not add functions unless necessar
Encapsulation
Hide the object’s usage information, and expose only what is needed through the public interface.
Design Patterns
Design patterns are specific solutions to common problems in software development. Not specific rules but templates that can be used to solve specific problems in different situations. Design systems can be divided into three main categories,
Creational Patterns
Singleton- Checks that a class has only one instance and provides global access.
Factory Method- Defines the interface for creating an object, but allows subclasses to change the type of object to be created.
Abstract Factory- Provides an interface for creating a family of related or dependent objects without specifying their concrete classes.
Builder- separates the construction of the complex object from its representations, so that the same construction method can consist of different representations.
Prototype- Creates new objects by overwriting an existing object, known as a prototype.
Structural Patterns
Adapter- Enables incompatible interfaces to work together by acting as a bridge between them.
Composite- organize objects into a tree structure to represent a part- whole structure. It allows customers to handle individual products and packages with precision.
Proxy- Provides a delegate or placeholder for another object to manage its path.
Decorator- Dynamically attaches additional responsibilities to objects, providing an easy way for subcategorization to extend functionality.
Facade- Provides a simple interface for a complex subsystem.
Flyweight- Reduces the cost of manufacturing and converting large quantities of identical components by manufacturing parts together.
Behavioral Patterns
Observer- Defines one to many dependent objects so that when one object changes state, all of its dependencies are automatically notified and updated.
Strategy- Defines a family of algorithms, holds each one, and makes it scalable, so that the algorithm stands out independently from the clients who use it
Commands- Contains the request as an object, thereby allowing users to parameterize clients with queues, queries, and services.
Responsibility chaining- sends requests in a chain of handlers, so that each handler can process or forward the request to the next handler in the chain
State- When an object changes its internal state, it can change its behavior, and seems to change its class.
summary
Design principles- Detailed guidelines for improving overall software design (e.g., SOLID principles, DRY, KISS). They are abstract and can be used in a variety of software programs and architectures.
Design Patterns- Specific solutions to common programming problems (e.g., Singleton, Factory Method, Observer). They are concrete templates that can be used to solve specific problems in software design.
Both principles and patterns are important in software development because they help create a clean, efficient and maintainable codebase in C#.
Also, Read: C# Boxing and Unboxing