1. Home
  2. Explore
  3. Computer Science
  4. Design Patterns Introduction

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Design Patterns Introduction

Transcript

hello everyone and welcome to this talk on  design patterns now in this first video I  want to introduce the concept of design patterns  um see what they are and what they are useful for   and also why you might want to use them in your  coding and in your application or project so what   are design patterns design patterns are basically  ways of solving similar problems in similar ways   okay so during our development um pretty much no  matter what language you develop on you will run

into kind of the same issues and you might want  to implement the same Solutions okay so design   patterns gives us a way of fixing or solving a  problem in a kind of accepted standard way we know   that these Solutions work and we want to use them  to implement them in our code so design patterns   will allow us to do that they will allow us to um  have the certainty uh to know that our solution   does solve the problem but also introduces some  other things that we might want in addition to

Simply Having a solution whatever that solution  may be so these things might be uh scalability   where your application or code might be able  to grow um when you add further functionality   and further um code to it so scalability is  one thing testability is another we want our   application or project to be tested in a simple  way and we can introduce unit tests and more   complex tests okay so design patterns um allow  us to do that it also introduces separation of

concerns okay so that means um our code is split  into kind of blocks of code or components that   each solve their individual problems and this  in turn leads to better architecture and better   um you know scalability maintainability things  like that so that is at the core that is what   design patterns are in addition to all that  they introduce standard terminology okay so   we want to be able to discuss uh the solutions  to our problems um in an easy way and speaking

to let's say developers or um Architects um about  your code about your project using design patterns   um provides an easy way to kind of communicate  what you have done so for for example if you say   that you have implemented a biller pattern for a  particular class then the person you're talking to   will automatically know what you are talking  about they introduce best practices so that means um what are the most efficient ways the best  ways to solve certain problems in your code um in   software development we often have one right way  to uh fix to solve a particular problem and many   kind of wrong ways even though the solution might  work you might not get things like uh separation   of concerns if you let's say put everything in  one code right so that would be an example of

a um wrong way to solve a problem design patterns  introduce best practices the best way uh that we   have found to solve particular problems now  I want to introduce this term you might have   heard of it the gang of four the gang of four is  basically a group of four um software development   Architects um that have put together the book uh  the uh foundational book on design patterns okay   and this is what this course is kind of based upon  um they describe based on their experience they

describe the best ways to solve a lot of problems  and come up with some solutions to common problems   okay they have named these Solutions design  patterns um they provide a number of them that   we will cover here in this course okay so this is  what it's based on um there is a book out by the   gang of four so if you want more information um  check out that book as well all right so that's   it for this introductory video thank you for  watching I will see you in the next one all right

so the types of design patterns now of course we  have different types of design patterns depending   on what we want to achieve with a particular  pattern okay so in this video we're going to   go through the three uh types that that we will  cover in this course the first one is going to be   the creational type of design patterns and the  creational design patterns basically relate to   how you create specific objects or specific  components in your applications okay so why

do we have this well we have different ways  of creating it creating objects or components   depending on the requirements we put on those  so you can imagine for instance a class that   has uh multiple parameters we want to let's say  instantiate some parameters and not others um and   we want to do this in an efficient way to provide  the quote unquote user so the color object um   with a lot of options and a lot of possibilities  and this is what where for example the Builder

pattern would come in okay so um different ways  of creating objects or creating components in a   way that achieves the nice to haves that we talked  about the second type of design patterns is the   structural and here we talk more about how we kind  of structure our whole project how we structure   our application um to achieve certain objectives  this is sort of related to architecture although architecture is kind of a a broader view um which  relates to components and which components um are   responsible for which uh actions here in the  structural design patterns we take a kind of   closer view um closer to the individual class or  component and see how that component is structured   um in order to again achieve some nice to Hales  like testability scalability and so on and the   final type of design pattern is going to be the  behavioral and the behavioral pattern relate to

how the components that we create behave in our  application and interact with each other okay   so here is where we will see um how we communicate  between components based again on these structural   types of design patterns here we see how those  components that we have created kind of interact   and communicate with each other and here we will  have quite a number of design patterns that we   need to go through but all are quite useful to  know and uh I think you will use most in your

