The Component Object Model Information Technology Essay

Published: November 30, 2015 Words: 3090

polymorphism is integral part of object oriented programming. It is one of the great feature of object oriented programming along with encapsulation, inheritance etc. Basically there are two types of polymorphism, one is runtime polymorphism and another is compile time polymorphism. This paper tries to find out these two polymorphisms inside component object model and .net technologies. This paper also defines the overview of component object model technology and polymorphic behavior of component object model and .net. Polymorphism works at .net technology in similar way it works at object oriented programming. Here Method overriding works with defining identifier, virtual at base class and override at derived class. This paper also defines the polymorphic behavior of component object model which is not similar like object oriented approach. In component object model component inheritance is not feasible because components are made to work independently and communicate through their interface. Only polymorphism in component object model is implemented by interface inheritance.

Introduction

Object oriented programming is the most familiar approach inside software engineering field. Polymorphism is one of the benefits of object oriented technique. Object oriented programming features can be extended and reused using this technique .Override and overloading are the two form of polymorphism. As an integral part of object oriented programming approach polymorphism is also used frequently in other technologies. .Net supports full feature of polymorphism like object oriented approach. In term of component it is not supported as smoothly as .Net due to some contradiction with component architecture. Components are not built to access ease through source code. Source codes are encrypted in component and it is only assessable by component interface. So component object model extends features in different way thus polymorphism is occurred differently from traditional object oriented and .Net technology.

The goal of this paper is to draw a connection in between polymorphism of two different architectures, component object model and .net. Section 2 discusses brief overview on component object model. Section 3.1 explains static polymorphism. Section 3.2 discusses dynamic polymorphism. Section 3.3 discusses polymorphism in .Net. Section 3.4 discusses polymorphism in term of interface and object perspective. Section 3.5 discusses Polymorphism in Component Object Model and finally section 3.6 discusses the problem area in Component Object Model in term of polymorphism. Section 4 draws the conclusion of the paper.

Component Object Model

Component object model (COM) is a standard architecture to combine different components to use together to form a software application. Components can be developed by different vendors with different language, but using this underlying architecture they are interacted with each other. There were a lot of disadvantages to using developed components. Component object model has given a common platform to use components that is developed at different time, technology, language and vendors.

There are some disadvantages of component. These are addresses in the following,

Basic interoperability- how the component will interact with another component? It is a main concern issue for a developer that how his developed component will interact with other components.

Versioning - how the component will be updated while it is inheriting methods of another component.

Programming Language difference- components is developed in different language but here it is the same problem, interoperability issue.

Component object model is solved all these problems. In advanced it is also added some extra functionalities. Some advantages of component object model are described below:

Binary standard- binary standard is a standard to call functions in Component object model. So the languages that support the scenario can be used to develop components and used in component based model.

Runtime polymorphism-Appropriate and suitable components are selected at run time environment. Any change or upgrade in component doesn't need to recompile or change in client application.

Location transparency-component client can communicate with components reside at anywhere.

Memory management- it is an additional feature addressed by COM. Components share memory.

Multi platform is supported

Component is loaded dynamically etc.

Components communicate through component interface at COM. The following section explains Interface.

Interfaces in COM

Interface is a collection of functions without implementation. Functions are only implemented at component object. Interfaces are the simply contractor in between component class and client. Interfaces are not transparent. Client can't modify or view the code of particular function so it is fully encapsulated. Before define interface, component object must be implemented and instantiated.

The following code shows an example of interface ILookup with two methods "LookupByName, LookupByNumber.

interface ILookup : public IUnknown

{

public:

virtual HRESULT __stdcall LookupByName( LPTSTR lpName,TCHAR **lplpNumber) = 0;

virtual HRESULT __stdcall LookupByNumber(LPTSTR lpNumber, TCHAR **lplpName) = 0;

}" [1,3]

Interfaces are implemented by component object and same interface can be accessed by different component objects according to expected behavior. A client only communicates with component through interface pointer. Component object contacts with different interfaces according to specific service provide by component. Each interface is identified with an identifier, a global unique id (GUID). Developer develops a component with identifier. A pointer to the interface is requested by identifier.

One of the features of the component object model is never versioned. So component interfaces are not versioned. If some change is required in interface with new methods then new interface is implemented with new identifier. The following figures show the interaction between interfaces, component objects and applications.

Figure 1: component object with three interfaces [1, 3]

Figure 2: client connects to object interface through interface pointer [1, 3]

Figure 3: Two applications are connected with each other object through object interface [1, 3]

Some benefits of using interface are pointed out at below

