Skip to main content

The Concepts Of Design Pattern - Questions and Answers

This article helps you to learn about design patterns and uses of them. I have tried to easily explain the problem statement where you can use these design patterns.

I have cover all below topics to understand the concepts of Design Pattern.

Table of Contexts -
1.      What is Design Pattern?
2.      Why should you use Design Patterns?
3.      What are the Advantages of Design Patterns?
4.      What are the Disadvantages of Design Patterns?
5.      What about Anti-patterns?
6.      Are Design Patterns the same thing as Frameworks?
7.      What are the Gang of Four (GoF) Design Patterns?
8.      Which Pattern is the Foundation of Design Pattern?
9.      What are the types of Design Patterns?
10.  What is Creational Design Pattern?
11.  What is Structural Design Pattern?
12.  What is Behavioural Design Pattern?
13.  What is Factory method pattern?
14.  When to use Factory method pattern?
15.  What is Abstract Factory Pattern?
16.  When you use Abstract Factory Pattern?
17.  What is the difference between factory and abstract factory patterns?
18.  What is Prototype Pattern?
20.  What is MVC Pattern?

What Is a Design Pattern?

The “design pattern” is a general reusable solution for a usually occurring problem in software design. Basically, it is a description for how to solve problems that can be used in different circumstances.

The “design patterns” are used for best practices that the programmers can use to solve common occurring problems when designing an application.

The “design patterns” can improve the development process by providing tested, proven development paradigms.

Stayed Informed – What is Repository Pattern?

Why should you use Design Patterns?

The “design patterns” provides us many more advantages when we are designing an application and some of the following advantages as given bellow,
1.      A pattern provides us solution for the common problems which occur in software application.
2.      A pattern provides us common platform to the developers and the developers can be used in any programming languages.
3.      A pattern provides us code reusability, testability and extensibility.
4.      A pattern helps us to improve developer communication.

What are the Advantages of Design Patterns?

The List of Advantages,
1.      A pattern provides us code reusability, testability and extensibility.
2.      A pattern provides us to enabling large scale reuse of S/W code.
3.      A pattern helps us to improve developer communication.
4.      Patterns capture expert knowledge and design. It’s make expertise widely available.
5.      Pattern allows us to change the design of your application more readily.
6.      Pattern allows us to easily test the seam of an application.
7.      And so on..

What are the Disadvantages of Design Patterns?

The List of Disadvantages,
1.      We don’t lead to direct code reuse and complex in nature. I mean we used extra layer in our applications.
2.      The patterns consume more memory because it is in generalized format.
3.      The patters are validated by experience and discussion.
4.      And so on..

What About Anti-patterns?

The selection of wrong design pattern can perform a negative effect on your application codes because it is required to choose appropriate design pattern for you and the selection of wrong design pattern is defined as “anti-pattern”.

Are Design Patterns the same thing as Frameworks?

No!, Both the design pattern and framework are different.  The design pattern provides only the guideline, not codes but framework deals with codes and so on..

What are the Gang of Four (GoF) Design Patterns?

The “design patterns” gained popularity in computer science after the Design Patterns book. Basically, it was published in 1994/95 by four authors Erich Gamma, Richard Helm, Ralph Johnson and John. These four book authors are known as Gang of Four (GoF).

Which Pattern is the Foundation of Design Pattern?

The Gang of Four (GoF) design patterns is the foundation for all other design patterns that is called the mother of design patterns.

What are the types of Design Patterns?
                                           OR
What are the main Categories of (GoF) Pattern?

The “design patterns” are divided in to three main categories and it’s depending on the problem and its solutions. The categories are,

1.      Creational Patterns – The creational patterns are ones that create objects for you, rather than having you instantiate objects directly. This pattern gives us more flexibility in deciding which objects need to be created for a given case.
2.      Structural Patterns - These concern class and object composition. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.
3.      Behavioural Patterns – The most of these design patterns are specifically concerned with communication between objects or classes.

What Is Creational Design Pattern?

The creational patterns are ones that create objects for you, rather than having you instantiate objects directly. This pattern gives us more flexibility in deciding which objects need to be created for a given case.

We have five types of Design Patterns i.e.
1.      Singleton
2.      Factory
3.      Abstract Factory
4.      Prototype
5.      Builder

What Is Structural Design Pattern?

These concern class and object composition. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.
We have 6th types of Design Patterns i.e.
1.      Adapter
2.      Bridge
3.      Decorator
4.      Facade
5.      Flyweight
6.      Proxy

What Is Behavioural Design Pattern?