development and in your projects okay so this is  kind of of a broad view of what types of design   patterns we have and in this course we have split  the uh structure of the course we have sections   for each individual type okay so firstly we  will talk about all the creational patterns   um and we will begin that in the next section  okay so thank you for watching this video and   I will see you there okay so let's get into it the  first design pattern we're going to talk about is

the Singleton now if you've heard of one design  pattern this is probably the one you heard about   the Singleton is probably the most well-known  and the most talked about design pattern both   in a positive way and in a negative way it's  not necessarily the most used one um or the   most useful but just that people uh do talk about  and know about the Singleton and we're going to   cover the positives and negatives in this video  so let's take kind of a thought experiment um to

understand why these Singleton might be useful so  let's say we have a project with three components   and these three components need to access some  external resource for example an API um on a   network on the internet so a naive solution to  this would be to have a network Communicator we   instantiate that Network Communicator every  time we need to access that resource so for   example for component one we have its own instance  component two has its own instance as well and we

communicate with the resource in this way now  there are a few problems with this um so first   of all this is kind of resource inefficient so you  might be um on a uh you know a smaller device that   doesn't have enough resources or you might be  on a server where you have a lot of um access   and you want to conserve your resources in this  case creating three instances that do the same   thing um that is not a very efficient solution  also you might want to have some sorting some

ordering of requests in this communication okay  so um you might want to serve each request as it   comes in and order those those requests if that  is important okay also for testing purposes this   is not very efficient because you cannot um you  shouldn't really test multiple instances of the   same thing so a better solution would be to have  a Singleton which is going to be a single instance   of this network communicator and then all the  components that require that functionality will

access that Network Communicator okay so this  is kind of the idea behind the single turn put   a central resource in a single instance of that  resource rather than duplicating it multiple times   okay so only one instance it's a single point of  access for a resource and we've taken the example   of a network communicator but we might have for  example a database or a logging system or things   like that where you want a single resource okay  so here are a few examples but of course it's not

limited to that now as I mentioned there are some  disadvantages to Singletons and people are often   um quite against this pattern the main ideas the  main reasons why people don't like them are first   of all it breaks the single responsibility  principle which means that um a Singleton   will basically manage its own State and will allow  others to create only one instance and this is not   ideal this is often a code smells quote unquote  code smell which simply means that a particular

class should um create a component when required  whereas the Singleton creates its own instance   okay so this is not ideal also testability issues  might come in since the Singleton kind of handles   multiple situations multiple cases multiple  components will access that Singleton by its   very nature where you have a single instance  of a component that means you couple tightly um   the components that need the Singleton with the  Singleton instance okay so this kind of reduces

testability you cannot really mock a singlet ton  in a test since you have only one instance and   you need to um you know kind of get around that in  some way also um you have a state for the lifetime   of your application okay so once the Singleton has  been instantiated you can only use that particular   instance of course you could Implement a solution  where you kind of kill that instance in some situations and recreate it but that would break  the principle behind the Singleton okay so that   means once a Singleton is instantiated at you have  your state for the lifetime of your application   and that causes some issues all right so enough  with the theoretical introduction let's go to our   editor and start to implement a Singleton in our  code now here I am using py charm for development

but you can use any editor you like and I've just  simply created a new project here okay so let me   just delete that file all right now we're going  to let me just create a python file we're going to   implement the Singleton in two different ways the  first way is going to be a naive implementation   where we Implement a Singleton that works but I  will show you a particular case where it doesn't work to kind of um tell you straight away what  the issue is U it works in a single thread but   not in a multi-threaded environment okay so we're  going to implement this naive implementation and   then we're going to see what the solution is and  how we can fix the multi-threaded implementation   okay so first of all I'm going to have a naive  Singleton so let's implement this naive Singleton

