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

Study methods

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

Design Patterns Overview

Transcript

welcome to the design patterns course in this  course we're going to cover the fundamental design   patterns that all software Engineers need to know  so what is so special about using design patterns   what are they first of all design patterns are  well- tested reusable solutions they are like a   solution template for how to solve common software  engineering problems they are flexible and adap able you can use them in a variety of scenarios  and they save time because they have been used so   many times that they have been optimized they are  not only well tested and reusable solutions they   are also Optimal Solutions so why would we use  them well you know when you use a design pattern   that you're using something that has been utilized  successfully in countless projects before and they

are like a common meta language for developers  if you and I both know what a Singleton pattern   is then we already have a common understanding  in of everything about how it would be used or   how it should be implemented you can think of a  pattern as a common vocabulary for architects in   other words we are on the same page and this  is very powerful because it facilitates good   communication and that's true even if you code  in different languages if you and I work with different languages you work with c and I work for  example with Java or Dart we still will understand   what a Singleton pattern is in in other words  design patterns in General carry over to different   programming languages if you understand a pattern  in a general manner then you know that regardless   of the language implementation it will effectively  offer the same useful reusable solution so in

summary design patterns are associated with good  well organized software design as developers we   are obviously prone to mistakes and sometimes  inexperienced decisions think of design patterns   as a bag of reusable experience from many  generations of coders and software Engineers   that we get to use and learn from next we're going  to learn what are the design patterns covered in   this course in this course we'll cover the seven  most crucial design patterns each pattern will be covered as follows we will explain and explore the  design pattern architecture we will then implement   this pattern with objectoriented python we  will look into how this pattern helps good   architecture and in a practical manner we're going  to use all these patterns to architect and write   a simulation of John Conway's famous Game of Life  which will run on your mobile device here are the

patterns that we'll cover in this course in the  creational family of patterns we're going to cover   the Singleton pattern Factory method pattern and  the Builder pattern the creational design patterns   are design patterns that manage object creation  mechanisms they are designed to create objects   in flexible and highly adaptable ways these  patterns offer the most reusable and flexible ways of creating object instances next we're going to  move into the structural family of design patterns   specifically we're going to look at the adapter  pattern structural design patterns are design   patterns that improve and simplify your designs  by identifying the most efficient and re usable   way of composing relationships among entities  this allows for the best and most efficient   ways of creating complex hierarchies of objects  lastly we're going to move to behavioral family

of patterns specifically we're going to look at  the strategy pattern The Observer pattern and the   state design pattern behavioral design patterns  are design patterns that improve interaction   and communication between objects these patterns  for Loosely coupled ways of allowing objects to   talk to each other and exchange messages once you  master these patterns you will immediately see the   world of coding differently you will start seeing  those patterns everywhere just remember these pattern families we have creational structural and  behavioral they Encompass a whole world of design   patterns creational patterns deal with ways of  creating objects or families of objects structural   patterns deal with ways of managing complex object  hierarchies and behavioral design patterns deal   with ways of identifying and improving object  messaging next we're going to learn as to why

do you actually need software architecture we'll  see you in the next lecture great to see you again   let's talk about why is it that we need software  architecture as you know complex software systems   are plagued with many issues timelines are  stretched as requirements change multiple   developers have a hard time coordinating their  efforts and often there is code redundancy and   poor documentation this in turn creates issues  with maintenance and overall flexibility for

adding new features in general this means poorly  designed systems that are hard to maintain and are   not adaptable one answer to all decided problems  is having a proper design and architecture think   of a skyscraper being built like the Empire State  Building there's always a highlevel blueprint this   blueprint is used to show everybody involved from  Architects to supply chain to construction workers to Machinery scheduling what is being worked  on and we want that kind of predictability   and coherence in our software projects in  other words we want to have a well-organized   and cohesive construction site so how do we  achieve that let's look at the development   cycle you start with requirements you gather  and document all the requirements for UI the   ux and general goals of the project once you have  all that what you do next is you do a design you

