CFCs and Design patterns - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

CFCs and Design patterns

Description:

cfset myObj = createObject('component','person' ... Need to create your own types for structures to be passed to CF defined web services ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 45
Provided by: david379
Category:
Tags: cfcs | create | design | own | patterns | person | your

less

Transcript and Presenter's Notes

Title: CFCs and Design patterns


1
CFCs and Design patterns
  • ColdFusion Components Best Practices and Design
    Patterns
  • JaxFusion 11/15/2004

2
quotes
  • When I invented the term Object-Oriented, and I
    can tell you I did not have C in mind. (Alan
    Kay)
  • C makes it easy to shoot yourself in the foot
    C makes it harder, but when you do, it blows
    away your whole leg. (Bjarne Stroustrup)

3
What will this presentation cover
  • How to make a CFC
  • How to consume a CFC
  • Designing applications using Object Oriented
    Design Patterns
  • Real world CFCs

4
Why use CFCs and OO design patterns
  • So you can stay employed
  • ColdFusion Components allow for code reuse.
  • Most Object Oriented features are available in
    CFCs, i.e. Encapsulation, Inheritance
  • Allows true modeling of data.

5
Basic Object Oriented Concepts
  • Encapsulation
  • Composition
  • Inheritance
  • Polymorphism
  • Persistance

6
Encapsulation
  • Encapsulation is the ability to store some of the
    complexity of the information in a CFC without
    exposing that complexity to the consumer of the
    object.

7
Composition
  • An object can be composed of other objects that
    are not subtypes of that object. The parent
    object can aggregate other objects.

8
Inheritance
  • Inheritance is the ability of a CFC to inherit
    the properties and methods of a parent CFC.
  • ColdFusion MX only allows for single inheritance

9
Polymorphism
  • Polymorphism is the ability of a CFC to change
    the behavior of inherited properties from a
    parent CFC
  • ColdFusion only allows for method overriding. Can
    not do method overloading.

10
Persistence
  • A CFC object can be created once, and reused to
    maintain persisted information in the object

11
Difference between Classes and Objects
  • A CFC is like a Java Class. It is like a template
    for an Object
  • Once a CFC has been instantiated or created, it
    becomes an Object

12
CFC Tags
  • ltcfcomponentgt
  • ltcfpropertygt
  • ltcffunctiongt
  • ltcfargumentgt
  • ltcfreturngt

13
ltcfcomponentgt
  • The cfcomponent tag is the generic wrapper for
    the entire file. Most parameters in this tag are
    for hinting and self documentation.

14
ltcfpropertygt tag
  • The cfproperty tag is used to broadcast component
    properties to a web service.

15
ltcffunctiongt
  • The cffunction tag is used for creating methods
    in cfcs and cfm files.

16
ltcfargumentgt
  • The cfargument tag is used to predefine the
    parameters that can be passed in the cffunction
    tag.
  • This allows for simple data type validation.

17
ltcfreturngt
  • The cfreturn tag is used to return the data from
    the function to where the function was called.
  • Foo myObj.getFoo()

18
Component scopes
  • Arguments scope
  • This scope
  • Variables scope
  • Super scope (new 6.1)
  • Var for function specific variables

19
Arguments scope
  • The arguments scope contains the variables passed
    to the functions arguments.

20
This and variables scope
  • The this and variables scope are equivalent
    to the public and private variable modifiers
    in Java.
  • This scoped variables can be seen as object
    properties outside of the component. Variables
    scoped variables are local to the component, and
    can not be seen outside of the component

21
Super scope
  • The super scope allows the component to call
    methods from the parent component when using
    inheritance.

22
Var
  • Var allows for variables to be used only in the
    function, so once the function has ended, the
    variable does not exist in the component, and
    will not persist.

23
Cffunction parameters
  • Name parameter is the name of the function
  • NamegetFoo parameter would be referenced as
    getFoo() .
  • Accesstype is the access mofifier for the
    function. How can this function be accessed, i.e.
    Public, Private
  • Returntype is the type of value returned from the
    function

24
Cfargument parameters
  • Name is the name of the argument.
  • Type will be the datatype, i.e. string, numeric,
    Boolean, or your own types
  • Required is self explanatory. Set with a Boolean
    value
  • Default will allow you to set a automatic value.

25
Cfc features
  • No constructor or init method required
  • All code before the first function is run on
    object creation.
  • Can display text in a function, but will not work
    for web services or flash remoting

26
Consuming CFCs
  • Cfobject tag
  • Cfinvoke tag
  • createObject function

27
ltcfobjectgt
  • Will instantiate without executing a method
  • ltcfobject namemyObj componentperson /gt

28
CFinvoke and cfinvokeargument
  • Will instantiate an object and execute method at
    the same time
  • ltcfinvoke component"person" method"setPerson"
    returnvariable"personObj"gt
  • ltcfinvokeargument name"FirstName" value"Joe"
    /gt
  • ltcfinvokeargument name"LastName" value"Dirt"
    /gt
  • lt/cfinvokegt

29
createObject function
  • Has the same functionality as cfobject and
    cfinvoke but works as a method
  • ltcfset myObj createObject(component,person)
    /gt
  • First parameter is the object type, i.e. java,
    webservice
  • Second parameter is the component path

30
Referencing a CFC
  • CFCs can be placed in the Web root, Custom Tag
    paths and mappings
  • D\Inetpub\wwwroot\com\fekke\person.cfc
  • createObject(component, com.fekke.person)
  • The . delimited path is referred to as the
    package name

31
Modeling relationship
  • Two kinds of entity relationships
  • Is-a relationships
  • Has-a relationships

32
Is-a relationships
  • Is-a functionality most common using inheritance.
  • Can take advantage of super scope
  • Use parent properties

33
Has-a relationships
  • Has-a relationships can use composition to store
    relationship
  • An object can store another object
  • A person can have an address or a cart can have
    an item

34
Getter and Setter methods
  • Get and Set methods should be used to set
    individual component parameters
  • setFoo(myFoo)
  • getFoo() would return value set with set foo

35
Create you own data types
  • Need to create your own types for structures to
    be passed to CF defined web services
  • Will allow you to create beans for Mach-II.
  • Self defined types is the Foundation for most
    design patterns

36
Design Pattern examples
  • MVC or model view controller
  • MVC allows for more code reuse, and makes it easy
    to maintain because data model is separated from
    control and view functionality
  • J2EE EJB design pattern
  • Similar to web service
  • Create and pass objects using stubs to reassemble

37
OO versus RDBMS
  • Difficult to store complex objects in a
    relational database
  • DAO or data access objects are a good way to
    store model in a database
  • XML can be used to store model
  • Object and XML databases becoming more common

38
Design Pattern Class types
  • Similar to GoF (Gang of Four)
  • Bean or Entity types
  • Managers
  • Gateways
  • Factories
  • Session Facadé

39
Real world Best practices
  • Dont use scoped variables inside components,
    i.e. session, request, client and application
  • Dont use components to display. This will not
    work in flash gateway or web services
  • Try to avoid creating components in the session
    scope

40
Best practices continued
  • Dont use cfmodule or custom tags in components
  • Try not to use cfinclude tag
  • Branch offset error can be fixed by using
    cfinclude
  • May need to reference private methods in the this
    scope
  • Use the KISS principle
  • Keep It Simple Stupid!

41
Best practices continued again
  • Dont overuse inheritance
  • Can use named parameters in method calls. This is
    helpful when you have many arguments that need to
    be passes
  • myObj.getResults(id attribute.ID,searchText
    attributes.text,Datasource request.datasource)

42
Useful development tools
  • Dreamweaver MX 2004
  • Component Browser
  • Cfdump tag
  • CFeclipse plugin

43
CFC related URLs
  • Macromedia.com/devnet
  • Cfczone.org
  • Techspediation.com
  • Mach-ii.com
  • Mach-ii.info
  • Seancorfield.org/blog
  • Halhelms.com

44
Recommended books
  • Discovering CFCs by Hal Helms
  • ColdFusion WACK by Ben Forta
  • Design Patterns by Gamma, Helm, Johnson,
    Vlissides
  • Anti-patterns by William J. Brown
Write a Comment
User Comments (0)
About PowerShow.com