I'm going to have a class with a singl ton and I'm  going to have a type here okay I'm going to have   underscore instances is going to be this empty  dictionary and I'm going to override the call   method So Def uncore underscore call and I have  here self ARs and KW ARS now um I'm going to say if um self not in self doore instances  so it if we don't have that instance then   we will create it so instance equals super  do call okay okay here we have arcs and KW

ARS okay and then we're going to have self  doore instances of underscore uh sorry self equals instance okay and outside  my if I'm going to say return self do   instances of self so what I'm doing here  is I'm checking does this instance exist   and if it doesn't then I create it and I add  it to my dictionary if it does exist then I   don't need to create it again and I simply  return it okay so that will just create um

a single instance for my Singleton class okay  I'm going to have here a class Network driver   with a meta class Singleton okay and here I will  just have a method Define log of self and I'm   going to print um I'm going to print an F string  and I'm going to have self and I want a sln at the end um that should be I put self here twice  there we go so um I will just Define a method   that prints out my instance of my class okay  and then finally I will have a def create

Singleton okay no parameters here and I want to have  Singleton equals Network driver singleton. log and I will return Singleton okay  so that is my creation function so here I will   have a main and I will say let me just put a um  comment here single thread okay so we're on a   single thread I'm going to say S1 equals create  Singleton and S2 2 equals create Singleton okay   so I create two Singleton but obviously if this  works as it should then only one Singleton will   be created okay so let me go ahead and run  this the na quote unquote naive Singleton

and we can see here that the created objects are  exactly the same now the problem comes when we   uh Implement a multi-threaded environment  I've just realized that the text is quite   small so let me just um up dat this uh let me  search for font font size 16 let's put it to 20 hopefully what is happening okay so hopefully that  is easier to see now um so as I   mentioned this is a single thread  so let's add a um here multi- thread

all right so for multithread we're going to say uh  proc P1 for process one equals I need a thread of   course so let me just go at the top here and I'm  going to say uh from threading import thread okay   so I have my thread let me put a space here okay  going down I have uh P1 equals thread and I have   Target equals create Singleton okay and I will  have P2 equals thread with Target equals create

Singleton so this will create two Singleton on  two threads so let's see how this works if I run this um okay so I need to comment out the creation  because the creation once the creation is done   of course the Singleton is uh created so we  will get the same thing and I'm thinking uh   because this creation here can happen really  quickly I would need a bit of slowdown so in   a real world situation this might be delayed to  you know instantiate some resource so I want to

simulate that I'm going to put here import time  if I type this correctly import time and I want   to simulate here that this might take let's say  um time do sleep for one second so it will take   let's say one second to create this Singleton and  in this case that will cause some issues okay the   way I've the reason I've done this here is because  in a real world situation it takes some time to

instantiate some resource sources on your machine  so for example instantiating a connection to a   database or to a network Communicator or whatever  it is okay so um with that let's run our naive   Singleton and and I've just forgotten here to say  p1. start and P2 do start okay so running that again this should be a should not be a function call all right we got there in the end okay  so um sorry about that there was a bit of

confusion but in the end um we basically  create two threads we start them and as   we can see here we create two different um  objects of this class right here we have 40   here we have d0 so these are two different  instances of our supposedly single instance   class okay so that is a problem um in a  multi-threaded environment so how do we   actually fix this let's go ahead and create a new  python file um here we have let's say thread safe

Singleton okay um I'm just going to copy  everything from here and paste it here   here um the way we solve this is once  we start to implement start to create   our Singleton we need to have a lock um  so that we lock the thread so that other   threads cannot access it here okay so in order  to do that I'm going to add lock here uh from   threading and in my Singleton class I'm going  to have underscore lock is going to be um lock

all right so I have my lock and in my call uh  function I'm going to say with um here I have   self. lock okay and I'm going to put my if inside  this WID block okay so with that done I think the   rest should stay the same and I'm going to just  run this and you can see that we have the same   instance for both threads so what is happening  here is basically whenever we enter this function   call we lock the thread to check whether or not  there's an instance okay so that is all that we