architect the system based on the requirements  you've gathered finally you implement and this   is where you code and test the solution to  ensure that it follows the design and fulfills   the requirements now mind you it doesn't have  to be exactly this way where first you gather   all the requirements and then you do all the  design and you do all the implementation you   could do it more in a manner where you get some  requirements you design it and you code it as a proof of concept then you go back to get more  requirements you add them to your design and   you code it so you start building it from  a core system no matter which way you do   this no matter how you follow this development  cycle you need to have a good design in this   course we will concentrate on the design part and  we'll explore how design patterns facilitate and   help achieve good software engineering let's look  at all the different documents and artifacts that

you will normally encounter in a project you  will have requirement documents you will have   design documents you're going to have UI and  ux documents test Suites and documentation for   that you're going to have deployment documents so  that you can deploy Your solution to a particular   system once your system is running you're going  to have an audit and logs which are artifacts   and they're also documents and you will also  possibly have analytics but of course all of this revolves around the source code that is the  final outcome of all your requirements Gathering   and your architecting but you'll also have  architecture documents and possibly you might   have diagrams for your database as well but what  we want to talk here about is how design patterns   facilitate good architecture documentation to  document and teach you those design patterns   we're going to use uml so as you can see projects  will have many moving Parts but in the realm of

software the final product is always the source  code and good architecture is crucial design   patterns help greatly in that aspect and this  is what we're going to explore in this course   next we're going to look at why are we going to  use uml in this course welcome back so whyl why   are we going to use use uml in this course  uml is a good tool to document something as   well defined as design patterns design patterns  are well understood and uml communicates their

structure very well and that is because it is a  highly visual representation and while it is not   easy to maintain uml diagrams when requirements  change for our illustrative purposes in this   course they are great we'll only use two types  of diagrams class diagrams and sequence diagrams   there are many other types of uml diagrams but  they are not needed for this course so let's talk   about the two types of diagrams class diagrams  are used to convey the structure of a design

pattern here's an example you see a director a  builder a concrete Builder and a product there   is a relationship between them you can see that  a director Aggregates one or more Builders you   can see that a builder is an abstract class from  which you could create specific concrete Builders   and you can see that what the Builder does is it  creates a product okay that's great so we kind of   understand the structure how does information flow  how does it actually work sequence diagrams are used to illustrate the interaction of the main  objects and the classes here's an example of a   sequence diagram we have a client and we have two  objects we have a director and a concrete Builder   just as you see in a class diagram the flow  goes as follows a client creates a specific new   concrete Builder it then creates a director  initialized with that concrete Builder it then   calls the director's construct method and then  behind the scenes what the director does is it

communicates with the concrete Builder to build  the different pieces of the product so it can   build part A Part B part C and then it delivers  the product to the client so what the director   does just like directors usually do it coordinates  possibly multiple Tas tasks so that you get a well   organized object and in fact what the Builder  design pattern does it is used to build complex   hierarchies of objects so because design patterns  form their own blocks of interaction we can study

them independently and uml is a great tool at  that level of abstraction in other words we can   concentrate on one design pattern fully Master  it and understand it and then move on to another   they're isolated you can study them on their own  that's what makes this great and it's basically   following that divide and conquer approach next  we're going to have a look at the difference   between organized and unorganized code what you  see here is a grid of simple rectangles you can see that there's a very simple grid pattern there  is some color differences but it's very simple in   its organization on the other hand when you  look at this this looks more like a bunch of   overlapping blobs with some larger some smaller  it's not as well organized if you think of these   two as different ways of implementing code you can  imagine the one on the left as being nicely broken

up kind of like the divide and conquer approach  and well encapsulated whereas the one on the right   has a lot of large pieces of codes sitting in  one place and you can see a number of overlaps in   other words the one on the left is more structured  the one on the right seems a little more chaotic   and then if you facilitate the communication  between the two you can see that it's easier   to see who communicates with what on the left hand  side whereas on the right hand side it's a little little harder to discern a pattern this is a  visualization of what organized versus unorganized   approach is in this course we'll strive to get  you to the left side so that your architectures   and your code are going to follow very nicely and  well defined patterns as opposed to what you have   with the more chaotic and unorganized approach  on the right hand side next we're going to have   an optional lecture which is a uml refresher for  those of you who want to refresh your knowledge of

