public decimal CalculatePremium() { switch (this.product.ProductType) { case Common.ProductTypes.WholeLife: product.Amount += 1000; WholeLife wl = new WholeLife() { IssueAge = product.IssueAge, IsSmoker = product.IsSmoker, DeathBenefitAmount = product.Amount, PremiumRate = product.PremiumRate }; return(wl.CalculatePremium()); case Common.ProductTypes.BusinessExpense: product.Amount += 500; BusinessExpense be = new BusinessExpense() { IssueAge = product.IssueAge, IsSmoker = product.IsSmoker, BenefitAmount = product.Amount, PremiumRate = product.PremiumRate }; return(be.CalculatePremium()); } return(0); }
static void Main(string[] args) { IProduct product = new WholeLife() { DeathBenefitAmount = 100000, IsSmoker = false, IssueAge = 34 }; #region Decorator Pattern Console.WriteLine(""); Console.WriteLine("=================Decorator Pattern===================="); Console.WriteLine(""); Console.WriteLine("1. Add new functionality dynamically to existing objects, or remove it "); Console.WriteLine("2. There is no one big feature-laden class with all the options in it."); Console.WriteLine("3. The decorations are independent of each other"); Console.WriteLine("4. The decorations can be composed together in a mix-and-match fashion"); Console.WriteLine(""); //person A, i am giving the Whole Life product without discount Console.WriteLine("Person A Benefit Amount: " + product.Benefit); Console.WriteLine("Person A should pay premium for Whole Life product: " + product.CalculatePremium().ToString("C")); Console.WriteLine(""); ProductDiscount personBdiscount = new ProductDiscount(product, Common.DiscountValue.PERCENT_25); BenefitIncrease presonBBenefitIncrease = new BenefitIncrease(personBdiscount, 1000); //person B is a probable lead and offer 25% discount // Give additional benefit of $1000 along with discount Console.WriteLine("Person B Benefit Amount: " + presonBBenefitIncrease.Benefit); Console.WriteLine("Person B should pay premium for Whole Life product with 25% discount: " + personBdiscount.CalculatePremium().ToString("C")); Console.WriteLine(""); ProductDiscount personCdiscount = new ProductDiscount(product, Common.DiscountValue.PERCENT_50); BenefitIncrease presonCBenefitIncrease = new BenefitIncrease(personCdiscount, 500); //person C has already been a customer to me and i would like to cross-sell with 50% discount as an offer price // Give additional discount of $500 along with discount Console.WriteLine("Person C Benefit Amount: " + presonCBenefitIncrease.Benefit); Console.WriteLine("Person C should pay premium for Whole Life product with 50% discount: " + personCdiscount.CalculatePremium().ToString("C")); Console.WriteLine(""); #endregion #region Proxy Pattern ProductRequest request1 = new ProductRequest() { Amount = 100000, IsSmoker = false, IssueAge = 25, ProductType = Common.ProductTypes.WholeLife }; ProductRequest request2 = new ProductRequest() { Amount = 200000, IsSmoker = false, IssueAge = 78, ProductType = Common.ProductTypes.WholeLife }; /* Virtual proxies: Hands the creation of an object over to another object (useful if the creation process * might be slow or might prove unnecessary) * Authentication proxies: Checks that the access permissions for a request are correct * Remote proxies: Encodes requests and send them across a network * Smart proxies: Adds to or change requests before sending them on Within the scope of the social networking system mentioned earlier, these map as * follows: * • Delaying the creation of a rich environment (virtual proxy) * • Logging in users (authentication proxy) * • Sending requests across the network (remote proxy) * • Performing actions on friends’ books (smart proxy * */ Console.WriteLine("=================Proxy Pattern===================="); Console.WriteLine(""); ICreateProduct productCreation = new ProductProxy(); IProduct wlproduct = productCreation.CreateProduct(request1); Console.WriteLine("Created WholeLife product with benefit {0} and Premium {1}: ", wlproduct.Benefit.ToString("C"), wlproduct.CalculatePremium().ToString("C")); IProduct wlproduct1 = productCreation.CreateProduct(request2); if (wlproduct1 == null) { Console.WriteLine("Cannot create the Whole life product"); } Console.WriteLine(""); #endregion #region Bridge Pattern /*Decouble an abstraction from its implementation, so that both of them can vary independently * Use the Bridge pattern when * You can: * • Identify that there are operations that do not always need to be implemented in the same way. * You want to: * • Completely hide implementations from clients. * • Avoid binding an implementation to an abstraction directly. * • Change an implementation without even recompiling an abstraction. * • Combine different parts of a system at runtime. */ IProduct termPlan = new TermPlan() { Benefit = 200000 }; IProduct tp2013 = new TermPlan2013() { Benefit = 200000 }; LifeProduct liProduct = new LifeProduct(); liProduct.AssociatedProduct = termPlan; Console.WriteLine("=================Bridge Pattern===================="); Console.WriteLine(""); Console.WriteLine("Term Plan"); Console.WriteLine("Term Plan Annual Premium is : " + liProduct.AnnualPremium.ToString("C")); Console.WriteLine("Term Plan Monthly Premium is : " + liProduct.MonthlyPremium.ToString("C")); Console.WriteLine(""); liProduct.AssociatedProduct = tp2013; Console.WriteLine(""); Console.WriteLine("Term Plan 2013"); Console.WriteLine("Term Plan Annual Premium is : " + liProduct.AnnualPremium.ToString("C")); Console.WriteLine("Term Plan Monthly Premium is : " + liProduct.MonthlyPremium.ToString("C")); Console.WriteLine(""); #endregion #region Composite Pattern /*Use the Composite pattern when * You have: * • An irregular structure of objects and composites of the objects * You want: * • Clients to ignore all but the essential differences between individual objects and composites of objects * • To treat all objects in a composite uniformly * But consider using as well: * • The Decorator pattern to provide operations like Add, Remove, and Find * • The Flyweight pattern to share components, provided the notion of “where I am” can be disregarded and all * operations start at the root of the composite * • The Visitor pattern to localize the operations that are currently distributed between the Composite and * Component classes */ Console.WriteLine("=================Composite Pattern===================="); Console.WriteLine(""); List <IProduct> products = new List <IProduct>(); products.Add(new WholeLife() { DeathBenefitAmount = 100000, IssueAge = 24, IsSmoker = false }); products.Add(new WholeLife() { DeathBenefitAmount = 200000, IssueAge = 29, IsSmoker = true }); List <IProduct> bProducts = new List <IProduct>(); bProducts.Add(new BusinessExpense() { BenefitAmount = 100000, IsSmoker = false, IssueAge = 34 }); bProducts.Add(new BusinessExpense() { BenefitAmount = 200000, IsSmoker = true, IssueAge = 39 }); FamilyProtection protectionPack = new FamilyProtection(products); Console.WriteLine("Family Protection Pack premium : " + protectionPack.PremiumAmount.ToString("C")); Console.WriteLine(""); BusinessProtection bProtectionPack = new BusinessProtection(bProducts); Console.WriteLine("Business Protection Pack premium: " + bProtectionPack.PremiumAmount.ToString("C")); Console.WriteLine(""); #endregion #region Adapter Pattern /* Convert the interface of a class into another interface clients expect. * Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces * Use the Adapter pattern when * You have: * • A domain-specific interface. * • A class to connect to with a mismatching interface. * You want to: * • Create a reusable class to cooperate with yet-to-be-built classes. * • Change the names of methods as called and as implemented. * • Support different sets of methods for different purposes. * Choose the Adapter you need * Class adapter * Simple and versatile, invisible to the client. * Object adapter * Extensible to subclasses of the adapter. * Two-way adapter * Enables different clients to view an object differently. * Pluggable adapter * Presence of adapter is transparent; it can be put in and taken out * Several adapters can be active. */ ISFUniversalLife ulProduct = new UniversalLifeAdapter() { IssueAge = 35, IsSmoker = false, Amount = 100000 }; Console.WriteLine("=================Adapter Pattern===================="); Console.WriteLine(""); Console.WriteLine("State Farm Universal Life Product Premiums "); Console.WriteLine("Universal Life Product Annual Premium : " + ulProduct.CalculatePremium().ToString("C")); Console.WriteLine("Universal Life Product Monthly Premium : " + ulProduct.GetMonthlyPremium().ToString("C")); Console.WriteLine(""); #endregion #region Facade Pattern /*Façade pattern is to provide different high-level views of subsystems whose details are hidden from users. * Use the Façade pattern when * A system has several identifiable subsystems and: * • The abstractions and implementations of a subsystem are tightly coupled. * • The system evolves and gets more complex, but early adopters might want to retain their simple views. * • You want to provide alternative novice, intermediate, and “power user” interfaces. * • There is a need for an entry point to each level of layered software. * But consider using instead: * • The Abstract Factory pattern for designs where subsystems create objects on behalf of the client. * Choose the Façade you need * Opaque * Subsystem operations can only be called through the Façade. * Transparent * Subsystem operations can be called directly as well as through the Façade. * Singleton * Only one instance of the Façade is meaningful. */ ProductRequest wlrequest = new ProductRequest() { Amount = 100000, IsSmoker = false, IssueAge = 34, ProductType = Common.ProductTypes.WholeLife }; ProductRequest berequest = new ProductRequest() { Amount = 40000, IsSmoker = true, IssueAge = 45, ProductType = Common.ProductTypes.BusinessExpense }; Console.WriteLine("============================Facade Pattern==========================="); Console.WriteLine(""); QuotingFacade quoteFacade = new QuotingFacade(wlrequest); Console.WriteLine("Whole Life Annual Premium : " + quoteFacade.AnnualPremium.ToString("C")); Console.WriteLine("Whole Life Monthly Premium : " + quoteFacade.MonthlyPremium.ToString("C")); Console.WriteLine(""); QuotingFacade quoteFacade2 = new QuotingFacade(berequest); Console.WriteLine("Business Expense Annual Premium : " + quoteFacade.AnnualPremium.ToString("C")); Console.WriteLine("Business ExpenseMonthly Premium : " + quoteFacade.MonthlyPremium.ToString("C")); Console.WriteLine(""); #endregion Console.ReadKey(); }
static void Main(string[] args) { #region Prototype Pattern /* Use the Prototype pattern when * You want to: * • Hide concrete classes from the client. * • Add and remove new classes (via prototypes) at runtime. * • Keep the number of classes in the system to a minimum. * • Adapt to changing structures of data at runtime. * Because: * • In C# 3.0, cloning by deep copying is absolutely straightforward. * Consider using this pattern: * • With the Composite pattern, to provide archiving. * • Instead of the Factory Method pattern, when subclasses start proliferating */ Console.WriteLine(""); Console.WriteLine("=================Prototype Pattern========================="); WholeLifePremiumIllustrations illustrations = new WholeLifePremiumIllustrations(); WholeLife wl1 = illustrations.Prototypes["SFWholeLife"].Clone(); wl1.ChildRiders[0].CoverageAmount = 1000000000; WholeLife wl2 = illustrations.Prototypes["IMLWholeLife"].DeepCopy(); wl2.ChildRiders[0].CoverageAmount = 1000000000; Console.WriteLine(""); Console.WriteLine("SFWholeLife Actual Object Coverage Amount : " + illustrations.Prototypes["SFWholeLife"].ChildRiders[0].CoverageAmount); Console.WriteLine("SFWholeLife Cloned Copy Coverage Amount : " + wl1.ChildRiders[0].CoverageAmount); Console.WriteLine(""); Console.WriteLine("IMLWholeLife Actual Object Coverage Amount : " + illustrations.Prototypes["IMLWholeLife"].ChildRiders[0].CoverageAmount); Console.WriteLine("IMLWholeLife Deep Copy Object Coverage Amount : " + wl2.ChildRiders[0].CoverageAmount); Console.WriteLine(""); #endregion #region Factory Pattern /*The Factory Method pattern is a way of creating objects, but letting subclasses * decide exactly which class to instantiate. Various subclasses might implement the * interface; the Factory Method instantiates the appropriate subclass based on information * supplied by the client or extracted from the current state. * * Use the Factory Method pattern when * • Flexibility is important. * • Objects can be extended in subclasses * • There is a specific reason why one subclass would be chosen over another—this logic forms part of the Factory * Method. * • A client delegates responsibilities to subclasses in parallel hierarchies. * Consider using instead.... * • The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex). * • The Prototype pattern to store a set of objects to clone from the abstract factory. */ Samples.StructuralPatterns.General.ProductRequest request = new Samples.StructuralPatterns.General.ProductRequest() { Amount = 10000, IsSmoker = false, IssueAge = 45, ProductType = StructuralPatterns.Common.ProductTypes.WholeLife }; ProductCreator creator = new ProductCreator(); IProduct product = creator.GetFactoryProduct(request); Console.WriteLine("=================Factory Pattern========================="); Console.WriteLine(""); Console.WriteLine("Whole Life Product Amount: {0} and Premium: {1} ", product.Benefit.ToString("C"), product.CalculatePremium().ToString("C")); Console.WriteLine(""); #endregion #region Singleton Pattern #endregion #region Abstract Factory /* Use the Abstract Factory pattern when * • A system should be independent of how its products are created, composed, and represented. * • A system can be configured with one of multiple families of products. * • The constraint requiring products from the same factory to be used together must be enforced. * • The emphasis is on revealing interfaces, not implementations. */ ILifeFactory factory = new LifeFactory(request); ILifeProduct lifeproduct = factory.GetLifeProduct(); ITermProduct tpProduct = factory.GetTermProduct(false); ITermProduct ropProduct = factory.GetTermProduct(true); Console.WriteLine(""); Console.WriteLine("==========================Abstract Factory=============================="); Console.WriteLine("Whole Life Product Premium : " + lifeproduct.CalculatePremium().ToString("C")); Console.WriteLine("Term Plan Product Premium : " + tpProduct.CalculatePremium().ToString("C")); Console.WriteLine("ROP Product Premium : " + ropProduct.CalculatePremium().ToString("C")); Console.WriteLine(""); IDIFactory difactory = new DIFactory(request); IPaycheckProtection diproduct = difactory.GetDIProduct(); IBusinessExpense beproduct = difactory.GetBEProduct(false); IBusinessExpense beproduct1 = difactory.GetBEProduct(true); Console.WriteLine(""); Console.WriteLine("Paycheck protection Product Premium : " + diproduct.CalculatePremium().ToString("C")); Console.WriteLine("Business Expense Product Premium : " + beproduct.CalculatePremium().ToString("C")); Console.WriteLine("Business Expense 2013 Product Premium : " + beproduct1.CalculatePremium().ToString("C")); Console.WriteLine(""); #endregion #region Builder Pattern Samples.StructuralPatterns.General.ProductRequest request11 = new StructuralPatterns.General.ProductRequest() { Amount = 100000, IsSmoker = false, IssueAge = 34, ProductType = Common.ProductTypes.WholeLife }; Samples.StructuralPatterns.General.ProductRequest berequest1 = new StructuralPatterns.General.ProductRequest() { Amount = 40000, IsSmoker = true, IssueAge = 45, ProductType = Common.ProductTypes.BusinessExpense }; IBuilder builder = new LifeBuilder(request11); IBuilder bebuilder = new DIBuilder(berequest1); Console.WriteLine(""); Console.WriteLine("==========================Builder Pattern============================="); Console.WriteLine("Created Whole Life Extended product and Calcuated Premium is : " + builder.InsuranceProduct.CalculatePremium().ToString("C")); Console.WriteLine("Created BE Extended product and Calcuated Premium is : " + bebuilder.InsuranceProduct.CalculatePremium().ToString("C")); Console.WriteLine(""); #endregion Console.ReadKey(); }