Showing posts with label design pattern. Show all posts
Showing posts with label design pattern. Show all posts

Thursday, August 13, 2009

Inversion of control and dependency injection

The caller eventually get the results, but how and when is out of its control. The callee decide when and how. This is a technique separating execution of a certain task from its implementation.

1. decoupling execution and its implementation
2. Every system focus on what it is designed for.
3. Every system does not what other system will do or should do.
4. Changing one system will not affect others.

Techniques:

1. Factory pattern.
2. setter injection
3. Constructor injection.
4. Services Locater
5. Interface injection

Dependency Injection is decoupling high level modules from low level services.

Monday, August 10, 2009

template VS stragety

he Template pattern is similar to the Strategy pattern. These two patterns differ in scope and in methodology.

Strategy is used to allow callers to vary an entire algorithm, like how to calculate different types of tax, while Template Method is used to vary steps in an algorithm. Because of this, Strategy is more coarsely grained. The Template allows finer-grained controls in the sequent of operations, and yet allows the implementations of these details to vary.

The other main difference is that Strategy uses delegation while Template Method uses inheritance. In Strategy, the algorithm is delegated to the another xxxStrategy class that the subject will have a reference to, but with Template you subclass the base and override methods to make changes.

Strategy pattern example:
Class MainSubject
{

ITaxStrategy taxCalculator = GetStrategy(taxType);
//strategy is member class.
taxCalculator.Calculate();

private GetStrategy(string taxType)
{

if (taxType == "incometax")
return new IncomeTaxStrategy();
else if (taxType == "propertytax")
return new PropertyTaxStrategy();
}
}

Class IncomeTaxStrategy : ITaxStrategy
{


public Calculate()
{
//calculate based on income tax rates.
}

}

Class PropertyTaxStrategy : ITaxStrategy
{

public Calculate()
{
//calculate based on property tax
policies.
}

}

Template pattern example:


abstract Class TaxCalculator
{
public CalculateTax()
{
CalculateIncome();
tax =+ CalculateTax();
tax =+ CalculateRelief();

}
abstract CalculateTax();
abstract CalculateRelief();

}

Class IncomeTaxCalculator : TaxCalculator
{

override CalculateTax() { //calculate income tax. }
override CalculateRelief() { //calculate personal relief }

}

Class PropertyTaxCalculator : TaxCalculator
{

override CalculateTax() { //calculate property tax. }
override CalculateRelief() { //do nothing; no relief. }

}

Monday, July 27, 2009

What is the difference between class adapter and object adapter design pattern

Object adapter contains adaptee , and class adapter inherits from adaptee.
If you have multiple adaptees, then you have to use object adapter.

// Object adapter version

public class ReportGenerator implements OldReporter {

private NewReportWriter newReporter = new NewReportWriter();

public void writeHeader(String headerData) {

newReporter.generateHeaderLines(headerData);

}

}

// Class adapter version

public class ReportGenerator extends NewReportWriter

implements OldReporter {

public void writeHeader(String headerData) {

generateHeaderLines(headerData);

}

}

Friday, July 24, 2009

design pattern Singleton

  1. public class SimpleSingleton {
  2. private SimpleSingleton singleInstance = null;
  3. //Marking default constructor private
  4. //to avoid direct instantiation.
  5. private SimpleSingleton() {
  6. }
  7. //Get instance for class SimpleSingleton
  8. public static SimpleSingleton getInstance() {
  9. if(null == singleInstance) {
  10. singleInstance = new SimpleSingleton();
  11. }
  12. return singleInstance;
  13. }
  14. }
in multi-thread environment, please use:

public static synchronized SimpleSingleton getInstance() { }