uml welcome back in all the coding examples for  this course we will be using visual studio code   so I will quickly describe and show you how to  set up vs code environment for python first a few   assumptions you have python interpreter already  installed and running I recommend the version 3.7   plus so anything above that is good you also have  Visual Studio installed and running on your system   Next Step will be to install a visual studio  python extension from the marketplace and then

after that we'll go over a few python environment  commands which are run from the terminal let's go   over them ven or virtual environment creates a  virtual environment for our project for example   in the terminal command line you could write  something like python DMV then you put the project   path the project in which you're working but you  also have to include the ven subdirectory because   that's where the actual environment is going to  be created we will also be using pip pip which

is a package manager for python libraries whenever  we need a specific library to be imported we will   need to use that for example you could just say  pip install and then you give the library name   we're going to go through these examples in this  video so you will be able to see all of this in   Action a few additional notes we will mostly  work with objectoriented paradigms this means   that we'll mostly work with objectoriented python  and we'll try to keep it as purely objectoriented

as possible this means using the following  Constructors and initializers instances and   classes and we'll also use some inheritance we  will also touch upon threat safety when needed so   let's have a look at the actual process of making  your Visual Studio code ready for python to check   the python version all I need to do is simply just  run the python command with the version so this   way you can check that your python is installed  and what version you have you could also just do

this this is pretty much equivalent the next  thing that we're going to do is we're going   to now start Visual Studio get rid of that so now  we're going to install the python extension so you   go into extensions we're going to look for python  we're going to grab this first one by Microsoft   intellisense just click on it and install it will  install a number of other sub extensions takes a   little bit of time to install all right this is  done so now that we have the python extension

what we're going to do is we're going to create a  project folder and I'm just going to do it right   inside my little practice I'm just going to  create a folder I'm going to call it project one get rid of this and now what I'm going to  do is I'm just going to open the folder my C   drive impr practice and project you could do it  anywhere and you can call it anything you want so   now that we have this what I'm going to do is I'm  just going to create a file and I'm just going to

call it main. UI and as you can see because of  the extension Visual Studio understands what we   have here so now what I'm going to do is I'm  going to create a virtual environment let me   just actually put some code in here first so  as you can see we have SIMPLE fully functional   visual UI python code but as you can see here  that squiggly line under py game tells us that   it doesn't know it cannot resolve it in other  words it cannot import it so we will need to

do two things first what we're going to do is  we're going to create a virtual environment so   the way we do that is we go into terminal and pay  attention to where the terminal opens it happens   to open inside our project so what I'm going  to do right now I'm going to create the virtual environment this just shows me the current path  and I'm going to create the new environment   inside the VNV folder so this is it so as you can  see here it has created a number of configuration

files also this is where the libraries are going  to go so we have our virtual environment so what   I'm going to do right now so I'm going to get rid  of this and start a new terminal and as you can   see at this point because of this VNV in green  that we have the environment understands that   this is now a virtual environment so now what  we're going to do is we're going to use pip to   install this Library above so we're going to go  pip install and the library name is simply py G

I'm just going to copy that paste it here so now  I'm going to run this and as you can see we have   our little pame window and our environment is  running fine all right so this is it this is just   me showing you how I set up my environment but  of course if you're only using The Interpreter   and you're familiar with the way you're doing  things that's all good as long as you're able   to run the code that's all that we really  need to worry about but because I will be

showing you everything within the visual studio  environment it might be easier for you to just   follow exactly what I'm doing and therefore  I wanted to just make sure that you have an   environment set up like I do all right thank you  so much and I'm going to see you in the next video   and as always keep on coding welcome back we will  use uml to visualize two aspects of our learning   we're going to look at structural relationships  between objects in our designs this is where you see how all of the objects fit together  what are the relationships between them and   how are they built we're also going to look at  behavioral relationships between objects in our   designs this is where you see how the objects  behave how do they communicate with each other   the end goal is to understand what is being done  and how it is being accomplished to illustrate   the structure of our design patterns we will use a  class diagram a class diagram is used to construct

