21
OO Crash Course NVCFUG March 2015 Presented by Denard Springle

OO Crash Course NVCFUG March 2015 Presented by Denard Springle

Embed Size (px)

Citation preview

Page 1: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

OO Crash CourseNVCFUG March 2015

Presented by Denard Springle

Page 2: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

What is an Object?• Die– Sides• Dots (or

number, or image, etc.)

– Color– Shape

*Properties*

Page 3: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

What can be done with an object?• A die can be:

– Thrown – the result of the throw being the die lands on one side, with the other side facing (the result)

– Combined – with other die throws, the result of which can be manipulated (added, multiplied, divided, etc.) depending on the [business] logic desired

– Saved – the die could be placed on a shelf in the last thrown configuration

– Retrieved – the die could be taken off the shelf in the last thrown configuration

– Discarded – the die and it’s last thrown configuration can be thrown away– Rethrown – the result of the rethrow overwriting the previous result– Etc.

*Methods*

Page 4: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

Bean (aka Data Model)• ‘Bean’ is a Java term for ‘Object’ (loosely).• Data Model (or more simply, ‘model’) is the

modern term for ‘Object’ (loosely).• Model’s are used to instantiate an Object –

they return a single instance of a single Object.

• Can instantiate the same model multiple times, each with it’s own Object instance.

Page 5: OO Crash Course NVCFUG March 2015 Presented by Denard Springle
Page 6: OO Crash Course NVCFUG March 2015 Presented by Denard Springle
Page 7: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

Data Access Object (DAO)• Manages all CRUD methods for working

with a single model (bean, object).• Expects and returns Objects (models,

beans).• All other code interacts with the DAO so

that data access occurs from the same location in code.

Page 8: OO Crash Course NVCFUG March 2015 Presented by Denard Springle
Page 9: OO Crash Course NVCFUG March 2015 Presented by Denard Springle
Page 10: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

Gateways• Manages all data when working with

multiple rows (e.g. in a query).• Expects [filter] parameters and returns a

query (or array).• All other code which reads multiple rows

interacts with the gateway so that queries occur from the same location in code.

Page 11: OO Crash Course NVCFUG March 2015 Presented by Denard Springle
Page 12: OO Crash Course NVCFUG March 2015 Presented by Denard Springle
Page 13: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

Service LayerA combination of:

•Data Access Object (DAO) for working with a single instance of a model.

•Gateway for working with multiple rows of data (filter, cache, search, etc.).

•Utility methods that manipulate the data in other ways (convert from query to array, return a JSON or XML representation of a model instance, etc.) – this is often done in a baseService.cfc which other service layers extend (inherit).

Page 14: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

Why models and services?• Maintainability – changes only need to occur in a handful

of places within a few files (depending on implementation).• Abstraction – the application only needs to know what

data to pass and receive to the service(s).• Ease of implementation – services can be injected by a

factory, convention over configuration architecture can be employed, etc.

• Ease of understanding – other developers can readily understand the functions derived from using standard OO methodologies.

Page 15: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

Centralized Data Management• Using models and services centralizes data

management for the rest of the application.• Services can be invoked from

handlers/controllers, passing data to views or being passed the data from forms, etc.

• Services can be invoked from API’s, scheduled tasks, gateways, etc. while always using the same functions and expectations as any other part of the application.

Page 16: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

OO begets MVC (FW/1, ColdBox)• The ‘model’ in Model-View-Controller frameworks refers to the

beans and services used for data management and manipulation.• Without OO fundamentals, MVC is not feasible (though

technically possible, but ugly and defeats the purpose).• Services do the heavy lifting - *not* controllers and *not* views –

controllers should pass and return data between the model and the view – no more, no less. – This view is being requested – this data should be retrieved from the

service and handed off to the view. – This [form] data is being received – this data should be handed off to

the service and the service will hand me back data to hand off to (the/another) view.

Page 17: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

Utility Functions Are Services, Too!

• Day to day programming involves many aspects of security and validation that are well handled by services.– Authentication, Access Control, Session

Management– Form [API, etc.] validation and cleansing– Encryption/Decryption– Any function not specific to an individual object

used by multiple parts of an application

Page 18: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

Composition• A die is composed of multiple sides

– Each side of each die can contain different information (e.g. number of dots, numeric representation (1, 2, 3, etc.), images/icons/graphics (cow, dog, bird, etc.)

– ‘sides’ would be a separate table, related to the ‘die’ id for each side, and would be comprised of information about that side

– ‘side’ could be a model, and each die would have X number of ‘sides’– ‘sides’ can be also be composed into the ‘die’ model as a property.

The service layer(s) would then have to handle CRUD for both tables based on the data in the model (optimally, you would call a ‘sides’ service from the ‘die’ service)

Page 19: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

Inheritance• Provides the extending component access to (and ability to

override) the parent components methods (functions)• All frameworks work by extending your Application.cfc

from the framework’s own CFC – thereby giving your application access to all of the frameworks methods (functions)

• baseModel.cfc and baseService.cfc – used to store generic model and service related utility functions common to all models and services (e.g. getMemento() for models, queryToArray() for services) – all other models and services extend their base model or service, respectively.

Page 20: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

Other Design Patterns• Factory method – abstracts the instantiation of objects

(models and services).• Dependency Injection – injects instantiated models and

services from the factory on an as-needed basis.• Façade – Inheritance based pattern that allows implementation

and overwriting of parent methods in an abstracted fashion• Modern frameworks (FW/1, ColdBox, etc.) make use of design

patterns to abstract the complexity away and provide you with a consistent, reliable method of building MVC applications. They are steeped in OO methodologies and work best when utilized in that expected capacity.

Page 21: OO Crash Course NVCFUG March 2015 Presented by Denard Springle

ResourcesGang of Four Design Patterns - http://en.wikipedia.org/wiki/Design_Patterns

http://objectorientedcoldfusion.org/ - Kevan Stannard (dated but relevant)

http://www.learncfinaweek.com/week1/OOP/ - Adam Tuttle (and friends)

http://bit.ly/cfoobook1 - Matt Gifford (dated but relevant)

http://bit.ly/cfooblog1 - Adrian J. Moreno (dated but relevant)