From the course: C#: Design Patterns Part 1

Object-oriented programming - Python Tutorial

From the course: C#: Design Patterns Part 1

Object-oriented programming

- [Instructor] This isn't a course about OOP but to get to design patterns for C#, we have to start with object oriented programming. In object oriented programming, everything is a thing, an object is created or instantiated and has a lifetime. OOP is a structured organization of properties and methods. Alan Kay, one of the pioneers of OO design has said that he regretted the term object being so prevalent. The real point is messaging how information goes from place to place and is stored. OOP is a set of concepts that are very much about how and where data and actions come together and organizational strategy that makes it easier to build and maintain functionality over the long term. I'm going to slide quickly through some core concepts that make up OO programming. It's not what the course is about but a quick refresher isn't going to hurt anyone. Encapsulation, don't share what doesn't need to be shared. The concept of an interface is important here. Whether or not you have an explicit interface, a class has a practical interface, the properties and methods that are available outside of the class, what pops up in your IntelliSense window. Encapsulation means contemplating what the interface actually needs to be and keeping that interface as simple as possible. Don't expose anything that doesn't need to be exposed. Abstraction is next. Abstraction layers are really important. I don't need to know how the machine code works to build a mobile application. I can run an application in JavaScript that runs different machine code on any number of different processor architectures that run JavaScript. The lower layers are abstracted away so that I don't have to worry about process architectures when writing a web app. Smart encapsulation enables abstraction. I don't have to know how something is done, I just need to know what is being done when I make a call. If I say message sender.send message, I don't care if it's SMS or email or carrier pigeon, just that the message is sent. The more details that the caller has to interact with, the more the implementation is being built into the interaction. Violating abstraction and encapsulation makes for brittle code that is hard to change and easy to break. Inheritance means that I can extend existing code, add functionality or restrictions to its execution. Understanding where code is extensible is vital when working with existing frameworks or libraries. This is the same for an individual class. Inheritance means that I can create something new with the starting point of something that already exists without changing it. This is an excellent way to reuse code without breaking code that already depends on it. Finally, there's polymorphism. Extended objects can be treated as the thing they are extending. There's a logical path here from inheritance. Don't pretend that you're extending something if you're actually changing it. The power of typed exceptions in C# means that no matter what type of exception it is, it has a message and I can display that message. If some exceptions don't have a message, I can't have code that treats all exceptions the same way.

Contents