diff options
| author | mo.khan <mo.khan@a0a4a051-f042-0410-9e78-9fae330bdb64> | 2008-01-05 07:16:52 +0000 |
|---|---|---|
| committer | mo.khan <mo.khan@a0a4a051-f042-0410-9e78-9fae330bdb64> | 2008-01-05 07:16:52 +0000 |
| commit | 8c137f229c36a777ead5cacb3350cb8692646292 (patch) | |
| tree | 92876c5da0ffd17767e38f94a44415ac27a3c73e /DesignPatterns/src/app/DesignPatterns.Factory | |
| parent | cbdf42a6427eef7849d1ab7731f9185b410431d3 (diff) | |
git-svn-id: http://mokhan.googlecode.com/svn/trunk@9 a0a4a051-f042-0410-9e78-9fae330bdb64
Diffstat (limited to 'DesignPatterns/src/app/DesignPatterns.Factory')
21 files changed, 372 insertions, 0 deletions
diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Bank.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Bank.cs new file mode 100644 index 0000000..ac51e00 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Bank.cs @@ -0,0 +1,19 @@ +namespace DesignPatterns.Factory {
+ public abstract class Bank : IBank {
+ private readonly string _name;
+
+ public Bank( string name ) {
+ _name = name;
+ }
+
+ public string Name {
+ get { return _name; }
+ }
+
+ public override string ToString( ) {
+ return _name;
+ }
+
+ public abstract IBankAccountFactory GetAccountFactory( );
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/BankAccount.cs b/DesignPatterns/src/app/DesignPatterns.Factory/BankAccount.cs new file mode 100644 index 0000000..6bde449 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/BankAccount.cs @@ -0,0 +1,27 @@ +using System;
+using DesignPatterns.Test;
+
+namespace DesignPatterns.Factory {
+ public class BankAccount : IBankAccount {
+ public BankAccount( ) : this( new Money( 0 ) ) {}
+
+ public BankAccount( IMoney balance ) : this( balance, Guid.NewGuid( ).ToString( ) ) {}
+
+ public BankAccount( IMoney balance, string accountNumber ) {
+ _balance = balance;
+ _accountNumber = accountNumber;
+ }
+
+ public IMoney Balance {
+ get { return _balance; }
+ set { _balance = value; }
+ }
+
+ public string AccountNumber {
+ get { return _accountNumber; }
+ }
+
+ private IMoney _balance;
+ private readonly string _accountNumber;
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBank.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBank.cs new file mode 100644 index 0000000..b2a80ce --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBank.cs @@ -0,0 +1,9 @@ +namespace DesignPatterns.Factory {
+ public class CibcBank : Bank, IBank {
+ public CibcBank( ) : base( "CIBC" ) {}
+
+ public override IBankAccountFactory GetAccountFactory( ) {
+ return new CibcBankAccountFactory( );
+ }
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBankAccountFactory.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBankAccountFactory.cs new file mode 100644 index 0000000..23ac864 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcBankAccountFactory.cs @@ -0,0 +1,13 @@ +using DesignPatterns.Test;
+
+namespace DesignPatterns.Factory {
+ internal class CibcBankAccountFactory : IBankAccountFactory {
+ public IBankAccount CreateChequingAccount( ) {
+ return new CibcChequingAccount( );
+ }
+
+ public IBankAccount CreateSavingsAccount( ) {
+ return new CibcSavingsAccount( );
+ }
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcChequingAccount.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcChequingAccount.cs new file mode 100644 index 0000000..29e4b92 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcChequingAccount.cs @@ -0,0 +1,5 @@ +using DesignPatterns.Test;
+
+namespace DesignPatterns.Factory {
+ internal class CibcChequingAccount : BankAccount, IBankAccount {}
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcSavingsAccount.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcSavingsAccount.cs new file mode 100644 index 0000000..1582a1d --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Cibc/CibcSavingsAccount.cs @@ -0,0 +1,5 @@ +using DesignPatterns.Test;
+
+namespace DesignPatterns.Factory {
+ internal class CibcSavingsAccount : BankAccount, IBankAccount {}
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Currency.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Currency.cs new file mode 100644 index 0000000..2c0834e --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Currency.cs @@ -0,0 +1,38 @@ +namespace DesignPatterns.Factory {
+ public class Currency : ICurrency {
+ #region Constructors
+
+ private Currency( string name ) {
+ _name = name;
+ }
+
+ #endregion
+
+ #region Public Properties
+
+ public string Name {
+ get { return _name; }
+ }
+
+ public static readonly ICurrency Canadian = new Currency( "CAD" );
+ public static readonly ICurrency American = new Currency( "USD" );
+ public static readonly ICurrency GreatBritain = new Currency( "GBP" );
+ public static readonly ICurrency EuropeanUnion = new Currency( "EUR" );
+
+ #endregion
+
+ #region Public Methods
+
+ public override string ToString( ) {
+ return _name;
+ }
+
+ #endregion
+
+ #region Private Fields
+
+ private readonly string _name;
+
+ #endregion
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/DesignPatterns.Factory.csproj b/DesignPatterns/src/app/DesignPatterns.Factory/DesignPatterns.Factory.csproj new file mode 100644 index 0000000..4c50734 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/DesignPatterns.Factory.csproj @@ -0,0 +1,65 @@ +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.50727</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{0AAEDFE4-421D-4B3B-8629-5A20EC13F379}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>DesignPatterns.Factory</RootNamespace>
+ <AssemblyName>DesignPatterns.Factory</AssemblyName>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Bank.cs" />
+ <Compile Include="BankAccount.cs" />
+ <Compile Include="Exceptions\CannotAddMoniesException.cs" />
+ <Compile Include="Cibc\CibcBank.cs" />
+ <Compile Include="Cibc\CibcBankAccountFactory.cs" />
+ <Compile Include="Cibc\CibcChequingAccount.cs" />
+ <Compile Include="Cibc\CibcSavingsAccount.cs" />
+ <Compile Include="Currency.cs" />
+ <Compile Include="Interfaces\IBank.cs" />
+ <Compile Include="Interfaces\IBankAccount.cs" />
+ <Compile Include="Interfaces\IBankAccountFactory.cs" />
+ <Compile Include="Interfaces\ICurrency.cs" />
+ <Compile Include="Interfaces\IMoney.cs" />
+ <Compile Include="Money.cs" />
+ <Compile Include="Exceptions\NegativeMoneyException.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="RoyalBank\RoyalBank.cs" />
+ <Compile Include="RoyalBank\RoyalBankAccountFactory.cs" />
+ <Compile Include="RoyalBank\RoyalBankChequingAccount.cs" />
+ <Compile Include="RoyalBank\RoyalBankSavingsAccount.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/CannotAddMoniesException.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/CannotAddMoniesException.cs new file mode 100644 index 0000000..421883c --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/CannotAddMoniesException.cs @@ -0,0 +1,13 @@ +using System;
+using System.Runtime.Serialization;
+
+namespace DesignPatterns.Factory {
+ [Serializable]
+ public class CannotAddMoniesException : ArgumentException
+ {
+ public CannotAddMoniesException() { }
+ public CannotAddMoniesException(string message) : base(message) { }
+ public CannotAddMoniesException(string message, Exception innerException) : base(message, innerException) { }
+ protected CannotAddMoniesException(SerializationInfo info, StreamingContext context) : base(info, context) { }
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/NegativeMoneyException.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/NegativeMoneyException.cs new file mode 100644 index 0000000..d7d313d --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Exceptions/NegativeMoneyException.cs @@ -0,0 +1,15 @@ +using System;
+using System.Runtime.Serialization;
+
+namespace DesignPatterns.Factory {
+ [Serializable]
+ public class NegativeMoneyException : ArgumentException {
+ public NegativeMoneyException( ) : this( "Cannot create a negative money." ) {}
+
+ public NegativeMoneyException( string message ) : base( message ) {}
+
+ public NegativeMoneyException( string message, Exception innerException ) : base( message, innerException ) {}
+
+ protected NegativeMoneyException( SerializationInfo info, StreamingContext context ) : base( info, context ) {}
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBank.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBank.cs new file mode 100644 index 0000000..52f1e67 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBank.cs @@ -0,0 +1,6 @@ +namespace DesignPatterns.Factory {
+ public interface IBank {
+ string Name { get; }
+ IBankAccountFactory GetAccountFactory( );
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccount.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccount.cs new file mode 100644 index 0000000..f759e8d --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccount.cs @@ -0,0 +1,8 @@ +using DesignPatterns.Factory;
+
+namespace DesignPatterns.Test {
+ public interface IBankAccount {
+ IMoney Balance { get; set; }
+ string AccountNumber{ get;}
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccountFactory.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccountFactory.cs new file mode 100644 index 0000000..5099fd8 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IBankAccountFactory.cs @@ -0,0 +1,8 @@ +using DesignPatterns.Test;
+
+namespace DesignPatterns.Factory {
+ public interface IBankAccountFactory {
+ IBankAccount CreateChequingAccount( );
+ IBankAccount CreateSavingsAccount( );
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/ICurrency.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/ICurrency.cs new file mode 100644 index 0000000..8f1afd4 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/ICurrency.cs @@ -0,0 +1,5 @@ +namespace DesignPatterns.Factory {
+ public interface ICurrency {
+ string Name { get; }
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IMoney.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IMoney.cs new file mode 100644 index 0000000..c5392ae --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Interfaces/IMoney.cs @@ -0,0 +1,13 @@ +using DesignPatterns.Factory;
+
+namespace DesignPatterns.Factory {
+ public interface IMoney {
+ double Amount { get; }
+
+ ICurrency TypeOfCurrency { get; }
+
+ IMoney Add( IMoney other );
+
+ IMoney Subtract( IMoney money );
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Money.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Money.cs new file mode 100644 index 0000000..956c9a6 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Money.cs @@ -0,0 +1,56 @@ +using System;
+
+namespace DesignPatterns.Factory {
+ public class Money : IMoney {
+ public Money( double amount ) : this( amount, Currency.Canadian ) {}
+
+ public Money( double amount, ICurrency currency ) {
+ if ( amount < 0 ) {
+ throw new NegativeMoneyException( );
+ }
+ _amount = amount;
+ _currency = currency;
+ }
+
+ public double Amount {
+ get { return _amount; }
+ }
+
+ public ICurrency TypeOfCurrency {
+ get { return _currency; }
+ }
+
+ public IMoney Add( IMoney other ) {
+ if ( other != null ) {
+ if ( other.TypeOfCurrency.Equals( TypeOfCurrency ) ) {
+ return new Money( other.Amount + Amount );
+ }
+ throw new CannotAddMoniesException( "Cannot add monies of different currency" );
+ }
+ return this;
+ }
+
+ public IMoney Subtract( IMoney money ) {
+ return ( money != null ) ? new Money( Amount - money.Amount ) : null;
+ }
+
+ public override bool Equals( object obj ) {
+ IMoney other = obj as Money;
+ if ( other != null ) {
+ return other.Amount == Amount;
+ }
+ return false;
+ }
+
+ public override int GetHashCode( ) {
+ return base.GetHashCode( ) + new Random( ( int )DateTime.Now.Ticks ).Next( );
+ }
+
+ public override string ToString( ) {
+ return _amount.ToString( "F" );
+ }
+
+ private readonly double _amount;
+ private readonly ICurrency _currency;
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/Properties/AssemblyInfo.cs b/DesignPatterns/src/app/DesignPatterns.Factory/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7e3253a --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("DesignPatterns.Factory")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("DesignPatterns.Factory")]
+[assembly: AssemblyCopyright("Copyright © 2007")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("b2eb7e33-2bbe-4cc0-a003-5ee1c528e811")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Revision and Build Numbers
+// by using the '*' as shown below:
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBank.cs b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBank.cs new file mode 100644 index 0000000..1e234ab --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBank.cs @@ -0,0 +1,9 @@ +namespace DesignPatterns.Factory {
+ public class RoyalBank : Bank, IBank {
+ public RoyalBank( ) : base( "Royal Bank" ) {}
+
+ public override IBankAccountFactory GetAccountFactory( ) {
+ return new RoyalBankAccountFactory( );
+ }
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankAccountFactory.cs b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankAccountFactory.cs new file mode 100644 index 0000000..5a3c2a4 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankAccountFactory.cs @@ -0,0 +1,13 @@ +using DesignPatterns.Test;
+
+namespace DesignPatterns.Factory {
+ internal class RoyalBankAccountFactory : IBankAccountFactory {
+ public IBankAccount CreateChequingAccount( ) {
+ return new RoyalBankChequingAccount( );
+ }
+
+ public IBankAccount CreateSavingsAccount( ) {
+ return new RoyalBankSavingsAccount( );
+ }
+ }
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankChequingAccount.cs b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankChequingAccount.cs new file mode 100644 index 0000000..30c5d2a --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankChequingAccount.cs @@ -0,0 +1,5 @@ +using DesignPatterns.Test;
+
+namespace DesignPatterns.Factory {
+ internal class RoyalBankChequingAccount : BankAccount, IBankAccount {}
+}
\ No newline at end of file diff --git a/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankSavingsAccount.cs b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankSavingsAccount.cs new file mode 100644 index 0000000..06d3534 --- /dev/null +++ b/DesignPatterns/src/app/DesignPatterns.Factory/RoyalBank/RoyalBankSavingsAccount.cs @@ -0,0 +1,5 @@ +using DesignPatterns.Test;
+
+namespace DesignPatterns.Factory {
+ internal class RoyalBankSavingsAccount : BankAccount, IBankAccount {}
+}
\ No newline at end of file |
