From 689509c5de1322075ea4dd355d33002f16a55271 Mon Sep 17 00:00:00 2001 From: Petrutiu Mihai Date: Wed, 13 Jul 2016 16:47:20 +0300 Subject: [PATCH] Update description for visitor --- README.md | 29 +++++++++++++++++++ .../WithVisitor/CorruptionSuspect.cs | 28 ++++++++++++++++++ .../WithVisitor/SuspectVisitors.cs | 13 +++++++++ 3 files changed, 70 insertions(+) diff --git a/README.md b/README.md index 04ffd4b..d85a289 100644 --- a/README.md +++ b/README.md @@ -343,19 +343,48 @@ The strategy pattern defines a family of algorithms, encapsulates each algorithm ------------------ ### a. Pattern description +In software engineering, the template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in an operation, +defering some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm's structure. +Template pattern works using 'the Hollywood principle' from the base class point of view: 'Don't call us, we'll call you' ### b. When to use Template pattern +* Let subclasses implement (through method overriding) behavior that can vary. +* Avoid duplication in the code: the general workflow structure is implemented once in the abstract class's algorithm, and necessary variations are implemented in each of the subclasses. +* Control at what point(s) subclassing is allowed. As opposed to a simple polymorphic override, where the base method would be entirely rewritten allowing radical change to the workflow, only the specific details of the workflow are allowed to change. +* You want to be able to add customization hooks to your class ### c. Actors +* Workflow/Abstract/Framework class -> Where we define the workflow that is calling the methods that may be ovveriden by the concrete class +* Concrete class -> Class that implements the abstract methods of the Framework class ### d. Pitfalls +* Uses inheritance for defining the implementation instead of composition + ### e. Flavors +* Don't know any + ### f. Examples +* Workers example: Starting class: WokersExample.WorkersExample + * have the morning routine of a manager/construction worker/student until they start working +* Game example: Starting class: GameExample.GameExample + * Implement basketball/Football where instead of playing we just print some strings + 10. Visitor pattern ------------------ ### a. Pattern description +* Visitor pattern allows for one or more operation to be applied to a set of objects at runtime, decoupling the operations from the object structure. +* Visitor is similar to Iterator, the difference is that visitor pattern allows to process more complex structure with different types + ### b. When to use Visitor pattern +* When you have a complex structure that doesn't change that much, but you need to add more types of processing ### c. Actors +* Visitor - The interface that defines methods to visit each of the visitable types +* Concrete Visitor - Implemementation of the Visitor pattern +* Visitable - interface that accepts the visitor, usually one method->Accept(Visitor v) +* Concrete Visitable - Objects that are part of the complex structure that needs to be visited +* Object Structure - Object that contains all the visitable objects, and defines a way to visit them ### d. Pitfalls +* Every time you add a new visitable object, you need to modify all the visitors. ### e. Flavors + ### f. Examples diff --git a/src/VisitorPattern/CalculateMoney/WithVisitor/CorruptionSuspect.cs b/src/VisitorPattern/CalculateMoney/WithVisitor/CorruptionSuspect.cs index 8189e61..c01d534 100644 --- a/src/VisitorPattern/CalculateMoney/WithVisitor/CorruptionSuspect.cs +++ b/src/VisitorPattern/CalculateMoney/WithVisitor/CorruptionSuspect.cs @@ -3,6 +3,9 @@ using System.Collections.Generic; namespace VisitorPattern.CalculateMoney.WithVisitor { + /// + /// ObjectStructure + /// public class CorruptionSuspect : IAsset { public CorruptionSuspect() @@ -43,11 +46,18 @@ namespace VisitorPattern.CalculateMoney.WithVisitor } } + + /// + /// Visitable + /// public interface IAsset { void Accept(IVisitor visitor); } + /// + /// Concrete Visitable + /// public class Job : IAsset { public double Salary { get; set; } @@ -62,6 +72,9 @@ namespace VisitorPattern.CalculateMoney.WithVisitor } } + /// + /// Concrete Visitable + /// public class MoneyBankAccount : IAsset { public double Ammount { get; set; } @@ -80,6 +93,9 @@ namespace VisitorPattern.CalculateMoney.WithVisitor } } + /// + /// Concrete Visitable + /// public class Valuable : IAsset { public double EstimatedValue { get; set; } @@ -92,12 +108,24 @@ namespace VisitorPattern.CalculateMoney.WithVisitor } } + /// + /// Concrete Visitable + /// public class Clock : Valuable { } + /// + /// Concrete Visitable + /// public class RealEstate : Valuable { } + /// + /// Concrete Visitable + /// public class Art : Valuable { } + /// + /// Concrete Visitable + /// public class Loan : IAsset { public double Owed { get; set; } diff --git a/src/VisitorPattern/CalculateMoney/WithVisitor/SuspectVisitors.cs b/src/VisitorPattern/CalculateMoney/WithVisitor/SuspectVisitors.cs index 5cb3244..9d51f7d 100644 --- a/src/VisitorPattern/CalculateMoney/WithVisitor/SuspectVisitors.cs +++ b/src/VisitorPattern/CalculateMoney/WithVisitor/SuspectVisitors.cs @@ -2,6 +2,9 @@ namespace VisitorPattern.CalculateMoney.WithVisitor { + /// + /// Visitor + /// public interface IVisitor { void Visit(MoneyBankAccount moneyBankAccount); @@ -11,6 +14,10 @@ namespace VisitorPattern.CalculateMoney.WithVisitor void Visit(Valuable valuable); } + + /// + /// Concrete Visitor + /// public class NetWorthVisitor : IVisitor { public double NetWorth { get; private set; } @@ -36,6 +43,9 @@ namespace VisitorPattern.CalculateMoney.WithVisitor } } + /// + /// Concrete Visitor + /// public class NetWorth2Visitor : IVisitor { public double NetWorth { get; private set; } @@ -61,6 +71,9 @@ namespace VisitorPattern.CalculateMoney.WithVisitor } } + /// + /// Concrete Visitor + /// public class MonthlyIncomeVisitor : IVisitor { public double MonthlyIncome { get; private set; }