Top Banner
Embrace Factories Factories Embrace Factories Embrace Factories Factories Factories By Rob Gonda
29

Embrace Factories Factories - CFUnited

Feb 03, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Embrace Factories Factories - CFUnited

Embrace FactoriesFactories

Embrace FactoriesEmbrace FactoriesFactoriesFactories

By Rob Gonda

Page 2: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Brief History of OOP for CFBrief History of OOP for CFBrief History of OOP for CFOnce upon a time…Procedural Code

Spaghetti CodeOrganized

a) Includesb) Modules

OOP / CFC (mx+)Objects as containersThe Big ObjectScoped / Persistent ObjectsEncapsulation?Relations / Wiring / Dependencies

Page 3: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

OOP: The ObjectOOP: The ObjectOOP: The Object

BasicsLifecycle of a CFCConstructors / WorkaroundsTypes of Objects

Page 4: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

OOP: The Object: BasicsOOP: The Object: BasicsOOP: The Object: Basics

Class: A class abstractly defines a thing, including the thing's characteristics (attributes or properties), behaviors, or methodsObject: A particular instance of a class.Method: An object's abilities –Function

Page 5: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

OOP: The Object: LifecycleOOP: The Object: LifecycleOOP: The Object: Lifecycle

Gets created runs pseudo-constructorCfobject / createObject

Gets initialized CF has no constructor method. init() method commonly used

Returns the instance of the object (this)Use methods / functionsDestroy component CF has no deconstructor method.

Page 6: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

OOP: The Object: TypesOOP: The Object: TypesOOP: The Object: Types

Business Model LayerTransient – stateful

a) People, places, and things

Service LayerPersistent – stateless

a) Services and infrastructure

© Sean Corfield

Page 7: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

ProblemsProblemsProblems

Object Creation / InitializationRelationships / wiringManageability / moving objects / keeping pathsCohesion / Loose couplingUnit Testing

Page 8: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Design Patterns DefinedDesign Patterns DefinedDesign Patterns Defined

In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design.

Page 9: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

The Object FactoryThe Object FactoryThe Object Factory

Object with main role of collecting the information necessary to create an instance of the intended object's class and then to invoke that class's constructor.Typically quite simple and small.

Page 10: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

The Factory PatternThe Factory PatternThe Factory Pattern

The factory method pattern is an object-oriented design pattern. Like other creational patterns, it deals with the problem of creating objects without specifying the exact class of object that will be created.

Page 11: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Why FactoriesWhy FactoriesWhy Factories

Object Factory creates objectsCentralize repository for path and dependenciesAll objects depend on the factory

Factory is passed to all objectsObjects request the factory for instances of other objects

Page 12: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Inversion of ControlInversion of ControlInversion of Control

Inversion of Control, also known as IoC, is an important object-oriented programming principle that can be used to reduce coupling inherent in computer applications.

Page 13: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Dependency InjectionDependency InjectionDependency Injection

Dependency injection (DI) is a programming design pattern and architectural model, sometimes also referred to as inversion of control or IoC, although technically speaking, dependency injection specifically refers to an implementation of a particular form of IoC.

Page 14: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Dependency Injection IIDependency Injection IIDependency Injection IIDependency injection is a pattern in which responsibility for object creation and object linking is removed from the objects themselves and transferred to a factory. Dependency injection therefore is inverting the control for object creation and linking, and can be seen to be a form of IoC.There are three common forms of dependency injection: setter, constructor, and interface-based injection.

Page 15: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Dependency Injection IIIDependency Injection IIIDependency Injection IIIDependency injection is a way to achieve loose coupling. The technique results in highly testable objects, particularly when applying test-driven development using mock objects: Avoiding dependencies on the implementations of collaborating classes (by depending only on interfaces that those classes adhere to) makes it possible to produce controlled unit tests that focus on exercising the behavior of, and only of, the class under test. To achieve this, dependency injection is used to cause instances of the class under test to interact with mock collaborating objects, whereas, in production, dependency injection is used to set up associations with bona fide collaborating objects.