Backward compatibility-all the com objects are supported by QueyInterface method. Objects can create new interface which poses new functionalities to object. Thus new functions are added to object implementation. New clients are supported by new interface of object but old clients are also supported by component as well. This increases the performance and component compatibility.

Interface reuse- in COM code reuse is not allowed but interface reuse is supported.

Inter process is supported- com interrupts the call from interface to object and instantiates remote procedure call to an object which is running on different machine or remote machine.

The following section explains Globally Unique Identifiers (GUIDs)

Globally Unique Identifiers (GUIDs)

In component object model classes and interfaces are identified by unique id which is defined by the Open Software Foundation's Distributed Computing Environment. Classed are defined by CLSIDs which are Globally Unique Identifiers. Interfaces are defined by GUIDs named IID. Thus it is becoming easy to identify target component from million of components around in the world.

IUnknown

Each com object is defined by an interface IUnknown. This interface contains three methods. These are QueryIntreface, AddRef and Release. These methods implement important functionalities to component. The following code describes the structure of IUnknown interface.

"interface IUnknown {

virtual HRESULT QueryInterface(IID& iid, void** ppvObj) = 0;

virtual ULONG AddRef() = 0;

virtual ULONG Release() = 0;

}" [1,3]

AddRef and Release methods are related to using interface as reference. When other object is using the interface then AddRef method is called and reference count is increased thus it remains in memory. When an interface is released to be used by other object then Release method is called and reference count is decreased. By this process component object loads in memory and unloads from memory.

QueryIntreface is the method which tells client whether a particular interface is supported by an object or not. While client needs a pointer interface he sends a request to object by QueryIntreface method and if the requesting interface with functionalities is supported by the object then target interface pointer is returned successfully. Otherwise error is occurred. QueryInterface is a mechanism to find out interfaces among components.

Polymorphism in com and .net

Polymorphism means different forms. Polymorphism is an essential part in object oriented programming. It increases the reusability and extensibility in object oriented programming. There are two types of polymorphism. One is static polymorphism another one is dynamic polymorphism.

Static Polymorphism

Static polymorphism works in compile time. In static polymorphism method overloading is occurred. In method overloading multiple methods with same name but different in method parameters. The methods are only differentiated by their parameters. Methods can be defined inside a class or subclass. It is called compile time polymorphism because compiler identifies which object is assigned to which class at compile time. Compiler identifies different methods calling through their type, number of parameters, return type etc. the following code shows an example of java overloading.

"class A{

public void fun1(int x){

System.out.println("int");

}

public void fun1(int x,int y){

System.out.println("int and int");

}

}

public class B{

public static void main(String[] args){

A obj= A();

// Here compiler decides that fun1(int)

is to be called and "int" will be printed.

obj.fun1(2);

// Here compiler decides that fun1(int,int)

is to be called and "int and int" will be printed.

obj.fun1(2,3);

}"[2]

In the above example we see that method func1 () has been overloaded. There are two functions with same name but different signature. These two functions are called and identified with appropriate parameter like obj.fun1(2,3)with two integer parameters.

Dynamic polymorphism

Here same methods with same name and parameter is used but with different classes. There must be a super class method with same name, parameter and return type of sub class method. It is called method overriding. In method overriding methods are identified at run time. Here super class method is replaced by sub class method. Overriding is used to reuse the existing feature. The following code defines dynamic polymorphism in java.

"class A{

public void fun1(int x){

System.out.println("int in A");

}

public void fun1(int x,int y){

System.out.println("int and int");

}

}

class C extends A{

public void fun1(int x){

System.out.println("int in C");

}

}

public class D{

public static void main(String[] args){

A obj;

obj= new A(); // line 1

obj.fun1(2); // line 2 (prints "int in A")

obj=new C(); // line 3

obj.fun1(2); // line 4 (prints "int in C")

}

}" [2]

In the above example class C is extending the functions of class A. Here overriding function fun1 () is invoked by different object of class at different time. At lien 4 fun1 () method of class A is replaced by same method of derived class C.

Polymorphism in .Net

In .Net technology polymorphism works in different ways. Method overriding is occurred by defining the base class function as virtual function. Without defining the base class functions as virtual function, methods do not override from derived class. The following example shows method overriding in c#.

"using System;

class Base

{

public virtual void Method()

{

Console.WriteLine("Base method");

}

}

class Derived : Base

{

public override void Method()

{

Console.WriteLine("Derived method");

}

}

class MyClient

{

public static void Main()

{

Base b1 = new Derived();

b1.Method(); // Displays 'Base Method'

}

}" [4]