need to do once we have that determined then we  can simply return the instance that is available   so that is how you would implement the Singleton  for multi-threaded environments all right so in   this video we're going to talk about the factory  method meod now here is the problem that this   design pattern will try to solve so first of  all in our development in our projects often   times requirements change and we do need to go  into the code and change some particular aspects

depending on what new functionality is needed  or we need to refactor the code that we have   to adapt to new changes okay so um this is where  Factory method will come in because it will cause   kind of a separation between uh the creator of  an object and the user of that object right so   that means this separation will allow us to  change our code more easily without impacting   a lot of our code now Dynamic switching is  also a quite interesting topic because in a

real life situation you might have for example  you might have multiple data sources or you   might have multiple databases and you want to  be able to switch between those databases in a   dynamic way based on certain requirements um in  that particular moment okay so we don't want to   have a tight coupling between the user of the  object and the creator of the object so that   we can change that object dynamically based on  other requirements also there is the principle of separation of concerns which means that you want  to separate the creation from the utilization okay   this is kind of a general principle in software  development so how does factory method help with   all of this first of all the design logic and the  creation or instantiation of an object is hidden   from the client so the client doesn't really know  how to create the object it just knows how to use

it so that means multiple classes of objects will  be able to uh be created and the client will have   access to it without knowing which exact type it  is actually using we have many subclass types but   only one instance right so as I mentioned we can  have multiple classes multiple uh types of objects   but as long as the utilization or the interface  is the same then we only need one instance and

we don't really care which instance which type it  is creation is removed from the client obviously   we need another object that creates object or  another class that creates objects and then this   is useful for frequent code changes so when we  want to update our code when we want to introduce   another type of uh this uh you know object or  this component um this is where the factory method   really shines because the separation is done in  a way that we can just add or subtract multiple

types and the code will still work seamlessly  without impacting too much so to kind of give   you an overview how this will work is here we  have for example three types of data databases   okay we have instances of these databases these  connect to their respective databases and use   their particular language however all of these  objects that access the database will have the   same interface so that means this interface um  will allow us to communicate with either one of these Without Really caring about which particular  instance we are dealing with and then we have the   database Factory in this example this is a generic  Factory that creates the particular instance that   uh at that particular time is required okay so  this is where the separation kind of comes in   the client doesn't really know or doesn't really  particularly care which instance it is using as

long as it gets an instance with this particular  interface so you see the separation here between   utilization and creation so the components that  are required basically will be an interface or an   ract class that defines the common functionality  the implementations one or more uh as many as we   need and we can add as the project kind of grows  and matures we can add more implementations based   on that particular interface and then the  factory class that instantiates the right

implementation for that particular use case for  that particular situation all right so that is   all for the theoretical introduction let's move  to our uh code and I'm I'm going to create a new   file here new python file and I'm going to call  this Factory uncore Method All right so in the   factory method we're going to add first of all  we're going to add some objects to kind of serve   as um the things that we kind of move around and  pass around in our code so here I'm going to have

a class country and what I would like to do I'm  GNA just pass here what I would like to do with   this code is I would like to have a kind of quote  unquote currency Factory that will give us the   currency for that particular country okay and we  will have multiple instances of those currencies   we will see how uh that works so I have my class  country and I will have another class here let's   say USA that is a subtype of country I'm going  to pass here I will have class let's say Spain

country class and let's add one more class Japan  which is also a country all right now um let's   create a kind of abstract class however uh since  python doesn't natively support that we need to   import from ABC import um ABC and Abstract method  so that we can create abstract methods and down   here we will say class currency Factory okay this  is going to inherit from ABC and we're going to   have an abstract method let's Define currency  underscore Factory okay we have ourself but we

need to pass the country here and we will return  a string and for now we're going to pass okay so   this is the abstract method um which will allow  us to implement our um our Factory okay so we   have our currency Factory now let's define  uh this is our interface let's define our   let's say Fiat which is the usual kind of money  that you have in a country the usual currency um   current fiat currency Factory and this is going  to be a currency Factor okay so we're going to