Page 16: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

ColdSpring: what, whyColdSpring: what, whyColdSpring: what, why

“Spring is the best thing to happen to programming in 20 years” – AntranigBasman, Reasonable Server Faces (RSF) Lead, SEPP Conference Vancouver 2006

ColdSpring was “inspired” by Spring – not porting all the functionality, but solving the same problems.

© Dave Ross

Page 17: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

ColdSpring II: DefinitionColdSpring II: DefinitionColdSpring II: Definition

ColdSpring is a inversion-of-control framework/container for CFCs that allows for constructors and setters dependency injectionColdSpring borrows its XML syntax from the java-based Spring Framework, but ColdSpring is not necessarily a "port" of Spring

Page 18: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

ColdSpring III: PolymorphismColdSpring III: PolymorphismColdSpring III: Polymorphism

Components don’t care about their collaborators implementation, thus we have a perfect environment for “swap-ability”. Swap one implementation of a component out for another and collaborators wouldn’t know the difference – the famous Duck-TypingPerfect for Unit testing: swap dependencies per abstract classes – based on interfaces

Page 19: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

ColdSpring: Tiers, LayersColdSpring: Tiers, LayersColdSpring: Tiers, Layers

© Dave Ross

Page 20: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Code Examples: plainCode Examples: plainCode Examples: plain

Scope components in applicationSince we don’t want to break encapsulation, each component re-initialize all dependencies

Needs to know how to initialize dependenciesMultiple instances of the class in memory

Page 21: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Code Examples: settersCode Examples: settersCode Examples: setters

Scope components in applicationUse setters to pass instances of dependencies to each other.

Page 22: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Code Examples: factoryCode Examples: factoryCode Examples: factory

Scope the factory in applicationGet components from factoryOnly factory knows how to initialize componentsFactory passes itself to componentsComponents use factory to retrieve dependencies

Page 23: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Code Examples: factory DICode Examples: factory DICode Examples: factory DI

Scope the factory in applicationGet components from factoryOnly factory knows how to initialize componentsFactory injects dependencies to componentsComponents don’t even know the factory exists

Page 24: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

ColdSpring: The Config FileColdSpring: The ColdSpring: The ConfigConfig FileFile

BeansIDClass

DependenciesPropertyConstructor

Page 25: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Code Examples: ColdSpringCode Examples: ColdSpringCode Examples: ColdSpringScope ColdSpring in applicationPass configuration file to ColdSpringGet components from ColdSpringOnly ColdSpring knows how to initialize componentsColdSpring injects dependencies to componentsComponents don’t even know the ColdSpring exists

Page 26: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Code Examples: ColdSpring AutowiringCode Examples: ColdSpring Code Examples: ColdSpring AutowiringAutowiring

Scope ColdSpring in applicationPass configuration file to ColdSpring – Introduction to AutowiringGet components from ColdSpringOnly ColdSpring knows how to initialize components – Built-in autowiring logicColdSpring injects dependencies to componentsComponents don’t even know the ColdSpring exists

Page 27: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

ColdSpring: Auto wiringColdSpring: Auto wiringColdSpring: Auto wiring

ColdSpring will inspect constructor and setter methods and identify all dependencies by either type or namePros:

Less XMLCons:

No documentation

Page 28: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

ColdSpring: Other PerksColdSpring: Other PerksColdSpring: Other Perks

Aspect Oriented ProgrammingOnly Chris understands this :->

Front Controller Framework IntegrationMach IIModel GlueColdbox

Flash Remoting Code Generation and mapping

Page 29: Embrace Factories Factories - CFUnited

June 27th- 30th 2007 www.cfunited.comRob Gonda - www.robgonda.com

Thank YouThank YouThank You

Questions / Comments?Blog: http://www.robgonda.comCorp: http://www.ichameleongroup.comemail: [email protected]