Definition : The adapter pattern is used to match one interface with the other.
Problem : In the below example I have a console application based on FarmAnimals. I would like to build a collection of farm animals with several capabilities. Some of these capabilities I wish to implement on my own , other I hope to be able to make use of using third party libraries with existing functionality. I have found one such library called the ZooLib, but the ZooLib animal implementation a very different interface.
Analysis : My console application FarmAnimals consists of a concrete class called goat that implements the IFarmAnimal interface. The IFarmAnimal interface specifies a method getDietaryType.
The ZooLib assembly consists of a concrete function getFamily, that I would like to use in my console application FarmAnimals. But since my goat class implements IFarmAnimal and the getFamily which is part of the MountainGoat concerete class which implements the interface IZooAnimals which are incompatible interfaces.
Solution : I introduce an abstract base class called ZooAnimalAdapter. ZooAnimalAdapter impliments both interfaces, it delegates all my IFarmAnimal operations to the concrete subclasses and builds wrappers around the IFarmAnimal.
Code
The below is my FarmAnimal application
class Program
{
static void Main(string[] args)
{
Goat g = new Goat();
g.getFamily();
g.getDietaryType();
Console.ReadKey();
}
}
public abstract class ZooAnimalAdapter:IFarmAnimal,IZooAnimal
{
public abstract void getDietaryType();
public void getFamily()
{
IZooAnimal za = new MountainGoat();
za.getFamily();
}
}
public interface IFarmAnimal
{
void getDietaryType();
}
Below are the contents of the ZooLib library
public interface IZooAnimal
{
void getFamily();
}
public class MountainGoat : IZooAnimal
{
public void getFamily()
{
Console.WriteLine(" Family : Bovidae");
}
}
Net coder
A journey through the software development world can be interesting, exciting and quite daunting and Id like to share my own such experiences here.
Friday, February 10, 2012
The Adapter Pattern
Sunday, January 29, 2012
Object Oriented Programming
Object Oriented Concepts
Inheritance
Creating a class based on an existing base class or super type. As the title suggests, this concept defines reuse or the recreation of objects using an existing class.
In the programming world we realize this by defining a base class that contains a set of shared or common methods and then create subclasses that inherit from this class. These methods and properties are then automatically available in the subclass.
What can we achieve by Inheritance?
The implementation of a common method/methods can be reused or shared among the subclasses from the base class.
Abstraction
The basic principle behind abstraction is to hide the complex details and keep things simple.
In the programming world we realize this by defining a base abstract class that contains a method/methods with or without concrete implementations.The subclasses that extend this abstract class may or may not in turn implement these methods.
What can we achieve by Abstraction?
As you will read later below, one of the principles of object oriented principles is to code to an interface rather than an implementation. Abstraction helps in coding to a interface or a super type giving you the flexibility to change the implementation at a later stage.
Polymorphism
Polymorphisms is a generic term that means 'many shapes'. In OOP's polymorphism is the ability to be able define multiple implementations for the same method call.
In the programming world we realize this by defining a common interface across various concrete class with different implementations.
Ad-hoc polymorphism: polymorphism is not the same as method overloading or method overriding
What can we achieve by Polymorphism?
We can achieve interchangeability. That is we can easily change the behavior of a given method without the need to create a new type.
Encapsulation
Simply means hiding information that does not need to be seen by others. An analogy can be made here with the notion of a capsule, which not only encloses its contents, but also protects it from the exterior environment.
In the programming world we realize this by the use of access modifiers. For example we can make the methods of a class private thus making its implementation in accessible to other objects.
What can we achieve by Encapsulation?
We can reduce the dependency of objects on each other. For example we can prevent users from setting the internal data of the component into an invalid or inconsistent state.
Object oriented principles
1) Code to an interface rather than to an implementation
2) Favor composition over inheritance
3) Encapsulate what changes or varies
Reference
http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
http://www.my-project-management-expert.com/definition-object-oriented-programming.html
Inheritance
Creating a class based on an existing base class or super type. As the title suggests, this concept defines reuse or the recreation of objects using an existing class.
In the programming world we realize this by defining a base class that contains a set of shared or common methods and then create subclasses that inherit from this class. These methods and properties are then automatically available in the subclass.
What can we achieve by Inheritance?
The implementation of a common method/methods can be reused or shared among the subclasses from the base class.
Abstraction
The basic principle behind abstraction is to hide the complex details and keep things simple.
In the programming world we realize this by defining a base abstract class that contains a method/methods with or without concrete implementations.The subclasses that extend this abstract class may or may not in turn implement these methods.
What can we achieve by Abstraction?
As you will read later below, one of the principles of object oriented principles is to code to an interface rather than an implementation. Abstraction helps in coding to a interface or a super type giving you the flexibility to change the implementation at a later stage.
Polymorphism
Polymorphisms is a generic term that means 'many shapes'. In OOP's polymorphism is the ability to be able define multiple implementations for the same method call.
In the programming world we realize this by defining a common interface across various concrete class with different implementations.
Ad-hoc polymorphism: polymorphism is not the same as method overloading or method overriding
What can we achieve by Polymorphism?
We can achieve interchangeability. That is we can easily change the behavior of a given method without the need to create a new type.
Encapsulation
Simply means hiding information that does not need to be seen by others. An analogy can be made here with the notion of a capsule, which not only encloses its contents, but also protects it from the exterior environment.
In the programming world we realize this by the use of access modifiers. For example we can make the methods of a class private thus making its implementation in accessible to other objects.
What can we achieve by Encapsulation?
We can reduce the dependency of objects on each other. For example we can prevent users from setting the internal data of the component into an invalid or inconsistent state.
Object oriented principles
1) Code to an interface rather than to an implementation
2) Favor composition over inheritance
3) Encapsulate what changes or varies
Reference
http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
http://www.my-project-management-expert.com/definition-object-oriented-programming.html
CodeProject
Saturday, January 28, 2012
The startegy pattern
Definition: The Strategy pattern defines a family of algorithms whose individual behaviors are encapsulated allowing them to be interchangeable. Strategy lets the algorithm vary independently from clients that use it.
The above example illustrates the strategy pattern. The client in this case Jetplane is a subclass of type Plane. The Jetplane uses the flight algorithms Jetflight. Each flight algorithm is encapsulated in its own concrete class. The jet plane could very easily use a different flight algorithm making them interchangeable.
The above example illustrates the strategy pattern. The client in this case Jetplane is a subclass of type Plane. The Jetplane uses the flight algorithms Jetflight. Each flight algorithm is encapsulated in its own concrete class. The jet plane could very easily use a different flight algorithm making them interchangeable.
Labels:
Design patterns
| Reactions: |
Subscribe to:
Posts (Atom)