The most of these design patterns are specifically concerned with communication between objects or classes.
We have 11th types of Design Patterns i.e.
1.      Chain of responsibility
2.      Command
3.      Interpreter
4.      Iterator
5.      Mediator
6.      Memento
7.      Observer
8.      State
9.      Strategy
10.  Template method
11.  Visitor

What Is Factory method pattern?

The factory method pattern is used to create objects at run-time based on your demand. The factory pattern is most commonly used in modern programming languages like C# and Java.

For example, suppose that a bottle factory which produces us different types of bottles. If we went to bottle factory and the order for water-bottles, the factory method will produce water-bottle and will provide us.

When to use Factory method pattern?
The factory method pattern is used when the,
1.      Creation of object is done when it is required.
2.      Flexibility is important.
3.      Sub-classes decide what objects should be created.
4.      And so on..

What is Abstract Factory Pattern?
The abstract factory patterns acts as like “super factory” which creates other factories that is also called Factory of factories pattern.

The abstract factory patterns are used to create a set of dependent objects.  At the time of creating objects this pattern internally use “factory method design pattern” and “prototype design pattern”.

When you use Abstract Factory Pattern?
The abstract factory pattern is used when we need to create a set of dependent objects or related objects.

At the time of creating objects this pattern internally use “factory method design pattern” and “prototype design pattern”.

What Is the difference between factory and abstract factory patterns?

The Factory Pattern is used to create objects at run-time based on your demand. The factory pattern is most commonly used in modern programming languages like C# and Java.

The Abstract Factory Patterns acts as like “super factory” which creates other factories that is also called Factory of factories pattern.

The Abstract Factory Patterns are used to create a set of dependent objects.  At the time of creating objects this pattern internally use “factory method design pattern” and “prototype design pattern”.

What is Prototype Pattern?
The prototype patterns are used to create a new object from the existing object instance and these patterns are also used to create a duplicate object of the current object to enhance application performance.

When we use Prototype Pattern?
The prototype patterns are used when the creation of object is so complex.


The Singleton Pattern ensures that a class has only one instance in the across application and the object instance coordinate to across the app.
This pattern is the simplest design patterns and used to restrict the creation of multiple objects instance.

This pattern is the simplest design patterns and used to restrict the creation of multiple objects instance.

Table of Contents - Implementing the Singleton Pattern in C#
1.      Introduction - What Is Singleton Pattern?
2.      Non-thread-safe version
3.      Simple thread safety via locking
4.      Double-checked locking
5.      Safety through initialization
6.      Safe and fully lazy static initialization

7.      Lazy<T>
Lest see the example for implementing the Singleton Pattern in C# in depth –
//First version - not thread-safe
// Bad code! Do not use!
public sealed class Singleton
{
    private static Singleton instance=null;

    private Singleton() {   }

    public static Singleton Instance
    {
        get
        {
            if (instance==null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}

Example 2 –
//Second version - simple thread-safety
public sealed class Singleton
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();

    Singleton()  { }

    public static Singleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}

Example 3-
//Third version - attempted thread-safety using double-check locking
// Bad code! Do not use!
public sealed class Singleton
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();

    Singleton() {  }

    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }
}

Example 4 –
//Fourth version - not quite as lazy, but thread-safe without using locks
public sealed class Singleton
{
    private static readonly Singleton instance = new Singleton();

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Singleton() {  }

    private Singleton() {  }

    public static Singleton Instance
    {
        get
        {
            return instance;
        }
    }
}

Example 5-
//Fifth version - fully lazy instantiation
public sealed class Singleton
{
    private Singleton()  {  }

    public static Singleton Instance { get { return Nested.instance; } }       
    private class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }
        internal static readonly Singleton instance = new Singleton();
    }
}

Example 6 –
If you are using .NET 4 (or higher), you should use the System.Lazy<T> type to make the laziness really simple -
//Sixth version - using .NET 4's Lazy<T> type
public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazy =
        new Lazy<Singleton>(() => new Singleton());
   
    public static Singleton Instance { get { return lazy.Value; } }

    private Singleton() {  }
}


What Is MVC Pattern?

The Model View Controller (MVC) is a pattern used in software engineering. It is not a design pattern.  It is a pattern only. The MVC is most popular pattern now.

These patterns separate the applications structure into three parts that are model, view and controller.

1.      Model – The model is responsible for the data access layer and it responds to the requested view and controllers.
2.      View – The view is the presentation layer of the application and also called users interface.
3.      Controller – The controller is the business logic layer for the application and the controller is responsible for responding to user input and performs interactions on the data model objects and also controls to the models and view and controller work between the view and model.
References,
1.      GoF - Gang Of Four
2.      Design Patterns


