Subtitles section Play video
Welcome everyone
to the session of
Learn
Design Pattern
and architecture pattern
step by step
with the project
And this complete series
we are going to complete
in flat 8 hours
In 8 hours we will be completing
most of the important patterns
and architecture pattern
in this project which we are going to just start
The first question
which will probably come to your mind is that
Is it really possible to cover all the Design Pattern
in 8 hours
and that also in a single project
that also with Architecture Pattern
My answer is no
In this project we will be covering
almost
60% of the Gang of Four Design Pattern
plus some other patterns which are other than gang of four
As well as
we will be covering at least
40-50% of the architecture patterns
It is not possible to cover all the patterns in a project
Because
Design Patterns cannot be forcibly use in your project
But I can tell you that in whatever project your are working at this moment
at least 20-30% design patterns are already available
Via this project my goal is
to give you actual idea of how design pattern and architecture patterns are used in a project
In case you are concerned saying that no I want to learn all the design patterns
and that also I want to step by step
You can follow our video series which have on Questpondvd.com
Where we have explained 40-50
Design Patterns
Which you can see here
Factory pattern, Abstract Factory, Builder, Prototype
Adapter, Decorator
named that pattern
It has been completed in this series
There are two ways of learning Design Patterns
One is if you are a person
who wants to go concept by concept
code by code
here it is
You can see that login to your questpondvd account
see this sections step by step
And the other way is
by doing a project
Take up a project
start coding
the design pattern
and let
fall naturally
along the way
If you are asking me the suggestions
If you are asking me
tell us what is good
Is going code by code
and pattern by pattern by pattern
is good
or the project based approach is good
My personal choice is the project based approach
Start with the project based approach
but as i have told I can complete 60-70% Design Patter
The remaining 30% are still left out
Those 30% Design Pattern you can watch from here
and second
what I'll be doing
when I am teaching you this project I will be telling you to go and see more about the Design Pattern
in this section
I'll be guiding you
at right places
to hop in this series and come back to this project
My advice here would be to go
via the project based approach
and not via Design pattern after design approach
I'll tell you why
Design patterns
are thought processes
and thought processes cannot be explained via just sample code
or
PPTs
or some UML diagrams
If you see on the internet
lot of people explain
Design patterns by showing
UML diagrams
by showing examples like cars,trees and rivers
which does not make any sense
We need to do a real project
We need to feel design pattern
We need to understand it
In what kind of scenario it has to be used
The best way to learn design pattern
is by doing
a poject
I would suggest
you follow this video series
and as time comes I will tell you to go and see the appropriate design pattern from this section here
Before I move ahead
Just talk about one myth which people have about design patterns
Lot of people think that in order to
do a good architecture or create a good architecture we need to implement all design patterns in our project
Also I have seen that some of the developers try to force design patterns
They say lets implement Factory pattern
lets implement Singleton Pattern
Bu the fact is
Patterns come out naturally
They are completely on demand as per the scenario
It is a
very horrible myth that you need to force design pattern or you need to implement design patterns
for sibling your project or implement all design patterns in your project
But the fact is that patterns are natural and tehy should come on demand
Before we move ahead
and we start
defining design pattern
we start looking at the project
Let me spend 2 or 3 minutes here discussing about
difference between design pattern
architecture pattern
and
Architecture Style
I have worked with
so many developers
across different geographical location
with different culture
But when it comes about
differentiating between these three things
People just use this words interchangeable
This three things they must be looking very similar for you
but they have a huge difference between them
When you say design pattern
Design pattern is actually
at a code level
When some says this is a factory Design Pattern
He should actually show you
some kind of a sood of ....
He should should show you some kind of a logic
When you say its a architecture pattern
Its only block level diagrams
For example Model View Controller is a architecture pattern
Where we say there is a controller
then there is a model
There is a view
The first hit comes to the controller and then it picks up the model and the view
Over there we just draw
block diagrams
Its a 30000 feet level view
of your project
When we talk about architecture style
they are just principles
they are just
one liner
For example
rest is a architecture style which follows HTTP protocol
Design pattern
it is at the code level at the sood of code level
Somebody has to give you some kind of a sued a code
then it becomes a design pattern
Architecture pattern
just overall blocks
as I have said model view view model
or UI layer
and then
model layer
then
data access layer
These layers how they are implemented?
What code goes into that?
that is not we have shown here
And architecture principles
it is just principles just one liners
which we need to follow
How do you follow it by using C# or Java or whatever
It is all upto you
Design pattern at a very
code level
Architecture pattern
Its more of 30000 feet level
then architecture style which is just principle
Next time when you say that
this is a design pattern I expect code from you
Next time when you say this is architecture pattern I expect some kind of a higher level diagram from you
Here are some examples of design pattern, architecture pattern and architecture style
Architecture patterns are normally a gang of four pattern like factory pattern, Singleton pattern
Architecture pattern are more block level
which says
model view controller, model view model, model view presenter
And the architecture style is
just style its just one liner principles
For example REST is a architecture style which give importance to HTTP
We have REST
We have SOA we have IOC
These are principles
this are architecture style
Before we move ahead with the project
Let us define design pattern
If you see
the official design pattern
definition on the web if you go and search design pattern definition
everywhere you will find this definition
saying that design patterns are time tested solutions
for software architecture problems
Most of the time this is a definition what is given on the internet
Design patterns are
time tested solution for software architecture problems
After working
15 20 years in IT industries and after doing lot of designing and architecting
I came to one conclusion
Design pattern is nothing but it is
Object Oriented Programming principles
It helps you to understand
object oriented programming problems easily
Let me put
unofficial definition ahead
Because of my experience I am putting this definition
Design patterns
are nothing but they are best practices
or time tested practices
for object oriented programming problems
When you are doing design patterns you will suddenly feel that this is Polymorphism
This is polymorphism + Inheritance
It looks to be encapsulation
As you do
pattern after pattern
at one moment of time you will realize that this nothing but interfaces, abstract classes, inheritance, polymorphism, encapsulation
Design patterns\\
when you start doing practically
it is nothing but
solving object oriented programming problems
in a much better manner
In case your are too worried about this definition what I have given because definitely I am not the official person
to define definitions here
People who actually
created gang of four even they have admitted in their interview
And if you go see this small interview where
one of the person from gang of four Mr. Gamma
when he was a interviewed by Bill
he said that
patterns as a whole
can help people learn object oriented thinking
It can help you to leverage polmorphism help you to design composition, delegations
help you to make a more pluggable
system
It is not just my feeling
my understanding here
It also comes from the main source
they also admit
that yes it is nothing but it helps you to
think object oriented programming problems in a much better manner
Without wasting time let us go ahead and discuss about the project
This project is extremely simple project
In order to make the learning easy
and to not complete things
first go
I have divided this project into various phases
So at this moment we will concentrate just on phase 1
In phase 1 we have 6-7 requirements
Let us concentrate on that requirement and let us try to
architect this system and let us see what design pattern comes up
What is this coolShop project?
This coolShop project is a small software which a company wants to make this company a large retail shopping mall
and it has chain mall in Mumbai and Pune city
And the company wants
a very very simple customer management system for their shops
It has the following requirements
Which they want to fulfill
First requirement number 1
Application
would be capturing three fields
Customer name
The name of the customer phone number
Bill amount
How much is the bill
Bill Date
and the address of the customer
Five fields
this application should capture in Phase 1
The second requirement is in Phase 1
there are two types of
customers which they have included
One is a lead
a lead is person
who comes to the shop
but he does not buy anything
He just enquires
And a customer is a person
Who actually comes and buys things and does a financial transaction
Requirement Number 2
The system at thus moment
should support two types of customers
One is a lead
and the other is a customer
Now
The customer and the lead
have different types of validations the different terms of validations
A lead who comes to the shop he does not buy anything
For him bill amount and bill date is not compulsory
At least he has to give his name and phone number so that tomorrow probably my marketing people can call him up
say that there is discount on something
When it is a lead
only customer name and phone number is compulsory
But for customer
all the fields are compulsory
The system should have a provision
to add new validations rule seamlessly
This is very important
In other words tomorrow if I say
I want to add a new customer type called as gold customer
For him these validations rules are true
System should be flexible enough
to add this new rule without doing lot of changes across the system
That is a third requirement
The fourth requirement is
system should have the ability to add, update and delete customer data
The fifth requirement which is again a very important requirement
For now this system will use SQLServer
and to connect to SQL server it will use ADO.NET as a data layer technology
But tomorrow
The system would probably use new data access technologies like Entity framework or LINQ
This migration from one data layer to other data layer
should also happens seamlessly
We don't have to do lot of changes across this system
that is requirement number 5
and the last requirement is
system should have one ability of cancelling a modification
If the end user is going and
editing a record here
If there are some issues with the
updated value he should have the ability to cancel
This project basically has six requirements
First there are five fields
Second one there are two types of customers
at thus moment with they are having different validations
and tomorrow we should be able to plugin new customers customer type into this
it should have the basic crud functionality
and the fifth point is
basically
we should be able to migrate form one data access layer technology to other data access layer technology seamlessly
This is the phase 1 requirement of this project
In case it is not clear from the video
this requirement you can go ahead and you can download this requirement from questpondvd.com
Its a very simple one pager work document
Where we have put this requirement
You can read through this
Because
the other
part of the whole video will revolve around this project
If this project is not understood
then
you won't love the further class
It is important that you understand this project
Read it once and twice
and then continue with the video
Let us move ahead first and let us first try to identify classes
Let us identify entities
And then as we do the project let design pattern come when it has to come
First thing is to identify entities
What exactly are entities?
Entities are nothing but
they are things what you see in the real world
For example person is a entity
A living object is a entity
city
These things are actually entities
Entities in technical world or in object oriented programming world
we also
termed them as object
In short
Entities are nothing but nouns
A good architect
when he needs a requirement
one of the things
which keeps going at his back end is that
Whatever are the nouns they become classes and entities
You can see from our CoolShop project at this moment Mumbai, Pune, lead , customer this are some of the noun I have identified
Remember that this approach has to be taken very wisely
You can come with
a lot of unconcerned nouns
which has
nothing to do with your software
For example
I can see that there are nouns like the CoolShop or Mumbai and Pune but they really don'y have direct impact in my project
Must be they are some data which I will store in my data base
like a city master or something
but they really don't have impact as such on my code
But you can see like now customer
and lead
definitely nouns which I would be dealing with in my project
From here I am dropping of two three nouns like Mumbai, Pune, CoolShop but these two nouns i.e customer and lead are definitely very important for me
The first thing is
to identify entity
try to first identify the noun
and this approach please do take it very carefully
So that you don't come up with unnecessary nouns which you will
try to create entities off
These object or this entities
needs to have properties and methods
To identify the properties the way to go ahead is by identifying pronouns
Pronouns actually talks more about the noun
For example you can see we have customer name
So customer name actually talks more about the nouns we have customer address that talks more about the noun
Whatever talks about the noun becomes comes properties
and whatever actions
these nouns are doing for example
ordering a product
or asking for a refund
Those becomes the methods of the objects or entities
I am trying to lay down a rule here
Rule number 1 for architects people who are trying to become architects or people who are trying to learn design pattern
Whenever you take the requirement at the back end somewhere
don't has a hard and fast tool but at back end somewhere
you should think about that
your nouns actually becomes objects
Pronounce becomes the property for that object
and actions becomes
or the verbs becomes a actions or the method or the functions
for that object
In order that these entities
these objects becomes live into your computer
you need to write some kind of a code for it
these
code or these logics you write in something called as
classes
Classes are nothing but templates
Basically you write your code into the class and this class is lying into your hard disk
and then you need to insensate this class
and then your entity comes live into your RAM
To insensate
this class we need to use something called as a need keyword
I am sure that
lot of people who have
already done programming
they are aware of what I am saying
Basically if you ask me
Object oriented programming is a three step process
The first thing is
you need to create the class
write the code for it
Then you need to insensate the class
then you can start performing operations on that object or on that entity
Creation of objects
goes through
three phases
When you say that
you want to actually make a entity live inside your computer
You need to actually at least do these steps at least. The first thing is you need to write a class you need to write the logic
for that entity
Second you need to go and use the new keyword and instantiate that entity inside you computer
And the last one is you need to interact with those objects
and achieve the neccessary functionality
Any kind of
object oriented programming problems or design problems which comes into the class creation processes
which is more static in nature
falls into
structural design pattern category
We have a structural design pattern category which will address problems regarding class creation
inheritance
Those kind of structural issues
will come into structural design category
Second one is object creation instantiating
the object
With using the new keyword
any kind of problem associated with instantiation will come in two creational design category
And finally when object starts running they use polymorphism
and they do type casting
etc
On runtime if you have any kind of architectural problems
It will come into the behavioral category
You can see
As I have said previously also
design pattern is nothing but it is object oriented programming
It solves problems around object oriented programming
By this view here or by this image here
you can easily make out that design pattern will revolve around classes
objects
and instantiation
Now that we have analyse the requirement
When we read the requirement
we use the noun pronoun
and the verb approach
to
to conclude entities
And we said that we have two types of entities
One is a customer
and the other one is a
lead
Go ahead and create a class library
and
let us try to create
the business layer
the business layer means
the library which our in which our business logic will reside
I am going to create a class library a DLL
Let me name this project
as customer
project
Phase 1
This is a phase 1 of the project
This class library which I have created at this moment
I would like to go and
name this
library as
The library where my business logic will reside
Let me put a space here so rather than just putting customer project phase 1
I would like to name this namespace as my middle layer
Lot of people can say this is the middle layer
some people term this as the domain layer
Some people will term this as a business layer whatever layer you want to term this as
but this is the layer where
you business logic will reside
As we said we have two kinds of classes
At this moment one customer class and the other one is
alead class
We said that there are five properties which we have identified by using the pronoun approach
We said that we need the customer name
we need the phone numbers
we need the bill amount we need the bill date
we need the address and so on
You can see all of these five properties here
and also
For the lead also we have the same kind of properties
We said that lead customer are different types of customer
Actually the customer is who actually buys it
While lead kind of a customer who actually just comes and enquires about it but he actually does not do a transactions
I do understand lot of people would be screaming over here
Saying that you have duplicated all this properties you have to move it to the common class and so on
Believe me I am going to do that
Best way to learn programming
or the best way to learn architecting is by doing mistakes and then gradually improving top of it
Let me see that how I can improve on it in the later stages
In that way you can also see this architecture evolution
We identified the entities by using the noun
strategy
we identified the properties of the entities by using the pronoun strategy
We identified
the actions
by using the verb strategy
Now it is time to identify the relationship between these entities
Primarily
there are two kinds of relationship between entities
One is a relationship and other one is a Using relationship
or has a relationship
For example you can see on your video there
Shiv is a son of his father
It is more of a parent child relationship
While shiv uses a car or shiv
has a shirt
It is more of a using relationship
If you look at a requirement
in the requirement it is clearly said that lead is a type of customer with less validations
I would suggest you to go and read the requirement again
It clearly says that lead is a type of customer
In other words lead and the customer is having a kind of a
parent child relationship
So both of these can be inherited from a common class
So let us create a common class here
I am going to create a common class here called as
CustomerBase
This is a base class
It's a class which will have all of these properties overhere
And both these classes i.e the customer class will inherit from the customer base
As well as the lead class will inherit from the
customer base
So both them will actually inherit from the customer base class
Also we had one requirement regarding validation
We said that when it is a
only a lead then only customer name and phone number is compulsory
but for a customer
all the fields are compulsory
The difference between the customer and the lead
is in terms if validation
In the customer base let me go here and define
a method here
saying
validate
This method will
help to validate customer
This method would be overridden by
the customer and the lead
as per there requirement
I am going to make this method as
virtual
By defining a method as virtual
your child classes for example you can see over here I have the customer class
The customer class can go and override the validate method
And he can put all the validations here
While
the
lead class will only validate for
customer name and phone number
In the base class
over here I will
through a new exception saying that
not implemented
In other words this will be implemented by the child classes
In the customer
all the validations will happen
In other words here
The customer name is compulsory
If the customer name is length=0
then I will go and
throw a new excpetion
saying
customer name is required
and in the same way
I have put validations for all the other fields as well
You can say phone number is compulsory
bill amount is compulsory
bill date cannot exceed today's date
address is compulsory etc
For the lead
only the name and the phone number is compulsory
We have
the
customer and lead which are actually concrete type
and they are inheriting from the customer base and then they are overriding the validate method
as per their requirements ans per their need
Create a user interface
In the user interface let us consume this business logic what we have created
This is actually our middle layer
Let me go and
rename this
Also I would like to mention here I am creating this project in visual studio 2010
Why am I creating at the backwards version
people who are using 2012
2013
and 2014
or must be 2016 tomorrow
You can easily migrate this project and see the code
I am trying to create a version which is five years back
So that you can
just do a next next wizard
and see this code
Lets me also rename this class here like Customer.cs
Add a new project
Let me add a windowsForms here
I'll say winform
Csutomer
and there is a form let me rename this form to a nice name
Actually I am creating windows UI
to consume this middle layer or
the domain object
what we have created
At this moment UI for me is irrelevant because I want to ensure that my middle layer and my data access layer are properly
reusable
Bu t I do need a UI tested
So I am taking the winform
feel free to use other user interfaces like ASP.NET, WebForm or console applications etc
I'll say here frmCustomer
and on this form you can see I have
put all the necessary User Interface which will help us to fill data into our domain
object or into our customer class
Here is my UI
w
I am going to consume this middle layer
inside my UI winform customer
I am going to consume
this middle layer
inside my winformcustomer
here
overhere I will
say
using
middle layer
We have two types of customers
one is a lead
and the one is the
a simple customer
In this UI I have created a very simple combo box
I have a simple
drop down i'll say rather
and this drop down helps me to select
what kind of customer
I want to use
If I select lead and if I do validate
then accordingly the lead validation will fire
and if I select customer then customer validations will fire
I'll go back to my UI
Depending on situation
I'll say here
Private
custoemr
either this UI will go and create a customer object
or probably it will create
a lead object
In this
selected change event
if
the CustomerType.text which is selected
if it is customer
then please go ahead and create the object of the customer
lets create the object of the lead
I'll say here Customer
or else go and create
the object of lead
In the
validate button
again here also depending on what is selected
I will either call the validate of the customer
or I will either call the validate of the lead
There is one big problem
with approach at this moment
Before I talk about the problem
Let me talk about
What is a sign of a good software architecure?
If you want to see
or if you want to test I will say that
a software architecture is good or not
He should see that how software architecture reacts when changes happens
Whenever a change happens in a software
and we have to change at ten places
then there is a problem with the software architecture
If you see at this moment
Tomorrow if I go and add a new customer type over here
Let us say I go ahead and I add
a new customer class
called as gold customer
Then look at the places where I have to make to changes
First thing change number 1
I have to add that new type here
in the form
second add in my combo selected event
I need to add one more condition that in the validate event I need to add one more if condition
I have to make changes at three places
and please note
at this moment this is a very very simple form
If I have 10 such forms like this
You can think about what kind of changes can happen in my system
A good software architecture is tested when the changes happen
and if you are changing in lot of places
that means you have not architected your software well
First thing is let us see that if we can minimize this changes from
atleast
three to one
Can we bring down these
number of code changes
I can use polymorphism
In other words if you remember
both of our classes i.e customer and lead
inherits form the customer base class
I can create one reference for the whple UI
This is customer case and depending on the situation
Customer base can now point towards
new customer or he can point towards
a lead
then in the validate I can say cust.
validate
If you see now
When I add
a new type
I have to only go and change in this section
Because of polymorphism
This is polymorphism
wherein the parent class can point toward his child classes on runtime
What do you mean by polymorphism?
Polymorphism means
change as per situation
My custobject is actually have customer base type
and depending on situations it can point to customer or it can point to to a lead
object
One of my personal belief is that
the biggest gift
form object oriented programming is polymorphism
If you see polymorphism in our real world also in our real life
Polymorphism means depending on situation you change your self
for example
At thus moment what I am teaching you
I will not be talking about my family work so I am teacher at this moment
But the time I
go and meet my kids I play with them
I don't about my office work
At that time I am dad and I just want to ensure that
I don't talk any kind of official work
In real life
people achieve decoupling
decoupling means
whatever happening in your office you don't take it home
Or whatever is happening in your home you don't take it office
when you actually
do polymorphism
And the same here
object oreinted world also
polymorphism is the biggest gift by which you can achieve decoupling
by which you can ensure that when change happens at one place it does not
go all over the places
Here also by using polymorphism
at this moment I am using polymorphism
and because of that I have minimize
my changes from three to one
Still i have to make changes
In other words if you see now
If somebody goes and add as a new type
First thing is he has to go and add
The class overhere
After that he has to again go back and add
A new if condition here in this combo change
Think about it if you have ten screens like that then you have to go and do it at ten places
Somehow we have to also get
rid of this
if condition as well
form this UI
In order to remove this if condition from here
We really need to do is we need to get rid of this new keyword
Remember what is the whole goal
of this exercise we are trying to do removing the if condition and removing the strongly types classes or concrete classes
The goal is that when I go and add a new customer type
I should not be making changes here
If I do want to make changes in my UI
Then I need to get rid of these final classes or concrete classes
In order to get rid of these classes I need to somehow ensure that
the creation of these objects goes into some central class
In other words I need to
give this object creation
to some other library
I am going to add
a new class here
or a new library
Who will take away this object creation process from here
into that
class library
I would like to name this class library as
factory
why factory?
because it creates things
I am going to add
a new project
And I will say this project name as
factory
factory customer
In this
let me go and create this class as a static class
This is my factory
This factory class will be responsible for my object creation
I am going to add a reference here
I will say
public
create
create the customer
depending on
the type of
customer
I will move this if condition from here
to this
factory class
Over here I will say if
the type of
customer
If the TypeCust is the customer
return
a new customer
or else return
a new lead
This class here
this factory class here
is taking all the responsibilities
of creating the object
He is aware of all the strong type object
If you add a new type he will just go and add one more if condition here
But when he returns this strong type
he will always return it as the parent based class
The base of decoupling is polymorphism
Tomorrow if I go and type
He will always
go and return only customer base
My UI now
can become something like this
I can say here
My UI will now go and refer
the factory customer class or the library
I will say here using
factory customer
and then in the customer type
in the customer type selected event
of the combo box
I can say here
Please use the factory
and
create
the object
In my ui
I have no reference
of
the
customer class
or the least class must be I can search for the lead
There is no reference of the strongly type classes
By using this simple factory pattern
We are able to decouple
the user interface from the strongly type classes
If somebody goes and adds a new customer type here
He has to just go and add that if condition here
and that's it
All other
consumers
It can be a UI
It can be a batch process
It can be anyone
They don't have to worry
what kind of type is coming from the factory
The factory takes the responsibility of creating the object
There is one serious problem with this factory class
And the problem is the
if condition
Somehow I have to get rid of this if condition and we need some better approach here
Now the good news is
if you have polymorphism in action
then you can get rid of the if condition
If you have polymorphism
and if you see lots of if conditions and case statements
that means polymorphism has not been exploited to the full
Over here
I can delete this complete of condition
I can create very simple
collection here
of customer base type
go and do something like this I can create
a collection
base type of dictionary
CustomerBase
= new
dictionary of customer base
In the constructor
I can add
the newly
the strong types
or the
concrete type
This is
new customer
and this is
for lead
We can go ahead and
return the
customer type by just doing a look ups so
we don't need now the if condition I can say here return
custs
This custs
is a nonstatic variable
Inside the static methods you can only
access
static variable
We need to make it static I'll say cust
or type cust
and returns the type
We don't need any more if condition
It can be just
removed by simple lookup
The lookup will work why? because polymorphism is happening internally
Basically he will do a lookup
It will give out customer
that will get automatically type to a
Customer Base
Its a same kind of
output there are some errors let us see what errors are there
Let me first rebuild the solution let me see what errors we have
Static classes cannot have any instance constructor
This has to be static
Rebuild again
Let me do a F5
If I do a lead
It goes
it looks up the collection
give me back
lead type
If I do a lookup of a customer
There is a customer again does a look up
It goes looks up and it gives me a
customer type
We have a factory class centralized
all the new key words are centralized there
We successfully got rid of that if condition
This getting rid of this if condition by using polymorphism
Is termed as
RIP pattern
RIP means replace
if with
plymorphism
This centralization of object creation
is termed as
factory pattern
There are two kind of factory pattern one is what is discussed by gang of four and this one here
is a simple factory pattern
This factory here
Is termed as a simple factory pattern
All over the project
just put this word design pattern
I will put the name of the pattern
Design pattern
and
name of the pattern
If you want to see
if you do a Control+Shift+I
and if you just search for
design pattern
You should be able to state forward go and see the reference
This factory class
can be improved in terms of performance
If you look at this moment
this customer collections is loaded irrespective you wanted or you don't wanted
I want that
the customer type should be loaded
only on demand
When somebody calls this create method at that time
the customer collection should be loaded
Control+X this
I will remove this concept and here I will say
if
the custs
.count
= 0
then go and load the types
When somebody calls the create method
At that time only the customer types are loaded
or else they are not loaded
without any reasons
This pattern is termed as
lazy loading
One more design pattern here
Lazy Loading
The opposite if Lazy Loading
can be termed as
eager loading
Lazy Loading means when the objects are needed they are loaded
or else they are not loaded
Three
Design patterns we have covered at this moment
Factory pattern
Replace If with polymorphism
and
Lazy Loading
I would like to make a statement here
One is you know design pattern
The other is you should know the automation for the design pattern
It is very important
that
if you have automation for the design pattern
You should be using that
and not actually coding from scratch
For example, for Lazy loading
in c# we have a keyword called as Lazy
Rather than writing such kind of a If condition and then
checking it and then loading it
You can just use that Lazy loading keyword
I would give this you as home work
We have a video in questpondvd
which talks about the lazy keyword
Use that lazy keyword
and replace this If condition of the lazy loading pattern here
One is
You should know the pattern and the other is
in case there is some ready made framework or some ready made component
try to use that component
Because components are time tested you don't need to reinvent the scratch
Take this as a home work
Go ahead and replace
this
if condition or lazy design pattern
by using the lazy keyword of C#
Let is quickly test our application
In the validate
We are already getting the type
Create a method here saying set customer
this method will say cust.
customer name
= txt
customer name
.text
cust.
PhoneNumber = txt
Phonenumber.text
This method here
actually goes and takes a value from the UI
and sets to the object
Before I call the validate I would like to call the customer here
Let us quickly go and
test this
Basically if he is lead
then customer name is compulsory
If he is a lead then
customer name is compulsory
If I put the customer name
phone number is also compulsory
Then address is not compulsory
Its is working bit the time I say this is a customer
then even
the bill amount is required
it cannot be zero
Even the address is required
Depending on what kind of
type I am selecting here
He is calling the factory
factory gives him that type and depending on that type the validations are happens
If I go tomorrow and add a new customer type I don't have to make
any change in the UI. In the UI I just have to add that type over here
And only what I have to change is in the factory class
I need to go in the factory class and just go ahead and add the new type into my collection here
This brings us
to this
One hour of
Learn Design Pattern Step by Step
In this one hour believe me
you have literally cross the sea
I am not teaching you design pattern
step by step
I am not teaching you design pattern academically
I am teaching you design pattern practically
Once you see this example you will never forget how to use design pattern in your project
tomorrow
First we start it with design pattern
we defined design pattern
and we said that
design pattern is nothing but learning object oriented programming in a better way
they are time tested solution
for object oriented programming problems
Then we said the design pattern falls into three categories
One is the structure category
at that when we build our classes when we write our entities
Any kind of problems that occurs during that phase
will be solved by using the structural design pattern category
There is a second category called as creational pattern
which revolves around the new keyword
then we said there is something called as behavioral pattern
which talks about
the problems around dynamic nature of objects
After that we also defined what is a different between design pattern
architecture pattern
and architecture style
Then we moved ahead and we identified
the entities and the objects and the classes by using the noun
pronoun
and the verb
approach
After that
we identified the relationship between this entities
We said there are two kind of relationship
One is a is a relationship
and one is a has a relationship
One is using inheritance and the other is using
aggregation, association, composition
It is more of a using relationship
We went ahead
we created the UI
We created the classes and properties we created the UI
then we said
we are having
problems with the new customer type. So we said that if new customer types get added
then we have to make changes across all over
We went ahead and we created the simple factory class
This simple factory class is nothing but it actually collects all the new keyword into a centralized location
Afterwards we moved ahead we said that this simple factory class is looking good
But we need to replace that if condition
It had a if condition and that if condition will increase as new types gets added
We replace the if with polymorphism
That is a second pattern which we implemented
with
how about improving the performance of this factory class
Rather than loading the objects
How about loading them in demand
We implemented the
lazy design pattern
We have more
seven hour still pending
Let me talk about what I am going to cover in the next coming hours
In the next coming hour
I will be covering
something called as strategy pattern
Because if you look at this moment
The validations are
Coupled
tightly coupled with the entities
We will look into that how to use strategy pattern
and improve on this validations
We will also see
how we can go and automate
the factory pattern
the simple factory pattern class
At this moment if you see we are writing a collection then we are doing a lookup and what not
But in reality if you think about it
If you want to create
lot of these factories like this
You would end up into lot of custom coding
As I have said in this class also
one is you know the design pattern
and other is you know how to automate that design pattern
What we will do in coming hour
we will automate that factory class we will try to remove that collection
We'll
try to remove that lookup
And third
we will also implement some simple pattern like
prototype pattern
and memento pattern
The next coming hour is
more exciting
it is more adventurous
We will be learning all of this design pattern practically
and not just theatrically
Don't forget in case you want to learn pattern by pattern
Please see this video series where we have explained pattern by pattern
You can take a one pattern
we have showed sample code for it
You can understand it
There is a second pattern
Third pattern and so on
If yo want to learn pattern by pattern go through that video series
If you want to really see patterns happening then this is the video series which you have to go through
Thank you so much for bearing me for 1 hour
If you really like the series then bear me for the next coming hour as well
So seven hour still pending
If you don't give up
then I will also not give up
If you have zeal to learn design pattern
actually and practically
then I have the zeal to tech you
In case you like the series
In case you like this 1 hour what you have learnt
Please go to facebook.com/questpond and say that I have completed 1 hours I liked it and waiting for the other series
Or else if you have any issues and concerns you can also raise those concerns on the facebook page
Thank you so much
Let us complete the seven hours and let us become a real real architect
And let us become a person who knows design pattern actually
and not just theoretically
Thank You So Much