Define here our currency Factory that returns  a string and we're going to say if country is   USA then return uh USD United States dollar  L if country is Spain then return Euros else I forgot there we go um else return JPY  Japanese Yen okay so um very simple very   standard now let's create another one  that's going to be virtual currency Factory and this is also going to be  a currency Factory let me just copy

and paste this because it's going to be  exactly the same however here instead of   USD let's return let's say Bitcoin for Spain  let's return ethereum and for Japan obviously   we're going to return Dogecoin okay so we have  our Factory so now we need to we can simply have   a uh client code so let's Implement our main we're  going to have fub1 equals the currency Factory and   F2 is going to be virtual currency Factory okay  so now if we use F1 we're going to say print f1.

currency factory and we're going to do that for  USA and that's going to create the um USD and let   me just duplicate that two times and instead of  USA I'm going to have Spain and I'm going to have   Japan here okay so this is all with F1 and I'm  going to copy and paste and I'm going to replace   this with F2 okay so this should generate all  the coins all the currency that we have in   our application there we go so USD Euros Japan  and then Japanese Yen and then we have Bitcoin

ethereum and Dogecoin all right so that is the  implementation for the factory method so you   can see the use is uh where we use in our code  in our main code is separated from the creation   right um we don't know how to instantiate these  currencies but the factory itself does so that   is uh really useful in a lot of situations in  your project abstract Factory is you can think   of it as kind of one level of abstraction above  the factory meth and it is simply put it is a

factory that creates factories okay so just one  level up so let's see how this works in practice   so let's imagine we have a data source here  um data source type this is an interface and   then we have database we have user input Network  and whatever other data sources we might want we   might have all of these have underneath them they  have certain uh types so for example the database   might have SQL database mySQL database or whatever  type of database so you can think of this as kind

of a factory for uh databases Factory for user  inputs and so on now which one we use doesn't   really matter from the point of view of the user  which is in our case is going to be the display so   the display doesn't really care where it gets the  data as long as data is retrieved in the way that   it wants so in this case we can have a data source  Factory which is kind of higher level abstraction   which gives us the data source whatever that  data source may be okay so the data source

Factory instantiates the actual Factory that we're  going to use which in turn will allow us to access   the resource okay so this is how it works we  have a data source Factory Here We instantiate   the particular database Factory Network Factory  or whatever we will need and then that in turn   will be uh will implement the data source okay  and I haven't drawn here but obviously the   database and network will um will retrieve  the actual information so abstract Factory

provides a way to access functionality without  caring about implementation this is kind of   true for the factory method as well but as I  mentioned this is a one level up and in this   case we don't even care about which Factory we  use okay so we want to abstract that information   away as well one level of abstraction higher  and again it is a uh it follows the principle   of separation of concerns okay so we don't care  about how we use sorry we don't care about how we create the the object we only care about using  that implementation and again here it allows   for testability so we separate the concerns we  separate the components in our application and   therefore we can more easily uh test components  in isolation okay so that is how um that is the   theoretical introduction let's move to code and  here we are in our editor I am in py charm but you   can use whatever you like obviously I'm going to  create a abstract Factory file first of all like

in the factory method I'm going to say from ABC  import ABC and Abstract method okay so that we   can create abstract classes now what I would like  to do here is I would like to have kind of a uh a   practical example and we're going to implement a  quote unquote restaurant Factory okay so we want   to have a particular type of food as a client as  a user I want to decide what kind of food I want and then I would like to go to a restaurant so I  don't really care which restaurant it is obviously   in a real world situation you might care for other  reasons but in our situation here we don't care   what restaurant as long as we get the type of food  that we want so the abstract Factory is going to   be kind of the restaurant Creator the uh Factory  implementation we're going to have a uh let's say

American and French restaurants and the client the  user is going to just care about the food that it   receives okay so let's Implement that we're going  to have a class food type and we're going to have   French equals 1 and then American equals 2 okay so  we have our food type now let's create an abstract   Factory let me put here an abstract Factory here  and I'm going to have class restaurant and this is