and visualize object-oriented relationships  we use the diagram to capture the following   information class information such as attributes  and operations or basically methods here is a very   simple abstract class it's just your generic shape  but you can see that it has two two attributes   color and location and then it has three  operations get area get color and get location   you can also control the visibility the plus sign  here means that something is public if we put a minus it would for example be private but we don't  have to really worry about that in this course   the next thing that you have in a uml diagram is  class relationships between the different classes   in the diagram the first type of relationship that  we're looking at are generalizations for example   we have an abstract class here shape and then we  have three descendent classes triangle Circle and

rectangle all of these three classes they inherit  from shape what we have here is inheritance which   is a generalization the next type of relationship  would be an association here's a uml diagram that   shows a cylinder a cylinder could be created from  two circles and a rectangle first of all notice   that cylinder is a generalization of shape so  we inherit from shape and then also notice that we have Elements which is a list of shapes and we  stipulate that there are going to be three shapes   in there we also put a comment so the idea here  is that a cylinder is made up of three separate   shapes so this is an aggregation in other words  the cylinder Aggregates some elements that happen   to be shapes now let's have a look at dependencies  here is an example it's exactly the same diagram

but here we have two things that I would like  you to pay attention to one is that we have put   everything into a package this means that all of  these things belong together and also notice that   the calculator is not really associated with  any class but there is a dependency between   shape and calculator and what this shows you is  that shape uses a calculator so the dependency   is much looser here in a sense that a shape in  order to do something needs a calculator but the calculator is not part of shape all right  so let's kind of put all of this together and   have a look at another uml diagram that represents  a house so notice first that we have an interface   called iro notice then that we have an abstract  class room so what you have there is that room   implements the interface IO then you have three  generalizations of room we have a bathroom living   room and a bedroom when you look at the house you  can see that the house has a lot of associations

you have an association where a house Aggregates  three bathrooms there's one living room and it has   five bedrooms the house also has two doors 10  windows and a garage the house also has a pool   now I would like you to look at the difference  between the two diamonds there's a black diamond   and a white diamond when you have a black  diamond that means that whatever contains   the other elements so for example house contains  a bathroom what a black diamond means is that you cannot have an instance of the bathroom unless  you have a house or to look at it in a different   way if I remove or destroy the house the bathroom  is going to go with it this is the same for garage   window door because these are all inherent parts  of a house notice though that a pool also belongs   to a house but it has a white diamond and what  that basically means is that we still consider   a pool to be part of the house but if the house  goes away the pool actually is able to stand on

its own it's not completely composited into the  house so that's just a simp simple example and   you can also see that the house it depends on the  electrical grid but the electrical grid is not   part of the house it's a completely independent  entity but the house uses it therefore it's a   dependency so this is a very quick refresher  of class diagrams next let's have a look at   sequence diagrams a sequence diagram is used to  visualize objectoriented Behavior we use it to show message exchange over time and we capture  the following we capture interaction information   such as actors of operations so basically what is  part of the sequence and Method calls with data   for example here's a very simple sequence diagram  of how the cylinder would calculate an area a user   wants to get an area of the cylinder so it calls  the method get area inside the cylinder it creates

a variable called area sum and initializes that  to zero then it goes into a 4 each Loop it goes   through all the elements which are shapes inside  the cylinder and for each one of them it calls the   get area for that particular shape and when that  gets returned it adds that to the area sum and   it does that for all three once it goes through  the loop it puts the area sum into the result and   returns it to the user this gives you an idea  of not only what is happening but also to some extent how it is being done that was just a very  quick uml refresher next we're going to look at   what makes great architecture we'll see you in  the next lecture welcome back let's do a quick   refresher of the Python language with reference to  objectoriented programming and we're dealing here   mostly with the version of python which is 3.5  or more we'll discuss the following main Concepts   classes and objects encapsulation inheritance  and within inheritance we're going to talk about

