## CSDN博客

### A Taste of AOP from Solving Problems with OOP and Design Patterns (Part I)

?

public class Calculator
{
? public int Add(int x, int y) { return x + y; }
}

?

?

public void Test()
{
? Calculator calculator = new Calculator();

? Assert.IsNotNull(calculator);
}

?

?

public class Calculator
{
? public int Add(int x, int y)
? {
??? int result = x + y;
??? Console.WriteLine(" = {0}", result);
??? return result;
? }
}

?

?

public class Calculator
{
? public int Add(int x, int y)
? {
??? DateTime startTime = PreciseTimer.Now;
??? int result = x + y;
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? Console.Write(" [{0}] ", elapsedTime);

??? Console.WriteLine(" = {0}", result);
??? return result;
? }
}

?

?

public class Calculator
{
? public int Add(int x, int y)
? {
??? DateTime startTime = PreciseTimer.Now;
??? int result = x + y;
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? Console.Write(" [{0}] ", elapsedTime);
??? Console.WriteLine(" = {0}", result);
??? return result;
? }

? public int Subtract(int x, int y)
? {
??? Console.Write("Subtract({0},{1})", x, y);
??? DateTime startTime = PreciseTimer.Now;
??? int result = x - y;
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? Console.Write(" [{0}] ", elapsedTime);

??? Console.WriteLine(" = {0}", result);
??? return result;
? }

}

?

?

public void Test()
{
? ICalculator calculator = new Calculator();

? Assert.IsNotNull(calculator);
? Assert.AreEqual(5, calculator.Subtract(8,3));
}

?

?

?

public interface ICalculator
{
? int Add(int x, int y);
? int Subtract(int x, int y);
}

public class Calculator: ICalculator
{
? public int Add(int x, int y)
? {
??? DateTime startTime = PreciseTimer.Now;
??? int result = x + y;
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? Console.Write(" [{0}] ", elapsedTime);
??? Console.WriteLine(" = {0}", result);
??? return result;
? }

? public int Subtract(int x, int y)
? {
??? Console.Write("Subtract({0},{1})", x, y);
??? DateTime startTime = PreciseTimer.Now;
??? int result = x - y;
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? Console.Write(" [{0}] ", elapsedTime);
??? Console.WriteLine(" = {0}", result);
??? return result;
? }?
}

?

?

public void Test()
{
? ICalculator calculator = new Calculator();

? Assert.IsNotNull(calculator);
? Assert.AreEqual(5, calculator.Subtract(8,3));
}

?

?

public class Calculator: ICalculator
{
? public int Add(int x, int y)
? {
??? DateTime startTime = PreciseTimer.Now;
??? int result = x + y;
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? Console.Write(" [{0}] ", elapsedTime);

??? Console.WriteLine(" = {0}", result);
??? return result;
? }

? public int Subtract(int x, int y)
? {
??? Console.Write("Subtract({0},{1})", x, y);
??? DateTime startTime = PreciseTimer.Now;
??? int result = x - y;
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? Console.Write(" [{0}] ", elapsedTime);

??? Console.WriteLine(" = {0}", result);
??? return result;
? }?
}

?

public class CalculatorTimer: ICalculator
{
? public int Add(int x, int y)
? {
??? DateTime startTime = PreciseTimer.Now;
??? int result =
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? return result;
? }

? public int Subtract(int x, int y)
? {
??? DateTime startTime = PreciseTimer.Now;
??? int result =
Decoratee.Subtract(x, y);
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? return result;
? }
}

?

?

public interface ICalculatorDecorator
{
? void InitDecorator(ICalculator decoratee);
? ICalculator Decoratee { get; }
}

?

?

public abstract class CalculatorDecorator: ICalculatorDecorator
{
? private ICalculator decoratee;

? void ICalculatorDecorator.InitDecorator(ICalculator decoratee)
? {
??? this.decoratee = decoratee;
? }

? // FIXED: to use implicit interface implementation instead explicit way
? // so that derived classes could access this property. Thanks
greatqn!
? ICalculator ICalculatorDecorator.Decoratee
? public ICalculator Decoratee
? {
??? get { return this.decoratee; }
? }
}

?

?

public class CalculatorTimer: CalculatorDecorator, ICalculator
{
? public int Add(int x, int y)
? {
??? DateTime startTime = PreciseTimer.Now;
??? int result = Decoratee.Add(x, y);
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? return result;
? }

? public int Subtract(int x, int y)
? {
??? DateTime startTime = PreciseTimer.Now;
??? int result = Decoratee.Subtract(x, y);
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? return result;
? }
}

?

?

