What Are Design Patterns and Do I Need Them?
Software professionals may be familiar with the term “Design Patterns,” but many have no idea of where they come from and what they truly are. Consequently, some do not see the value and benefits design patterns bring to the software development process, especially in the areas of maintenance and code reuse. This article will bridge this gap by defining design patterns from a historical perspective. It will also summarize the salient features of a typical design pattern and arrive at a working definition so that you will know what they are and what to expect when you incorporate them into your designs. Finally, it will explicitly summarize the benefits design patterns bring to software development and why you should incorporate them into your work. Subsequent articles will present more detailed descriptions of some of the more common design patterns, and how they can be applied to software development on the .NET platform.
Benefits of Design Patterns
Design patterns have two major benefits. First, they provide you with a way to solve issues related to software development using a proven solution. The solution facilitates the development of highly cohesive modules with minimal coupling. They isolate the variability that may exist in the system requirements, making the overall system easier to understand and maintain. Second, design patterns make communication between designers more efficient. Software professionals can immediately picture the high-level design in their heads when they refer the name of the pattern used to solve a particular issue when discussing system design.
Design patterns are recurring solution to recurring problems in software architecture
Design patterns can be divided into 3 categories.
- Creational Patterns : These patterns deals mainly with creation of objects and classes.
- Structural Patterns : These patterns deals with Class and Object Composition.it is identifying a simple way to realize relationships between entities.
ex–Adapter,bridge,composite,decorator,flyweight,proxy
3.Behavioural Patterns : These mainly deals with Class – Object communication. That means they are concerned with the communication between class and objects.
it describes a process or flow.ex–command,iterator,observer,null object .
- Constructing a complex object step by step : builder pattern
- A simple object is created by using a single method : factory method pattern
- Creating Object by using multiple factory method : Abstract factory pattern
singleton and static class difference
- Singleton is a design pattern which solves the problem in a particular context (when we want that only one instance of an object can be created). Whereas static class is a concept where class will be defined with static keyword and all the members need to be strictly static
- Singleton pattern let us create the object of the class only once whereas we can’t create the object of static class.
- Class which follows singleton pattern can implement other interfaces whereas static class cannot.
There is one more question normally asked during interviews “What you will prefer for sharing, static class or singleton pattern”
First of all both allows us to share, but I personally prefer singleton pattern because we can derive our singleton class from other interfaces and so get polymorphic feature.
Example: IPerson s=SingletonPerson.GetObject(PersonEnum.Male); //s is Male
: IPerson s=SingletonPerson.GetObject(PersonEnum.Female); //s is Female
Secondly Singleton class may contain some non-static members as well.
And most importantly we can control the life of a singleton object. It can be explicitly destroyed in between the application.