I hope you are enjoying with this post! Please share with you friends. Thank you!!
By Anil Singh | Rating of this article (*****)

Popular posts from this blog

List of Countries, Nationalities and their Code In Excel File

Download JSON file for this List - Click on JSON file    Countries List, Nationalities and Code Excel ID Country Country Code Nationality Person 1 UNITED KINGDOM GB British a Briton 2 ARGENTINA AR Argentinian an Argentinian 3 AUSTRALIA AU Australian an Australian 4 BAHAMAS BS Bahamian a Bahamian 5 BELGIUM BE Belgian a Belgian 6 BRAZIL BR Brazilian a Brazilian 7 CANADA CA Canadian a Canadian 8 CHINA CN Chinese a Chinese 9 COLOMBIA CO Colombian a Colombian 10 CUBA CU Cuban a Cuban 11 DOMINICAN REPUBLIC DO Dominican a Dominican 12 ECUADOR EC Ecuadorean an Ecuadorean 13 EL SALVADOR

39 Best Object Oriented JavaScript Interview Questions and Answers

Most Popular 37 Key Questions for JavaScript Interviews. What is Object in JavaScript? What is the Prototype object in JavaScript and how it is used? What is "this"? What is its value? Explain why "self" is needed instead of "this". What is a Closure and why are they so useful to us? Explain how to write class methods vs. instance methods. Can you explain the difference between == and ===? Can you explain the difference between call and apply? Explain why Asynchronous code is important in JavaScript? Can you please tell me a story about JavaScript performance problems? Tell me your JavaScript Naming Convention? How do you define a class and its constructor? What is Hoisted in JavaScript? What is function overloadin

React | Encryption and Decryption Data/Text using CryptoJs

To encrypt and decrypt data, simply use encrypt () and decrypt () function from an instance of crypto-js. Node.js (Install) Requirements: 1.       Node.js 2.       npm (Node.js package manager) 3.       npm install crypto-js npm   install   crypto - js Usage - Step 1 - Import var   CryptoJS  =  require ( "crypto-js" ); Step 2 - Encrypt    // Encrypt    var   ciphertext  =  CryptoJS . AES . encrypt ( JSON . stringify ( data ),  'my-secret-key@123' ). toString (); Step 3 -Decrypt    // Decrypt    var   bytes  =  CryptoJS . AES . decrypt ( ciphertext ,  'my-secret-key@123' );    var   decryptedData  =  JSON . parse ( bytes . toString ( CryptoJS . enc . Utf8 )); As an Example,   import   React   from   'react' ; import   './App.css' ; //Including all libraries, for access to extra methods. var   CryptoJS  =  require ( "crypto-js" ); function   App () {    var   data

25 Best Vue.js 2 Interview Questions and Answers

What Is Vue.js? The Vue.js is a progressive JavaScript framework and used to building the interactive user interfaces and also it’s focused on the view layer only (front end). The Vue.js is easy to integrate with other libraries and others existing projects. Vue.js is very popular for Single Page Applications developments. The Vue.js is lighter, smaller in size and so faster. It also supports the MVVM ( Model-View-ViewModel ) pattern. The Vue.js is supporting to multiple Components and libraries like - ü   Tables and data grids ü   Notifications ü   Loader ü   Calendar ü   Display time, date and age ü   Progress Bar ü   Tooltip ü   Overlay ü   Icons ü   Menu ü   Charts ü   Map ü   Pdf viewer ü   And so on The Vue.js was developed by “ Evan You ”, an Ex Google software engineer. The latest version is Vue.js 2. The Vue.js 2 is very similar to Angular because Evan You was inspired by Angular and the Vue.js 2 components looks like -

Encryption and Decryption Data/Password in Angular

You can use crypto.js to encrypt data. We have used 'crypto-js'.   Follow the below steps, Steps 1 –  Install CryptoJS using below NPM commands in your project directory npm install crypto-js --save npm install @types/crypto-js –save After installing both above commands it looks like  – NPM Command  1 ->   npm install crypto-js --save NPM Command  2 ->   npm install @types/crypto-js --save Steps 2  - Add the script path in “ angular.json ” file. "scripts" : [                "../node_modules/crypto-js/crypto-js.js"               ] Steps 3 –  Create a service class “ EncrDecrService ” for  encrypts and decrypts get/set methods . Import “ CryptoJS ” in the service for using  encrypt and decrypt get/set methods . import  {  Injectable  }  from   '@angular/core' ; import   *   as   CryptoJS   from   'crypto-js' ; @ Injectable ({    providedIn:   'root' }) export   class   EncrDecrS