going to be ABC and the restaurant is going to be  uh is going to have an abstract method let's say   Define make food and this is just going to have  self um we're going to pass for now and let's   define another me method abstract method Define  make drink okay and we're going to pass for now   all right so let's define a class restaurant that  inherits from ABC we're going to have an abstract

method um let's say Define make food okay we're  going to pass for now and another abstract method   Define make drink okay and we're going to pass all  right so we have our restaurant kind of interface   and here we're going to create a first type of  restaurant that's going to be French restaurant

which obviously will inherit from restaurant  so here we need to Define make food and we're   going to just print some food that we make  let's say cordon blue okay and then we're   going to Define make drink and we're going to  print something like Mero okay so that is our   French rest restaurant um we're going to also  Define a American restaurant okay so these are   all factories right so restaurant and we're going  to have our make food here we're going to print hamburger and Define make drink and we're  going to print something like Coca-Cola

okay typical American food right so um now we want  to have our uh abstract Factory so let's define um   a restaurant Factory okay so the client the user  doesn't really care which type of restaurant they   care which type but doesn't They Don't Really Care  which instance of the restaurant right so that's   what the restaurant Factory will create so um here  we will have a de let's say suggest restaurant

based on um here we will have a type which is  going to be food type I think type overrides some   uh built-in type so let's say R type restaurant  type okay so we have our R type and now we can   say if R type equals food type. French then return  French restaurant else return American restaurant   okay so we have our restaurant Factory now we just  need a client method def dine at and here we have

our restaurant of type restaurant right so this is  going to Simply print line um let's say for dinner   we are having I forgot to put the quotes the right  so for dinner we are having then we will simply   call the method in the restaurant so make food and  restaurant. make drink okay so that is the client   method and we of course need a main here we have  let's say suggestion suggestion one is going to

be restaurant Factory do suggest restaurant and  let's say first we want some french food so food   type dot French and then for suggestion two is  going to be restaurant factory. suggest restaurant   food type. americ okay so the first suggestion  is going to print our French food and drink the   second one American food and drink so we're going  to dine at the first suggestion and then dine at   suggestion two okay so pretty uh self-explanatory  I think the abstract Factory the restaurant

Factory will basically create the factories that  we have French and American and these in turn will   um share a common interface which we have defined  here as restaurant so we can actually use them in   the client Without Really caring which type there  are which instance they are we know the food that   we want and based on that the factory method the  abstract Factory uh defines our restaurant but we don't really care about the instance okay so here  is where we use it and we just call that in dine   at so let's run this the only thing I forgot  here is to make this method St IC it's also a   suggestion there so we could just create a um we  could create a instance here but this is uh this   should be easier right so this is static for some  reason the types have disappeared so food type.   French and food type. americ right so since this  is a static method we can now access it so let's

go ahead and run this there we go so it is working  as expected for dinner we are having french food   and then American food all right that is all  for the um abstract Factory U design pattern all   right in this video we're going to talk about the  Builder pattern now the Builder pattern is really   useful in many situations because it allows us to  have an object that is initialized in different   ways so you can think about an object that has  multiple parameters that we need to initialize

and in some situations we might want to use some  parameters in others we might want to use another   set of parameters right so the Builder allows  us to be flexible when initializing an object   for example if we have many parameters it's quite  impractical to build all Constructors to build the   object that has the uh multitude of options when  it comes to building it so for example if we have five parameter combinations that will give us 120  Constructors so we don't want to list kind of all   possible permutations and combinations of these  parameters we have some optional parameters in   our objects which means that some parameters are  uh you know mandatory others are optional so we   might exclude them if we want to when building  our object and in addition it should be quite   easy to read okay so we should we should have  a way to construct the object that is easy to

understand and easy to use by whoever wants to  instantiate that particular object all right so   that is all for the quick introduction now we're  going to move to our code um and we're actually   going to see two ways of putting in place this  Builder pattern in Python one is the classical   way and one is kind of a simplified way to achieve  the same thing in an easier way all right so let's   start with the first one I'm going to create  a new file and I'm going to call this Builder