interface contracts as well as abstract classes  and we'll also look at uml diagrams for python   object rented programming so that you have a good  introduction to what to expect in terms of how   code translates into uml diagrams and vice versa  when you're looking at the rest of the course   all right so let's talk about classes and objects  classes are simple or maybe more complex recipes   that are used to create two main things we want  to have a container for our data so they provide

a data container which would mean variables or  constants and it also also provides operations   on data and this would be functions or methods we  then can use the classes to create instances of   objects which hold the specific data that we can  operate on and because we use classes to create   instances that's why we call them recipes here's  an example of a very simple class in Python we   have a greeting class we have a Constructor which  accepts a name we can then initialize the name

variable with the name that was passed in and  then we can define a method for example a say   hello method which simply prints a personalized  greeting message using the name attribute which   simply does this so what you see here is a class  this is a recipe for creating objects so let's see   how we would create a greeting object and it's  pretty straightforward we simply specify that a   greeing variable is equal to a greeting object  constructed with the value JN and then once we

have the particular instance we can then call a  method on it which will specifically work on that   particular instance and since that instance was  initialized with the value John that will print   hello John here is what this would look like in  a uml diagram we have a class name greeting it   has a name variable which is shown here as being  private in Python you don't really have that kind   of privacy like you do in other languages but we  want to show the intent of our design in the uml

diagram so if I want the variable to be treated  as private I'll show it with that little minus   as being private we then have a init method which  is basically a Constructor and notice that we're   also showing self as being of the type greeting  and name is a string and then we have this say   hello method now this is a slightly awkward way  of notating that it's it's very very language   specific we don't really want to have a super  language specific uml so I'm going to simplify it a little bit and this is what we're normally  going to be working with we have a greeting class   it has a variable n name and then we have a simple  Constructor which is simply greeting that accepts   a name the reason why we don't show self here  is because that's how the Python language works   you don't have to really add that into the uml  diagram notice that when we actually create an   object of the greeting type we don't for example  provide anything other than the name itself

so this is how you would do it in the actual  code you would basically say greeting is equal   to greeting with John being initialized sometimes  we will do a bit of a Java like syntax and we're   going to show it like this and clearly this is  not python syntax per se but what we want to draw   your attention to is that we expect the greeting  variable to hold the greeting type that's why we   put it on the left of course this is not how we're  going to show it in code but this is how we're going to show you in the examples to draw your  attention that we would like the greeting variable   to be what you see on the left of the greeting  type now you might think this is a little strange   to show that kind of notation but as you will  see a little bit later you could have something   that could for example follow an interface which  means that the actual type expected is different   from the type being initialized which is a little  bit strange for python but it's very important

when we deal with certain aspects of the design  pattern so I would like you to basically think   of this as interchangeable obviously the first  one is the correct python syntax but the second   one is just a little bit more explanatory and  it's going to be used more as an example rather   than actual code so this is basically how we  would create a class in Python you create the   recipe by using the class syntax create an init  method which is basically Constructor and then you provide a number of methods that you want to  operate maybe on some inner data perfect okay so   let's look at the next topic so what about  encapsulation what exactly is encapsulation   in general a well-designed class already achieves  encapsulation in the sense that it gathers all the   relevant data and functionality in one place it  helps us with controlling complexity as well in   other words when you encapsulate Behavior you can  actually put a lot of very complicated code behind

a very easyto use method that's one of the main  aspects of what encapsulation is all about so do   have the following points in mind make sure that  you create classes for all the objects you need in   your code and also create collections of related  objects so that they can be treated as units this   means that you start with very simple objects and  then you can put them together into more complex objects kind of like the way a house is made of  different rooms if a room was an object we would   Define it first and then house would be an object  that is made up of rooms which are other objects   or classes so here's an example let's say we have  a class author we have a very simple Constructor   basically the Constructor takes a name and a birth  year for the author we initialize it and then we

