Scanning the Internet for External Cloud Exposures via SSL Certs
Intro to object oriented programming
1. Principles of Object Oriented Programming David GiardMCTS, MCSE, MCDBA, MCSD DavidGiard.com
2. nPlus1.org nPlus1.org is a site dedicated to helping Architects, aspiring Architects and Lead Developers learn, connect and contribute. http://nplus1.org
5. The Complexity of software derives from 4 elements: Complexity of the problem domain Difficulty to manage the development process Flexibility possible through software Software problems often model discrete systems
7. Good question… lots of answers! The customer–friendly programmer says: “Great software always does what the customer wants it to. So even if the customer thinks of new ways to use the software, it doesn’t break or give them unexpected results.”
8. Good question… lots of answers! The design–guru programmer says: “Great software is when you use tried-and-true design patterns and principles. You’ve kept your objects loosely coupled, and your code opens for extension but closed for modification. That also helps keep the code more usable, so you don’t have to rework everything to use parts of your application over and over again.”
9. Good question… lots of answers! The object-oriented programmer says: “Great software is code that is object-oriented. So there's not a bunch of duplicate code, and each object pretty much controls its own behavior. It’s also easy to extend because your design is really solid and flexible.”
12. The Basics Objects Classes Members Properties Methods Events Constructors Instances Message Passing
13. Defining Classes class Name:BaseType{ // Members } class MyType{ public String SomeString; public Int32 x; public Int32 y; public void DoSomething() { … } }
14. Class Members Fields The state of an object Properties Also maintain state Methods Constructors Functions Properties (smart fields) Members come in two basic forms Instance Static
15. Fields Maintain state of an object Accessing a field class Point{ public Int32 X; public Int32 Y; private In32 _x; } Point p = new Point(); p.X = 100; p.Y = 200; Console.WriteLine(p.X);
16. Properties Methods that look like fields (smart fields) Can have read-only or write-only properties class Point{ Int32 _x; Int32 _y; public Int32 X { get{return _x;} set{_x = value;} } public Int32 Y { get{return _y;} set{_y = value;} } }
17. Methods class MyType{ public Int32 SomeMethod() { Int32 z = 0; // Code omitted… return z; } public static void StaticMethod() { // Do something } } MyType t = new MyType(); t.SomeMethod();
18. Method Parameters class MyType{ public Int32 AddNumbers(Int32 x, Int32 y) { Int32 z = x + y; return z; } } MyType t = new MyType(); t.AddNumbers(1,2);
20. Instantiating an Object public class MyType { public string MyStringField; public void InstanceMethod() { // Do something } } MyType o = new MyType(); o.MyStringField = “Hello”; String x = o.MyStringField; o.InstanceMethod();
21. Static Types public static class MyType{ public static void StaticMethod() { // Do something } } MyType.StaticMethod();
22. Constructors Constructors are used to initialize fields class Point { private Int32 _x; private Int32 _y; public Point() { // Setup code } public Point(Int32 xCoordinate, Int32 yCoordinate) { _x = xCoordinate; _y = yCoordinate; } }
23. Referencing an object Objects are reference types Pointer to memory MyType obj1 = new MyType(); obj2 = obj1; obj2.Color = “Red”; Console.WriteLine(obj1.Color)
24. Event Handling C# and VB have built in support for events Great for dealing with objects in an event-driven operating system More than one type can register interest in a single event A single type can register interest in any number of events
25. Event Handling class MyForm:Form{ MyForm(){ Button button = new Button(); button.Text = "Button"; button.Click += new EventHandler(HandleClick); Controls.Add(button); } void HandleClick(Object sender, EventArgs e){ MessageBox.Show("The Click event fired!"); } public static void Main(){ Application.Run(new MyForm()); } }
26. Designing Types: Accessibilities public – Accessible to all private – Accessible to containing class protected – Accessible to containing or derived classes internal – Accessible to code in same assembly
37. Designing Types: Polymorphism Use the virtual keyword to make a method virtual In derived class, override method is marked with the override keyword Example ToString() method in Object class Example derived class overriding ToString() public virtual string ToString(); class SomeClass:Object{ public override String ToString(){ return “Some String Representing State”; } }
39. Interfaces Define public members No Implementation Your types can implement interfaces Must implement all methods in the interface interface IName{ // Members } Class ClassName: IName{ }
40. Interfaces using System; class SomeType{}; class SortType:SomeType, IComparable{ Int32 val; public SortType(Int32 val){ this.val = val; } public Int32 CompareTo(Object obj){ return this.val - ((SortType)obj).val; } public override string ToString(){ return val.ToString(); } } class App{ public static void Main(){ SomeType[] objs = new SomeType[]{ new SortType(3), new SortType(1), new SortType(2)}; Array.Sort(objs); foreach(SomeType o in objs){ Console.WriteLine(o.ToString()); } } }
43. Airplane speed: int getSpeed(): int setSpeed(int) UML and Class Diagrams Class Diagram Name Member Variables name:type Methods name(parameters the method uses): return type
59. David Giard DavidGiard.com TechnologyAndFriends.com DavidGiard@DavidGiard.com Twitter.com/DavidGiard Special thanks to Chris Woodruff
Editor's Notes
Photo by meganpru(Creative Commons License)http://www.flickr.com/photos/meganpru/57943941/
Complexity of the problem domainThe problems we try to solve in software often involve elements of inescapable complexity, in which we find a myriad of competing, perhaps even contradictory, requirementsDifficulty to manage the development processThe fundamental task of the software development team is to engineer the illusion of simplicity – to shield users from this vast and often arbitrary external complexityFlexibility possible through softwareSoftware offers the ultimate flexibility, so it is possible for a developer to express almost any kinf of abstractionCharacterizing the behavior of discrete systemsA lot of software is designed to model discrete systems. Discrete systems are not easily modeled in procedural code.
Photo by All Glass Photo's photostream(Creative Commons License)http://www.flickr.com/photos/jim_rafferty_uk/2203549363/
Sample Dialog:In C# like any other object oriented language, the members in a type are basically either fields or methods. However, C# has a rich selection of field and method options.Fields are the data or state of an object or type, and methods are the functionality.Members come in two basic forms, instance and static. Instance members are the most common, and there will be one per instance or object. Static members are shared amongst instances and are one per type.Instance methods require an instance to be called, but static methods can be called directly just using the name of the type. The automatic “this” parameter is passed to instance methods, but not static methods.In .NET or managed code it is a common design pattern to create a class that cannot be instantiated, with nothing but static methods. This is a way of creating methods that are sort-of global, but are still logically grouped with other related methods. The System.Console class is an example of this design pattern.Speaker Instructions:You don’t have to say the stuff in that last paragraph, but concrete examples like these do help to nail down the ideas for the listeners.
As an object oriented programmer you will first and foremost create instances of types. This means that you will use a definition for a type, which has a name such as String or ArrayList, to create an actual object in memory. This object is structured based on the details described in the type’s definition.After you have created an object, you can use it by calling methods and/or referencing fields on the object. When you are finished with the object it must be cleaned up. Is some environments you do this explicitly; in others, such as C# or Java, cleanup is done for you by the system.Creating instances is a nice introduction to OO programming, but eventually you will have to define your own type. By doing so you create a new classification for a kind of object that can be created. You give the type a name, and you create members of the type such as methods and fields.It is important to distinguish between types and instances, so I will make an analogy. Think of the type as a descriptive tool (like a cookie cutter), while an instance is an object created from that description (in the same way that a cookie is created from a cookie cutter).
Static typesare most often used when you need to call a method repeatedly.It doesn’t make sense to waste the overhead of instantiating an object just to call a method.When using a static type, only one instance of the class is created.
Sample Dialog:C# supports constructor syntax much like C++. However, unlike C++ it is invalid to call a constructor method directly, so if you wish to implement a constructor in terms of another constructor you must use the special colon-this notation you see here.In this example, the simpler Point() constructor automatically calls the more complex constructor with the default x and y values of 0.You can indicate which base constructor to callUse the base keywordYou can implement simpler constructors in terms of more complex ones with the this keyword (suggested)Speaker Instructions:FYI, it is recommended that you have only one constructor for a type, and that the rest of the constructors be implemented in terms of this one. However, this is not always possible and must be handled on a type-by-type bases.
An object is a reference type.This means that variables that refer to an object are simply pointing to that object. Multiple variables can be pointing to the same object, which can cause confusion. Accessing any of these variables will access or modify the object.From the computer’s point of view, objects are data. They are the culmination of their fields and enough information to indicate their type. Often this data is complex and sizeable, and it is stored in the memory heap of the program that created the instance.Because objects so often live in the heap-memory of a program, the most common way of dealing with an instance is through a reference variable. The reference variable can be a global or local variable, or it can be a field in another object. Either way, there are some rules of reference variables.Reference variables have a type associated with them. For every object-type defined in an object oriented system, there is a matching reference variable type that is used to refer to instances of the type.Reference variables can refer to an instance or object, or they can refer to null (in most OO languages anyway). A null reference simply indicates that this variable does not refer to any object, but could have an object reference assigned to it.Reference variables do not always refer to objects of the exact sametype as the reference variable. This can be confusing, but in fact the rules are simple. A reference variable must refer to an object of matching type or it must refer to an object that is ultimately derived from the matching type.Looking back to the relationship between Automobile and Machine, a reference variable of type Automobile can only refer to an instance of Automobile; however a reference variable of type Machine can refer to an instance of Machine or an instance of Automobile. You can look at this as being possible, because Automobile is a Machine through the affect of derivation.Reference variables are your means of access to an object or instance. There are two related rules of reference variables.Regardless of what type of instance your reference variable refers to, it is the type of the variable that constrains how you can touch or affect the instance. Regardless of what type of reference variable you are using to refer to an object, the type of the object never changes for the life of the object.
Sample Dialog:C# has built in support for event notification and handling, which is great for dealing with OSs that are becoming increasingly event driven.Events in C# are very flexible compared to the virtual-function method of responding to system events.Speaker Instructions:We have several slides on this, so you don’t have to say everything on this slide.
Sample Dialog:The source code on this slide shows a simple class derived from Form. The interesting part, however, is how the class registers its interest in the Click event defined by an instance of the Button class. The MyForm class indicates which of its methods (in this case the HandleClick method) should be called when the event is fired.
Sample Dialog:Like C++ and Java, C# supports member and class accessibility. The default accessibility is private, and you must decorate members with an accessibility modifier to set a different accessibility.The most common accessibilities are public, private, and protected. However, C# introduces the idea of an internal accessibility which allows access by any class in the same binary assembly file. This is similar to, but more flexible than the C++ friend class.Speaker Instructions:You could spend forever talking about accessibility. If you like you can edge into the direction of design by saying that fields should be private, and methods can be public, etc. But be careful, because you could sink ten minutes on this slide alone.
Encapsulation and Abstraction are related concepts
Encapsulation is the hiding of the internal mechanisms and data structures of a software component behind a defined interface.We don’t need to understand the details of how the component works internally in order to use that component.Increases integrity by preventing external users from setting internal state of a component to an invalid state.
Abstraction is simplifying complex reality by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.
The object structure is important because it illustrates how different objects collaborate with one another through patterns of interaction.
A type defines a number of fields and methods. A derived type inherits the base type’s fields and methods, and adds more of its own, to become a new type (extending from the exisiting one).
Polymorphism is closely related to type-derivation and reference variables. But it is an advanced topic, and can be difficult to describe. The goal is to allow a generic piece of code to work with objects or instances generically; meanwhile we want the objects themselves to do the right thing based on their respective types.
Sample Dialog:To create a virtual function in C# you must attribute your method with the virtual keyword. Virtual methods can be public, protected, or protected internal.To override a virtual method in a derived class you must explicitly indicate your intent using the override keyword. Your derived method must also match the base method signature exactly.Speaker Instructions:The override keyword is actually part of the versioning story in the .NET Framework. But this is probably a bit too much to explain in the discussion. However, for your own study you may want to look up the “override” and “new” keywords in the documentation, as this is an example of a feature that is much more well thought out than the equivalent feature in Java.Another Example:using System; class App{ public static void Main(String[] args){ Object[] objects = new Object[]{ new Lemon(), new Grapefruit(), new Truck(), new Lemon(), new Lime() };foreach(Object o in objects){Console.WriteLine(o.ToString()); } }} class Citrus{ public override String ToString(){ return "I am a "+this.GetType().ToString(); }} class Lime:Citrus{} class Lemon:Citrus{} class Grapefruit:Citrus{} class Truck{ public override String ToString(){ return "Truck here"; }}
Sample Dialog:Interfaces are an excellent way for a type to take on a role without being derived from a type that defines this role. For example, two totally unrelated types can be sortable and therefore take on the well-defined ability to be sorted, even though they are not derived in the same derivation hierarchy. This is possible through interfaces.In C# you cannot have multiple base classes, but a type can implement any number of interfaces.You will find that you implement pre-existing interfaces often, but from time to time you will have to define your own interface. Interfaces can include only methods, properties, and events. It is not valid for an interface to include a field. Interfaces are defined using the interface keyword as shown here.
Interfaces are a great feature of object oriented programming. Some languages such as C++ support a concept called multiple derivation. The .NET Framework does not allow for multiple inheritance, and as a result neither does C#. Sometimes, it is nice, however, for a type to be able to take on several roles. This is where Interfaces come in.
When Designing a complex software project, it is essential to decompose it into smaller and smaller parts, each of which we may then refine independently.
Poster by Derrick Bailey
Poster by Derrick Bailey
Poster by Derrick Bailey
Code sample from Chander Dahl’s presentation, based on writings of Robert Martin