Update description for visitor
This commit is contained in:
29
README.md
29
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
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@ using System.Collections.Generic;
|
||||
|
||||
namespace VisitorPattern.CalculateMoney.WithVisitor
|
||||
{
|
||||
/// <summary>
|
||||
/// ObjectStructure
|
||||
/// </summary>
|
||||
public class CorruptionSuspect : IAsset
|
||||
{
|
||||
public CorruptionSuspect()
|
||||
@@ -43,11 +46,18 @@ namespace VisitorPattern.CalculateMoney.WithVisitor
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Visitable
|
||||
/// </summary>
|
||||
public interface IAsset
|
||||
{
|
||||
void Accept(IVisitor visitor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete Visitable
|
||||
/// </summary>
|
||||
public class Job : IAsset
|
||||
{
|
||||
public double Salary { get; set; }
|
||||
@@ -62,6 +72,9 @@ namespace VisitorPattern.CalculateMoney.WithVisitor
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete Visitable
|
||||
/// </summary>
|
||||
public class MoneyBankAccount : IAsset
|
||||
{
|
||||
public double Ammount { get; set; }
|
||||
@@ -80,6 +93,9 @@ namespace VisitorPattern.CalculateMoney.WithVisitor
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete Visitable
|
||||
/// </summary>
|
||||
public class Valuable : IAsset
|
||||
{
|
||||
public double EstimatedValue { get; set; }
|
||||
@@ -92,12 +108,24 @@ namespace VisitorPattern.CalculateMoney.WithVisitor
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete Visitable
|
||||
/// </summary>
|
||||
public class Clock : Valuable { }
|
||||
|
||||
/// <summary>
|
||||
/// Concrete Visitable
|
||||
/// </summary>
|
||||
public class RealEstate : Valuable { }
|
||||
|
||||
/// <summary>
|
||||
/// Concrete Visitable
|
||||
/// </summary>
|
||||
public class Art : Valuable { }
|
||||
|
||||
/// <summary>
|
||||
/// Concrete Visitable
|
||||
/// </summary>
|
||||
public class Loan : IAsset
|
||||
{
|
||||
public double Owed { get; set; }
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace VisitorPattern.CalculateMoney.WithVisitor
|
||||
{
|
||||
/// <summary>
|
||||
/// Visitor
|
||||
/// </summary>
|
||||
public interface IVisitor
|
||||
{
|
||||
void Visit(MoneyBankAccount moneyBankAccount);
|
||||
@@ -11,6 +14,10 @@ namespace VisitorPattern.CalculateMoney.WithVisitor
|
||||
void Visit(Valuable valuable);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Concrete Visitor
|
||||
/// </summary>
|
||||
public class NetWorthVisitor : IVisitor
|
||||
{
|
||||
public double NetWorth { get; private set; }
|
||||
@@ -36,6 +43,9 @@ namespace VisitorPattern.CalculateMoney.WithVisitor
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete Visitor
|
||||
/// </summary>
|
||||
public class NetWorth2Visitor : IVisitor
|
||||
{
|
||||
public double NetWorth { get; private set; }
|
||||
@@ -61,6 +71,9 @@ namespace VisitorPattern.CalculateMoney.WithVisitor
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete Visitor
|
||||
/// </summary>
|
||||
public class MonthlyIncomeVisitor : IVisitor
|
||||
{
|
||||
public double MonthlyIncome { get; private set; }
|
||||
|
||||
Reference in New Issue
Block a user