have a method which is going to be get author info  which simply returns a string that encapsulates or   shows the information about this author so in this  case we simply return a string that is going to   specify the name of the author and what year they  were born so this is one class we just created a   very small a simple recipe for what an author is  next we're going to create a class called book now the book itself is a more complex class it  has variable title it has a publication year but   it also Aggregates or contains within itself the  author object so in other words a book is a class   that Aggregates or contains another class and  this is how you can build hierarchies of more   complex classes or objects with relatively simple  ones so let's have a look at our book so the book   is going to initialize the title it's going to  initialize the publication year and it's going

to initialize the author next we will have a  simple method which is going to be get book   info all of that does is it returns a string that  basically Aggregates all this information such as   title which was by a particular author and you  get the author info from the author object and   you specify that it was published in a particular  year here and this is how you would create an   author object and this is how you would create a  book object which Aggregates the author object so this is pretty straightforward and this is how you  would use it you could for example say let's print   the book info so what you have here is a slightly  more complex hierarchy of objects now let's have   a look at the uml for that so the uml is pretty  straightforward we have a book which Aggregates a   single author so what you have here is hierarchy  a book can contain an author and an author is a

smaller encapsulation and then book is a larger  encapsulation that encapsulates another object   all right so now let's have a look at inheritance  very important object oriented programming idea   inheritance allows us to generalize we can think  of it as a tree which grows more complex as we   keep on extending its branches so it's almost  like you start building more and more by dividing   your hierarchy more and more basically the idea  is that we start with some property or behavior

that is present in different instances or types  of entities and we then create new and more   specialized versions of the parent by inheriting  either data and or behavior in the children you   will see what that means in just a second so let's  say that we create a simple animal Base Class so   what this is is basically just a generalized  idea rest recipe that creates an animal that   has a name and the animal can speak it can say it  can basically vocalize something and all that this

class does is basically says let's initialize  the name and then let's write in the speak   method where it prints basically that the name  makes such a sound so this is very very specific   so let's assume that all animals make sounds  and let's assume that all animals have names   so rather than creating now a dog object that  will have its own name and it will have its own   speak method we can actually inherit some of that  already from the animal class so let's have a look so we can now create a dog class so notice that  dog inherits everything from animal and then we   are going to override so we're going to change the  inherited speak method we're going to make it more   specific to the dog and we'll say for example that  the dog barks but no notice that we did not have   to reinvent the name the name has been inherited  already from the animal class and notice another

interesting thing we do not have in the dog class  we don't actually have a Constructor well we do   but that Constructor is inherited from animal  in other words the same way you create an animal   object you will do exactly the same thing with  dog now let's look at a different animal cat again   simple idea we just override the specific behavior  of a cat but a cat still inherits everything from animal so now let's have a look at the objects of  the derived classes so we can say dog is equal to   the dog class with Buddy and then cat could be  whiskers and then when we call the speak method   on the dog it will say that buddy barks but if  we say for the cat it will say whiskers meows   this is very important to understand that what  inherit allows you to do is twofold it allows   you to inherit behavior and certain data from  the parent class but more importantly it is

still considered in the same category so a dog is  still an animal a cat is still an animal in other   words if the animal class which is the parent  class has a method speak we expect for both the   dog and the cat or any descendant of the animal  class to also have the speak method and that's   very important you you're not only inheriting you  know some methods and some data you're actually   inheriting an expectation of behavior so let's  have a look at what it looks like inl and inl it looks pretty straightforward we have our animal  class the animal class has a name it also defines   a Constructor and it has a speak method now our  dog class we're notating here that a dog has a   dog Constructor but that actually Constructor is  inherited from animal and then we over write the   speak method and we do exactly the same thing for  the cat so this is pretty nice because what this

