Design Patterns Interview Questions and Answers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there welcome to this Java interview guide from in 28 minutes in this series of videos we are covering a lot of interview questions on different java topics as you already know in 28 minutes is famous for all its interview question videos we have interview question videos on lambda expressions multi-threading collections exception handling and also we have a lot of different videos on Janet's design patterns eclipse mockito spring MVC and stuff in this series of videos we are covering interview questions on oops advanced java servlets jsps design patterns JDBC j2ee maven and Eclipse all the information about different questions that we would discuss is on the github repository github.com in 28 minutes welcome to this video where we are discussing about the interview questions on design patterns we are on the github repository in 28 minutes interview - guide so you can find this in the link in the description as well and we are in the section on design patterns so over here so if you look at this thing like among the resources is one of the full videos on design patterns this kinds of covers the entire things around design pattern so if you look at this video if I can get through the ad as you can see this is a very popular video on YouTube I mean this is the new version the old version had almost 1 lakh views and now this version of the video we have created recently it's about 158 views but 8 likes already and as you can see we have a lot of subscribers as well so yeah this video is really a popular video if you want to understand design patterns in detail we would look at all the design patterns in detail in this particular video the other resource we have is a presentation this is a presentation on the Java EE thing so if you look at it this is called demystifying Java II where we go through all the different things so web layer business layer what are the different things which are possible MVC view web services we look at it from the AG Arctic perspective so we look at what are different layers what are the different frameworks that you can use in the business layer we use eg B and spring and if you look at the typical architecture in the data layer we use hibernate I bet is JDBC and things like that and we have the integration layer we have JMS MQ and web services and things like that and also we looked at we look at the different Java EE up architecture patterns so the our creature patterns then we have different things which different important principles related to design and then we have patterns which are related to each layer like friend control or application control or intercepting filter yeah business layer as well as integration layers so there are a lot of things that we cover as part of this particular course I mean it's it's a huge course it's it one of our favorite courses Java EE design patterns if you are interested in that particular course then you can actually go and find the link in here so this is right beneath the design patterns thing so you would find a link Java EE patterns this would be a huge discount code for you so you can go ahead and try that so in this video the attempt is to cover some of the important examples around design patterns so what we'll do is we'll cover a few examples around factory pattern prototype pattern singleton pattern adapter decorator and things like that so let's get started we will use the presentation which is present in here java design patterns so this is presentation I'm going to write enough this is third link in here java design patterns that's the one which i am loading up it's loading from github so i am not on a fast collection network sites little slow let's wait for it to download typically one of the quick one of the question which is asked around design patterns is what are the three types of design patterns yeah it's very simple right so three types of design patterns are creation and structural and behavioral okay before that what a design patterns basically like I have 15 years of Java experience there are people who have 25 years of Java experience and there are others who have hundred elec probably are working on Java from the time it was created so basically I mean also there are things like object-oriented programming right object-oriented programming I mean there are there is objected entry programming even before Java so with object-oriented programming there are certain patterns which a few people found that they are good to follow so in their experience they realize that when things are done in this way it helps problems in this specific context so those are what are called design patterns there are four people called the Gang of Four came up with the set of design patterns they said okay given this set of context given this context this is a prop this is the solution in this context this is the right solution that is what are called patterns and this set of patterns are called design patterns and we have three such kind of design and three such categories of design patterns and here we are restricting our discussion to just the object-oriented design patterns I mean there are other patterns how do you develop Java EE web application and things like that as far as the design patterns related to object-oriented programming a concern there three part three kinds one is creational that deals with hub you create objects that I use a single ton whether user prototype whether user flyweight so those are creational patterns have you create objects so object-oriented programming is all about objects right so how do you create objects the creational patterns deal with it how do you organize your objects whether there is an inheritance relationship there there is an association relationship between them how do you structure your objects that's your structural design patterns they deal with the structure of your objects how do you organize different parts what are the member variables how is it structured that's your structural design patterns and behavioral patterns are just related to the behavior of the objects runtime behavior have the objects interact with each other now that's basically your behavioral patterns so when it comes to the creational patterns the best examples are prototype pattern builder pattern singleton and the factory pattern factory pattern is typically used when I have like different kinds of objects of a similar type and I don't really want everybody to start creating objects using new in that kind of situations I would use a factory to allow to provide a single static method of I'm talking about static factory method so you provide a method which would I love you to create an object take the inputs create the object of the specific type you need and written back so the external people will not know anything about what kind of object that you are creating so then singleton singleton is one of the popular design patterns I would say so singleton is one object per JBM so for that particular class you can only create one object in a j-1 JVM that's basically what is called a single turn so there are multiple ways of creating singleton one of them is using a private constructor and create a static method in there and check whether there is that instance already there if you if there are that instance is already created you create return a reference of the existing one and the other patterns which are important are the Builder and the prototype patterns let's look at a few quick examples in Java for those so factory method the good example is the calendar dot get instance so if I want to get create a calendar instance all that I need to do is calendar dot get instance so this is a factory method similarly the care set in the new i/o so that's a good example of the factory method the prototype is basically the object or clone is the best example for prototype so you have an object already you create a copy of it by using the clone method so that's your prototype design pattern single turn like your run time dot get run time or the system dot get security manager these are really good example for your design creational design patterns let's quickly move on to your structural design patterns the structural design patterns are related to hub you organize your objects so a proxy decorator facade adapter flyweight these are all called structural design patterns let's quickly look at some examples of the structural pattern so the first one is adapter pattern adapter pattern is basically from one format to another format so for example here arrays dot as lists so you have the arrays dot as list method which converts an array I mean a list of items into a list that's a good example of a adapter pattern so you can use a list of items I mean you can have a set of items and you convert them into a list that's a very good example of an adapter pattern and also the typical unmarshal and the Marshall method so the marshaling of the thing would convert the object from XML and the unmarshal is the reverse another good example of these structural patterns is the flyweight pattern flyweight pattern is typically used for example as in a chess game you have an initial set of pieces which you want to create for everything so what you do is you'd have that ready and you'd kind of reuse it again and again similarly a flyweight pattern is used in the integer class so the value of method for a int to crater in integer it actually tries entry uses whatever values which are present so if the value is between 1 and 28 it reduces whatever object is already present so that's a good example of the flyweight pattern next let's quickly look at a few behavioral patterns the behavioral patterns we would want to look at our chain of responsibility command iterator strategy and template method the chain of responsibility is basically have the exact exception handling works so let's say a like I am calling method this method 1 which calls method 2 and method 2 calls method 3 and method 3 throws an exception when a method 3 throws exception the if there is no try catch block in the method 3 then the exception is goes to the method 2 and it checks is there a try-catch block and method - no it goes to method 1 so this is basically what is called chain of responsibility where you have a sequence of things so just like the approval process right so if I am not having the rights to approve something I'll send it to my manager and his manager and so on similar to that in exception handling if a method cannot do exception handling then it gives it to the calling method - its mod calling method - its mod calling method and so on so that's exception handling another good example of that is the servlet do filter method so if a servlet is able to it will process it and it will give it to the next thing which can filter it so that's a very good example of the chain of responsibility thing a command pattern is basically half the entire request response cycle worked so when a servlet if there is a servlet the servlet gets the input as a request so all the data in the HTML page or in the form gets encapsulated it creates translated into a form and the browser sends that form out to this server so this form whichever is created is the command object this is an example of the command pattern what the servlet does it takes this command object as the input it creates a response and returns that back so it's a very good example of the command pattern the other good example is creating threads using runnable interface so implement or specific method called run and you define this is what needs to be done so whenever somebody calls the start method on that particular thread Java knows that the run method needs to be called so the all the logic that you need to do is encapsulated as part of the run method so this is a good example of the command pattern the other thing is I traitor I told is this commonly used to loop around collections right so we don't want to understand how the collections are internally so whether it's using an array or whether it's using a linked list I don't really want to mind about it I want one way of looping around them and that's what the I traitor provides it provides a way of looping around all of them the next pattern is strategy so basically strategy pattern is using like different algorithms so I would want to be able to separate the algorithm out two separate things so that I can use different algorithms on the same problem one of the ways Java implements that is by using the comparator so when you have a collections dot sort you can pass in a comparator so you can pass in the algorithm that you would want to use do the sorting based on as an input so this is a good example of the strategy pattern you can say I want to sort these by using this logic so the logic is outside so you kind of separate the logic out from the sort method what the sort method does is given a logic it can sort based on that the other pattern that we want to look at is the template method pattern so the template method pattern is typically used in all the abstract methods so you have a lot of non abstract methods in abstract list abstract set and abstract map this all use the template method pattern and the good example of the template method pattern is the abstract controller in Java so kind of defines the pattern of how things has to be done and it has a lot of abstract methods to implement the details so that's basically example of a template method so the idea behind this particular video is not to clearly make you a expert on design pattern but just to give you an overview if you really want to become an expert on design patterns we would really recommend you to look at the full video on design patterns and just go through the entire thing and a do our entire course on Java EE patterns I think that would be a really good step for becoming an expert on the design pattern the idea behind this was to quickly take you over all the different kinds of design patterns on the object-oriented side kind of give you quick examples on some of them so that you can get up overview of them in about ten minutes so I hope we are successful in that effort this video is part of a series of 25 videos which we have created and all the links to those videos are present in the github repository github.com slash in 28 minutes slash interview - guide so you can find the link in the description of the video as well and you can watch all the other videos as well one of the facts that we really believe at in 28 minutes is all these interview guides are useful once you are at something or once you have a little bit of experience with that so there are a lot of courses that we have java ee patterns spring MVC JSP servlets maven these are all really good courses we also have a complete 5 our course on Java interview questions and answers' it's almost excessive videos on different things related to Java so those are the things I think you will find interesting after watching this video and if you liked it I am sure you'll like the other courses as well so give them a try see if you like them you can make the best use of this git repository which is present in here good luck with all your interview preparation until next time this is bye from Rangga at in 28 minutes
Info
Channel: in28minutes Cloud, DevOps and Microservices
Views: 85,715
Rating: 3.8284228 out of 5
Keywords: in28minutes tutorial, design patterns in java, java interview questions and answers, design patterns, java interview questions and answers for experienced, java interview questions, java design patterns, design patterns interview questions, in28minutes, Design Patterns interview questions and answers, Design Patterns interview questions
Id: W9gz3NGeojU
Channel Id: undefined
Length: 15min 51sec (951 seconds)
Published: Mon Sep 26 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.