#6 Object Oriented Design Patterns

#6 Object Oriented Design Patterns -...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Extending Object-Oriented  Programming OOP was originally introduced as programs became larger and more complex. The idea was to wrap functionality inside objects. In other words, the inspiration was to divide and conquer. Until OOP appeared, you could divide your code into functions, but that wasn’t enough in the long run. As programs became longer and longer, some way of dividing them up in terms of easily handled concepts was needed. What those concepts were, depended on the program itself, and those concepts came to be known as objects.
Background image of page 2
The big four OOP building  blocks Abstraction is the good kind of breakdown Abstraction isn’t a programming technique; in essence, it just means that you conceptualize a problem before applying OOP techniques. Abstraction is all about breaking your approach to a problem into natural segments. Working with design patterns often means spending more time on the abstraction part of the process than on the concrete classes part.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The big four OOP building  blocks Encapsulation - you wrap methods and data up to the object You remove the complexity from view and make it into an easily graspable object. When you encapsulate functionality into an object, you decide what interface that object exposes to the world. you decide what getter and setter methods and/or public properties your objects present to the rest of the application so that the application can interact with it.
Background image of page 4
The big four OOP building  blocks That’s the idea behind encapsulation — you hide the complexities inside objects and then create a simple interface to let that object interact with the rest of your code. Design patterns are particularly big on encapsulation. One of the primary design insights here is that you should encapsulate what changes the most. Extracting the part of your code that changes the most, or that needs the most maintenance, and encapsulating that part into its own object for easier handling.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The big four OOP building  blocks Polymorphism: the ability to write code that can work with different object types and decide on the actual object type at runtime. For example, you might want to write code that handles all kinds of different shapes — rectangles, circles, triangles, and so on. Although they’re different shapes, they all have in common certain actions as far as your code goes — for example, they can all be drawn.
Background image of page 6
Start with this Shape class that draws a generic shape when  you call its draw method: class Shape { public virtual void Draw() { MessageBox.Show("Drawing a Shape"); } }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Then you inherits a new class, Rectangle, from Shape, and  let it draw a rectangle when you call its draw method as  follows: class Rectangle : Shape { public override void Draw() { MessageBox.Show("Drawing a Rectangle"); } }
Background image of page 8
Draw a Shape Shape myShape = new Shape(); myShape.Draw(); myShape = new Rectangle(); myShape.Draw();
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 46

#6 Object Oriented Design Patterns -...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online