shows us is that a dog is an animal a cat is an  animal so whatever the animal class does in terms   of behavior you can expect that from both dog and  a cat so how does this extend to the idea of an   interface well that's actually very powerful  the concepts of an interface contract and an   abstract class are crucial in software engineering  we'll be using them heavily in our design pattern   so I would like to make sure that you have both  of these Concepts well covered in Python so we

will devote a whole lecture to those topics  I'll see you in the next video and as always   keep uncoding welcome back in this lecture we'll  go over interfaces and Abstract classes let's talk   about contracts a contract is like a promise  that you will provide some specific behavior   in classes this means that you promise to provide  some functionality as you've seen before with the   animal if I know that a cat is an animal then I  know that it can speak because all animals can

speak so in other words I expect the cat class to  fulfill the promise of the animal contract one way   to create a pure contract is through a concept  of an interface in Python we do not really have   strict interface syntax what we can do is we can  use an abstract class with no implementation to   the methods so let's have a look at that so these  are the Imports that you would need for abstract   classes you would have to import ABC and you  would have to import abstract method so here

is an interface contract and children will have  to implement all of the abstract methods and in   an interface method what we do is we have no  implementation so we simply use pass instead   of the function body so for example here is a  simple class it's called My interface notice   that it inherits from a ABC all abstract classes  have to do that and we Define an abstract method   called let's say My Method and notice that we  put pass in it because we're not providing any behavior and that is it so now what we do  is we're going to derive or create a class   based on my interface contract and we know that  since my class inherits from my interface it has   to have the method My Method so what do we do  we Define the My Method and then we put this   specific implementation that we would like for  that particular class implementation here is an   example of another class that also inherits from  my interface and it also has to it has to fulfill

the contract and it has to define the abstract  method My Method and here we do it with that print   and then basically all that we do is we can just  create an object like my object is equal to my   class and then you can just call the My Method and  you can have the other object and you do the same   thing call My Method so this is pretty powerful  because we can now use this idea of my interface   as being a contract which has we know for sure  a method called My Method so I know that if I call that method on any object derived from my  interface I am guaranteed that I'm going to have   an implementation of My Method now this is very  very important for design patterns so let's have a   look at the uml for this so usually the way we're  show it in the uml is we're going to just show it   as an interface notice that there's no such thing  as an interface in Python but given what we have

explained you will understand the uml and what  you see on the right the code on the right is   basically reflected in the uml diagram we have  a my interface and then we have a my class and   another class which implement the methods of my  interface okay so how do we enforce contracts we   can use something known as type henting in Python  to enforce a contract so this is our interface   just as before we have our my interface it's based  on the ABC which is abstract base class and then

we have one abstract method we could have more  and this abstract method is not implemented so   here's what we can do we can now create a class  that is derived from my interface so let's call   it my class and as you've seen before we just  implement the Mind method that's great but now   let's say that we have another class but it has  nothing to do with my interface and it has its own   method called Su method notice that we're going  to create a method that expects specifically my

interface contract to be passed as a parameter  to this method that's very important and in this   one we'll call the My Method and why is this  important because we are guaranteed that any   descendant any child of my interface is guaranteed  to have an implementation of My Method so now we   could pass into process my interface my class  object and it will work perfectly but if we   pass the not implementing interface we're going  to get an error so let's have a look we create my object which is my class wonderful and we pass  it into process my interface and that will work   properly but if I create an object of the type not  implementing interface and I pass it into process   my interface we're going to get an error all right  so now let's talk about abstract classes abstract   classes are kind of in between normal classes  and interfaces so let's talk about partially

implemented contracts which is all about reuse in  many cases you have actual functionality not just   a signature but actual functionality behind the  signature that you would like to have all child   classes inherit so as we've seen in case of like  an interface there is no actual functionality   these are just contract signatures every method  is just signature the name and the parameters   are just a signature of what is expected but  sometimes we already know that there should be some sort of a base implementation so this is  where you would use an abstract class you can   add specific complemented methods and you can also  add specific constants or variables that everybody   is going to inherit like you've seen with the case  of the animal where every single animal like a cat   or a dog or anything that has been descended from  the animal class automatically inherited the name

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent