Code like a ninja session 1 object-oriented principles

16
CODE LIKE A NINJA SESSION 1 - INTRODUCTION TO OBJECT-ORIENTATION

description

Session 1/8 on Object Orientation, SOLID and Design Patterns. Code can be found at: https://github.com/SheepWorx/Training

Transcript of Code like a ninja session 1 object-oriented principles

Page 1: Code like a ninja  session 1   object-oriented principles

CODE LIKE A NINJASESSION 1 - INTRODUCTION TO OBJECT-ORIENTATION

Page 2: Code like a ninja  session 1   object-oriented principles

SESSION RESOURCES

• Presentation session notes including link to this session, will be available on http://learningaboutfudge.blogspot.com

• All the source for this session is publically available at: https://github.com/SheepWorx/Training

• Source was compiled using Visual Studio 2012

Page 3: Code like a ninja  session 1   object-oriented principles

WHY DO WE NEED TO KNOW THIS?

• Dependency Management is an issue that most of us have faced.

• Poor dependency management leads to code that is hard to change, fragile, and non-reusable.

• When dependencies are well managed, the code remains flexible, robust, and reusable.

- Robert C Martin (father of OOD)

Page 4: Code like a ninja  session 1   object-oriented principles

WHAT WE WISH TO ACHIEVE

The aim to write code that is

• Testable

• Reusable

• Extensible

• Maintainable

• Less buggy

Page 5: Code like a ninja  session 1   object-oriented principles

PRIMARY PRINCIPLES

• Encapsulation

• Abstraction

• Inheritance

• Polymorphism

• Decoupling

Page 6: Code like a ninja  session 1   object-oriented principles

ENCAPSULATION

Definition: the hiding of non-essential features

Laymen’s terms: making something Public, Protected, Internal, Protected Internal or Private

[Code: OOPrinciples\Encapsulation]

Page 7: Code like a ninja  session 1   object-oriented principles

ENCAPSULATION (CONT)

• Public : Access is not restricted.

• Protected : Access is limited to the containing class or types derived from the containing class.

• Internal: Access is limited to the current assembly.

• Protected Internal: Access is limited to the current assembly or types derived from the containing class.

• Private : Access is limited to the containing type.

Page 8: Code like a ninja  session 1   object-oriented principles

ABSTRACTION

Definition: To only represent essential features of an object.

Definition 2: To reduce and factor out details so that once can focus on a few concepts at a time

Laymen’s terms: Reduce code redundancy and enable code reuse.

[Code: OOPrinciples\Abstraction]

Page 9: Code like a ninja  session 1   object-oriented principles

INHERITANCE

Definition: a Mechanism to enable code reuse by abstracting code to subclasses or defining standard signatures using interfaces

Laymen’s terms: Creating child-parent relationship between classes using normal classes, abstract classes or interfaces

Page 10: Code like a ninja  session 1   object-oriented principles

INHERITANCE (CONT)

[Code: OOPrinciples\Inheritance]

Before After

Page 11: Code like a ninja  session 1   object-oriented principles

POLYMORPHISM

Definition: the provision of a single interface to entities of different types

Laymen’s terms: Abstracting common functionality to a parent class and then creating child classes by inheriting the parent class, inheriting any accessible methods and properties along with it

Page 12: Code like a ninja  session 1   object-oriented principles

POLYMORPHISM (CONT)

[Code: OOPrinciples\Polymorphism]

Page 13: Code like a ninja  session 1   object-oriented principles

DECOUPLING

Definition: disconnecting an abstraction from its implementation so that the two can vary independently

Laymen’s terms: Introducing a potato-class. If class1 and class2 are tightly-coupled by the one instantiating the other, to split off the dependency by introducing a 3rd class, usually in the form of an interface.

Page 14: Code like a ninja  session 1   object-oriented principles

DECOUPLING (CONT)

Page 15: Code like a ninja  session 1   object-oriented principles

DECOUPLING (CONT)

Page 16: Code like a ninja  session 1   object-oriented principles

WHAT’S NEXT

Topic: S.O.L.ID – The Single Responsibility Principle

Where: 14:00-15:00 @ W4