so what I'm going to do here is I'm going to  have a class called network service where I can   instantiate it with uh different parameters we're  just going to have three parameters in our example   but we can add uh many more if you like so you  can kind of visualize how that would be so let's   have a class network service and we're going to  Define our init method here and we're just going   to say self. components is going to be an empty  dictionary all right so we're going to add a key

of type string and a value of also type string  and we're just going to say self. components of   key is going to be value okay easy enough and  we also want to just print out our components   to see what we have there so here we have do  show okay and I'm going to say print self.   components okay so that is our network service  it's quite simple but as you can see we can add   as many parameters as we like here now for the  purposes of this design pattern we're going to

have a class network service Builder which we're  actually going to use to inst instantiate our   network service so here I'm going to Define an  init method self. umore service is going to be   a network service all right now let's add a  few things so first of all we might want to   add a Target URL to our network service right  so def add Target URL and here we have our URL

of type string so what this does is basically  it says Self doore Service do add and we have   a key URL and our value URL okay easy enough  that's the target URL let's define a couple more   let's say add O Okay so that's authorization  and here we have self and O of type string okay um we're going to say selfservice do add  we're going to have the key authorization and   we're going to have the value off okay and  finally we're going to add um something like   Cash add cash or rather caching because this  is just the time interval for the cache okay   so caching and and here we're going to have  our cachee of type int okay and we're going   to say self doore service. add here we're going  to have cache control which is how you would um

spell that in the um URL headers okay and  here we have our cache okay that's going to   be converted to an to a string automatically  now we're going to Define also a method build   that will return us a network service okay so  we're going to Simply say here uh sorry service   equals Self doore Service I want to reset this  underscore service so underscore service equals   network service so reinstantiate that and  then I'm going to just return our service

okay that is fine so here we have our Builder and  now we're going to use it in our main so let me   Define the main and we're going to just say here  Builder equals network service Builder and now   we can use it to create our network service so  first of all we will create a very simple network   service that doesn't have authorization and  doesn't have caching so we're going to say build

do add Target URL that's the only thing we're  going to need and let's say google.com Okay so   we have our first network service we're going  to say service one equals Builder dot build and   then service one. show okay so we're just going  to print it out now this has been reset okay so   we have it here we reset it so going to create a  new one we're going to say builder. addtarget URL   let's say youtube.com we're going to say Builder  do um add o okay so here we have let's say abc1 23

this is the key for the authorization and finally  we're going to add caching and let's say this is   60,000 milliseconds that's 1 minute and then  if we do service 2 equals builder. build and   then service 2. show okay so that should basically  build our two services so let's go ahead and run   this and we will get the first one we have only  the parameter that we have instantiated and the   second one we have all three parameters okay  so very very uh clear I would say how we use

this Builder to to instantiate the network  service now as I mentioned earlier we have   an actually a simpler way to do kind of the same  thing so this is the classic way you would build   a builder okay if you require some more complex  functionality but let's keep it simple and let's   create a new file Builder 2 and in this file we're  going to build the same thing except we're going   to take advantage of the named parameters that  we have in Python okay so we're going to have

class um network service okay and here we're  going to have our init function but here the   difference is we're going to pass all parameters  here in the init function and we're going to give   them default values so URL of type string and  empty by default empty string we're going to   have off of type string also empty by default and  we're going to have cache of type int and that is   going to be Zero by default so here we're going  to build our dictionary so self. components is

going to be an empty dictionary now we're going  to say if URL okay so if this is not empty then   we're going to just add it to our components  so self do components of URL is going to be   our URL and we're going to do the same thing for  the other so if o self. components of O this is authorization is going to be equals to O and  then if cache so if this is not zero because   this is an integer we're going to say self.  components of cach control is going to be

our cache okay very very simple so this is kind  of equivalent to what we had previously and now   let's define our show function and here we're  just going to like before we're just going to   print self. components all right so we have that  and now that is basically all we need okay so   this is equivalent to these two um to these two  classes so now we can use our main and we can say   here service one is going to be equal to network  service and here we have the option to provide the

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent