Requirements are moving targets. It is hard for a static programming language to keep up with changes of requirements of a software system. Interestingly, we have seen increasing efforts to make C# programming language more dynamic with
dynamic keyword in C# and
ExpandoObject type in .NET 4.0.
With Component-Based Object Extender (CBO Extender), an object extensibility framework for .NET, behaviors can be added to object at runtime. It makes a software system more flexible and adaptive to changes.
Static Typing, Dynamic Typing and Dynamic Behaviors
Static typing is class-based programming in which a class has to be defined at design time prior to an object can be created out of it and used. In class design, methods are molded into classes, which is both a blessing and curse. On the brighter side, molding methods into classes is at the core of objects being able to receive messages and the ability to define standard protocols. On the darker side, explicitly molding every single method to a specific class leads to programs that are rigid and hard to evolve and maintain. Static typing is facing serious challenges from today’s constantly changing business requirements while software systems are continuously getting bigger and more complicate.
Dynamic typing is prototype-based programming in which classes are not present, and behavior reuse is performed via a process of cloning existing objects that serve as prototypes. With dynamic typing, you can add, delete or alter methods of an object at runtime anywhere in a program. It is so flexible that it is hard to maintain correctness, safety and predictability in a large software system. It does not seem to be a good solution to the challenges that static typing is facing.
Then, how can we make the class-based programming more dynamic without sacrificing its strengths? Well, if you think about the dynamic typing a bit more, you will find ultimately what we need from dynamic typing is its ability to add behaviors at runtime (dynamic behaviors). However, it does not mean that dynamic behaviors can only be provided by dynamic typing. Proxy-based programming can, too, add behaviors at runtime by decorating methods of object. In proxy-based programming, a dynamic decorator is used to attach behaviors to an object and returns a proxy of the object. To client of the object, the proxy is still the same type and is used as if it was the original object.
Proxy-based programming complements class-based programming with dynamic behaviors, which makes a software system more flexible and readily adaptive to changes without involving dynamic typing. With class-based programming, an application developer can expect a software component with core business functionality and well-defined client contract (interfaces). With proxy-based programming, the application developer can add extra functionality to objects of the component as needed at runtime.
Dynamic Behaviors with CBO Extender
Perhaps, one of most important accomplishments coming out of class-based programming is the concept of interface (client contract). With interface, a client does not need to know about the inner workings of a component in order to make use of it. By programming to interface, the client is completely decoupled from the implementation of a component. Consequently, a component can replace another (at design time or runtime) as long as the successor component implements the same interface as the initial component without breaking the system in which the component operates.
CBO Extender is proxy-based and uses programming to interface. With CBO Extender, you define a set of aspect methods to represent behaviors based on your business or system requirements. These behaviors, then, can be used to decorate object’s methods as needed in your application using its Dynamic Decorator or AOP Container.
CBO Extender works directly with objects and interfaces. It does not know or care, and is completely decoupled from, the classes from which objects are created. Any interface methods can be decorated with extra behaviors at runtime. Cross-cutting concerns like logging, security checking or transaction management can be presented as dynamic behaviors which are added to objects at runtime as needed (see article Dynamic Object Programming for examples). Business requirements like sorting or anything (e.g., Hello World!) can also be presented as dynamic behaviors which are added to objects at runtime (see articles Application Development With Component-Based Object Extender and OOP, AOP and OP).
- Mixing static typing with dynamic typing does not solve the rigidity associated with class-based programming.
- Proxy-based programming complements class-based programming with dynamic behaviors.
- Dynamic behaviors make software system flexible and adaptive to changes.