public class CalculatorFactory
{
? private CalculatorFactory() {}

?
private static Type[] GetObjectGraph()
? {
??? ArrayList objectGraphTypes = new ArrayList();

??? // Add decoratee as 1st type to be created

??? // and then add all decorators

??? return (Type[])objectGraphTypes.ToArray(typeof(Type));

? }

? public static ICalculator CreateInstance()
? {
??? Type[] objectGraphTypes = GetObjectGraph();

??? ICalculator result = null;

??? foreach (Type calcType in objectGraphTypes)
??? {
????? ICalculator calcImpl = (ICalculator)Activator.CreateInstance(calcType);

????? if (calcImpl is ICalculatorDecorator)
????? {
??????? ((ICalculatorDecorator)calcImpl).InitDecorator(result);
????? }

????? result = calcImpl;
??? }

??? return result;

? }
}

?

?

public void Test()
{
? ICalculator calculator = CalculatorFactory.CreateInstance();

? Assert.IsNotNull(calculator);
? Assert.AreEqual(5, calculator.Subtract(8,3));
}

?

?

?

public class CalculatorLogger: CalculatorDecorator, ICalculator
{
? public int Add(int x, int y)
? {
??? int result = Decoratee.Add(x, y);
??? Console.WriteLine(" = {0}", result);
??? return result;
? }

? public int Subtract(int x, int y)
? {
??? Console.Write("Subtract({0}, {1})", x, y);
??? int result = Decoratee.Subtract(x, y);
??? Console.WriteLine(" = {0}", result);
??? return result;
? }
}

?

?

public class CalculatorFactory
{
?
?
private static Type[] GetObjectGraph()
? {
??? ArrayList objectGraphTypes = new ArrayList();

??? // Add decoratee as 1st type to be created

??? // and then add all decorators
???

??? return (Type[])objectGraphTypes.ToArray(typeof(Type));

? }

? public static ICalculator CreateInstance() …
}

?

?

public abstract class CalculatorDecorator: ICalculatorDecorator, ICalculator
{
? private ICalculator decoratee;

? void ICalculatorDecorator.InitDecorator(ICalculator decoratee)
? {
??? this.decoratee = decoratee;
? }

? public ICalculator Decoratee
? {
??? get { return this.decoratee; }
? }

? int ICalculator.Add(int x, int y)
? {
??? int result = decoratee.Add(x, y);
??? return result;
? }

? int ICalculator.Subtract(int x, int y)
? {
??? DoPreSubtract(ref x, ref y);
??? int result = decoratee.Subtract(x, y);
??? DoPostSubtract(x, y, ref result);
??? return result;
? }

? protected virtual void DoPreAdd(ref int x, ref int y) {}
? protected virtual void DoPostAdd(int x, int y, ref result) {}

? protected virtual void DoPreSubtract(ref int x, ref int y) {}
? protected virtual void DePostSubtract(int x, int y, ref result) {}
}

?

?

?

public interface ICalculatorDecorator
{
? void InitDecorator(ICalculator decoratee, IDictionary context);
? ICalculator Decoratee { get; }
? IDictionary Context { get; }
}

?

public abstract class CalcuatorDecorator: ICalculatorDecorator
{
? private ICalculator decoratee;
? private IDictionary context;

? public void InitDecorator(ICalculator decoratee, IDictionary context)
? {
??? this.decoratee = decoratee;
??? this.context = context;
? }

? public ICalculator Decoratee
? {
??? get { return this.decoratee; }
? }

? public IDictionary Context
? {
??? get { return this.context; }
? }

}

?

?

public class CalculatorFactory
{
?
? public static ICalculator CreateInstance()
? {
??? Type[] objectGraphTypes = GetObjectGraph();

??? ICalculator result = null;
??? IDictionary context = new Hashtable();

??? foreach (Type calcType in objectGraphTypes)
??? {
????? ICalculator calcImpl = (ICalculator)Activator.CreateInstance(calcType);

????? if (calcImpl is ICalculatorDecorator)
????? {
??????? ((ICalculatorDecorator)calcImpl).InitDecorator(result, context);
????? }

????? result = calcImpl;
??? }

??? return result;

? }
}

?

?

public class CalculatorTimer: CalculatorDecorator, ICalculator
{
? public int Add(int x, int y)
? {
??? DateTime startTime = PreciseTimer.Now;
??? int result = Decoratee.Add(x, y);
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? return result;
? }

? public int Subtract(int x, int y)
? {
??? DateTime startTime = PreciseTimer.Now;
??? int result = Decoratee.Subtract(x, y);
??? TimeSpan elapsedTime = PreciseTimer.ElapsedSince(start);
??? Context["Subtract.ElapsedTime"] = elapsedTime;
??? return result;
? }
}

?

public class CalculatorLogger: CalculatorDecorator, ICalculator
{
? public int Add(int x, int y)
? {
??? int result = Decoratee.Add(x, y);
??? Console.WriteLine(" = {0} {1}", result, Context["Add.ElapsedTime"]);
??? return result;
? }

? public int Subtract(int x, int y)
? {
??? Console.Write("Subtract({0}, {1})", x, y);
??? int result = Decoratee.Subtract(x, y);
??? Console.WriteLine(" = {0} {1}", result, Context["Subtract.ElapsedTime"]);
??? return result;
? }
}

?

?

?

0 0