In the above example we see that to use overriding in .net a virtual method is declared in the base class then this method can be override successfully without error if it is defined as override at base class. The virtual method must not be empty. The access type of virtual method must not be private or static type. Thus runtime polymorphism is occurred in .net technology. This dynamic polymorphism occurs at run time.

Compile time polymorphism occurs when two or more methods overload same method using different parameters. In c# Console.WriteLine() method takes different parameter to output the result without showing any error. For example,

int x = 10;

Console.WriteLine (x);

string Message = "Hello";

Console.WriteLine (Message);

Here WriteLine() method is identified by compiler at run time by parameters that which method would be executed. WriteLine() method is not expecting a particular types or behavior from calling object.

There are many examples of polymorphism in .net. One of the examples is dataset. It is inherited from DataTable and DataRow. When the properties and methods of DataTable and DataRow are used in DataSet it omits the strong type casting of DataSet.

Another example is GridView and DetailsView web controls. They are inherited from the base class DataControlField and execute custom implementation by overriding ExactValuesFromCell and InitializeCell methods.

Polymorphism in term of interface and object perspective

It the perspective of interface and class relation, polymorphism occurs when base class is inherited by derived class. Thus the interface of base class reveals to the derived classes. Methods defined in interface must be implemented in class. The following code [4] shows an example of defining interface of automobile.

"

01.public abstract class Automobile

02.{

03. //Properties

04. public int DoorCount { .. }

05. public string EngineType { .. }

06. public float Height { .. }

07. public float GasTankSize { .. }

08. public float WheelBase { .. }

09. public float WheelSize { .. }

10.

11. //Abstract Properties

12. public abstract string BodyType { get; }

13.

14. //Methods

15. public float CalculateFuelEfficiency(int lastMileage);

16.}" [5]

This interface defines the general properties and methods of automobile. Different manufacturer uses this interface to implement base functionalities. In addition they also implement other functionalities according to their models. An abstract property is declared at line 12. When a property is defined as abstract or virtual it must be implemented at the derived class and returned some relevant type which is derived the interface

The following code defines two classes which is inheriting the methods and properties of automobile interface.

"

01.public abstract class Car : Automobile

02.{

03. //Properties

04. public bool HasSpoiler { .. }

05. public bool IsHatchback { .. }

06.

07. //Overridden Properties (abstract)

08. public override string BodyType { get { return "Car"; } }

09.

10. //Abstract properties

11. public abstract bool IsFast { get; }

12.}

13.

14.public abstract class Suv : Automobile

15.{

16. public bool CanOffroad { .. }

17. public int TowingCapacity { .. }

18.

19. public override string BodyType { get { return "SUV"; } }

20.}

21.public class SubaruWrx : Car

22.{

23. public override bool IsFast

24. {

25. get { return true; }

26. }

27.}" [5]

In the above example car class overrides the property of interface automobile and returns the body type string. At line 11 we see that car class also defines its own abstract property IsFast. At line 21 SubaruWrx is inheriting car class thus it is overriding the property IsFast.

Polymorphism in Component Object Model

The basic theory of polymorphism is applied to COM. Components are interacted by interface in COM thus the interfaces are implemented by component object according to their requirements. For example two component objects implement the IStack interface, one is using an array and other is using linked list. In Component object model appropriate component is selected at run time thus it supports run time polymorphism. There are some ways to execute polymorphism using component.

Interface polymorphism- Here component interfaces are inherited by other components thus component communicate with each other. A class can inherit multiple interfaces as well as an Interface is used by many component classes. Defining new interface component functionality is increased. This reduces the versioning problem in component.

Inheritance polymorphism- inheritance is quite complex and difficult to implement in component object model. If number of components are inherited the properties and methods of other components then it is difficult to keep track the implementation hierarchy. Parent or child component behavior can be changed suddenly. Thus Inheritance is quite impossible. It is only possible to include new functions to base class which is a process to extend base class functionalities without release new version.

Problems with inheritance in COM

For polymorphism inheritance is very important. Polymorphism types like override methods are occurred while one class is inherited methods of another class. Inheriting some functions from another component is against the architecture of the component object model. In Component object model encryption is an important concept. Inheritance violates encryption of component core functions.

In component hierarchical layer it is difficult to manage all the components which are in contact with each other. If the parent or child component is changed then than related component also needs to be changed. These kinds of problem occur while inheritance is used in component object model.

Conclusion

At the end if we compare polymorphism in between .Net and COM technology we see that polymorphism is fully implemented and executed in .net technology using method overloading, method overriding, interface inheritance etc. In contrary it is not fully supported or implemented by COM technology. But polymorphism exists in component object model through interface inheritance. Component is selected at runtime which tends to runtime polymorphism.