-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed14722
commit c7f5079
Showing
29 changed files
with
336 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChainOfResponsibility.Classes.Domin; | ||
|
||
public class CustomerModel | ||
{ | ||
public string Name { get; set; } | ||
public string Account { get; set; } | ||
public string Address { get; set; } | ||
public string Number { get; set; } | ||
|
||
|
||
} | ||
|
17 changes: 17 additions & 0 deletions
17
ChainOfResponsibility/Classes/Service/CustomerAddressValid.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using ChainOfResponsibility.Classes.Domin; | ||
using ChainOfResponsibility.Classes.interfaces; | ||
|
||
namespace ChainOfResponsibility.Classes.Service; | ||
|
||
|
||
public class CustomerAddressValid : AbstractHandler | ||
{ | ||
public override object Handle(object request) | ||
{ | ||
CustomerModel vm = (CustomerModel)request; | ||
|
||
if (vm.Address.Split(",").Count() != 3) | ||
return $"Address is not valid"; | ||
return base.Handle(request); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
ChainOfResponsibility/Classes/Service/CustomerNameValid.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using ChainOfResponsibility.Classes.Domin; | ||
using ChainOfResponsibility.Classes.interfaces; | ||
|
||
namespace ChainOfResponsibility.Classes.Service; | ||
|
||
|
||
public class CustomerNameValid : AbstractHandler | ||
{ | ||
public override object Handle(object request) | ||
{ | ||
CustomerModel vm = (CustomerModel)request; | ||
|
||
if (vm.Name.Length < 10) | ||
return $"Name is not valid"; | ||
return base.Handle(request); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
ChainOfResponsibility/Classes/Service/CustomerNumberValid.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using ChainOfResponsibility.Classes.Domin; | ||
using ChainOfResponsibility.Classes.interfaces; | ||
|
||
namespace ChainOfResponsibility.Classes.Service; | ||
|
||
|
||
public class CustomerNumberValid : AbstractHandler | ||
{ | ||
public override object Handle(object request) | ||
{ | ||
CustomerModel vm = (CustomerModel)request; | ||
|
||
if (vm.Number.Length == 10) | ||
return $"number is not valid"; | ||
return base.Handle(request); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
ChainOfResponsibility/Classes/interfaces/AbstractHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.ConstrainedExecution; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChainOfResponsibility.Classes.interfaces; | ||
|
||
public abstract class AbstractHandler : IHandler | ||
{ | ||
private IHandler NextHandler; | ||
public virtual object Handle(object request) => (NextHandler?.Handle(request)); | ||
|
||
|
||
|
||
|
||
public IHandler SetNext(IHandler next) | ||
{ | ||
NextHandler = next; | ||
return next; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChainOfResponsibility.Classes.interfaces; | ||
|
||
public interface IHandler | ||
{ | ||
IHandler SetNext(IHandler handler); | ||
object Handle(object request); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using ChainOfResponsibility.Classes.Domin; | ||
using ChainOfResponsibility.Classes.Service; | ||
|
||
CustomerModel Customervm = new CustomerModel() { Number = "123", Name = "test4", Address = "tehran,shariati,12", Account = "123" }; | ||
|
||
|
||
CustomerNameValid _CustomerNameValidHandler = new CustomerNameValid(); | ||
CustomerNumberValid _CustomerNumberValidHandler = new CustomerNumberValid(); | ||
CustomerAddressValid _CustomerAddressValidHandler = new CustomerAddressValid(); | ||
|
||
|
||
_CustomerNameValidHandler.SetNext(_CustomerNumberValidHandler).SetNext(_CustomerAddressValidHandler); | ||
|
||
var finalMessage = _CustomerNameValidHandler.Handle(Customervm); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
the Chain of Responsibility relies on transforming particular behaviors into stand-alone objects called handlers. | ||
In our case, each check should be extracted to its own class with a single method that performs the check. | ||
The request, along with its data, is passed to this method as an argument. | ||
|
||
The pattern suggests that you link these handlers into a chain. | ||
Each linked handler has a field for storing a reference to the next handler in the chain. | ||
In addition to processing a request, handlers pass the request further along the chain. | ||
The request travels along the chain until all handlers have had a chance to process it. | ||
|
||
Here’s the best part: a handler can decide not to pass the request further down the chain and effectively stop any further processing. | ||
|
||
In our example with ordering systems, a handler performs the processing and then decides whether to pass the request further down the chain. | ||
Assuming the request contains the right data, all the handlers can execute their primary behavior, whether it’s authentication checks or caching. | ||
|
||
|
||
---- | ||
|
||
Chain of Responsibility and Decorator have very similar class structures. | ||
Both patterns rely on recursive composition to pass the execution through a series of objects. However, there are several crucial differences. | ||
|
||
The CoR handlers can execute arbitrary operations independently of each other. | ||
They can also stop passing the request further at any point. | ||
On the other hand, various Decorators can extend the object’s behavior while keeping it consistent with the base interface. | ||
In addition, decorators aren’t allowed to break the flow of the request. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
Console.WriteLine("Hello, World!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
Console.WriteLine("Hello, World!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Extending a class is the first thing that comes to mind when you need to alter an object’s behavior. | ||
However, inheritance has several serious caveats that you need to be aware of. | ||
|
||
Inheritance is static. You can’t alter the behavior of an existing object at runtime. | ||
You can only replace the whole object with another one that’s created from a different subclass. | ||
Subclasses can have just one parent class. | ||
In most languages, inheritance doesn’t let a class inherit behaviors of multiple classes at the same time. | ||
One of the ways to overcome these caveats is by using Aggregation or Composition instead of Inheritance. | ||
Both of the alternatives work almost the same way: | ||
one object has a reference to another and delegates it some work, whereas with inheritance, | ||
the object itself is able to do that work, inheriting the behavior from its superclass. | ||
|
||
With this new approach you can easily substitute the linked “helper” object with another, changing the behavior of the container at runtime. | ||
An object can use the behavior of various classes, having references to multiple objects and delegating them all kinds of work. | ||
Aggregation/composition is the key principle behind many design patterns, including Decorator. On that note, let’s return to the pattern discussion. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
Console.WriteLine("Hello, World!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
A facade is a class that provides a simple interface to a complex subsystem which contains lots of moving parts. | ||
A facade might provide limited functionality in comparison to working with the subsystem directly. | ||
However, it includes only those features that clients really care about. | ||
|
||
Having a facade is handy when you need to integrate your app with a sophisticated library that has dozens of features, | ||
but you just need a tiny bit of its functionality. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
Console.WriteLine("Hello, World!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
Console.WriteLine("Hello, World!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
Console.WriteLine("Hello, World!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
Console.WriteLine("Hello, World!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
Console.WriteLine("Hello, World!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
The Visitor pattern suggests that you place the new behavior into a separate class called visitor,instead of trying to integrate it into existing classes. | ||
The original object that had to perform the behavior is now passed to one of the visitor’s methods as an argument, | ||
providing the method access to all necessary data contained within the object. | ||
|
||
Now, what if that behavior can be executed over objects of different classes? | ||
https://www.dntips.ir/post/2373/%d8%a7%d9%84%da%af%d9%88%db%8c-%d8%a8%d8%a7%d8%b2%d8%af%db%8c%d8%af%da%a9%d9%86%d9%86%d8%af%d9%87-visitor-pattern |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |