Skip to content

cbojar/acceptor-csharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Acceptor (in C#)

This is a generic, universal implementation of the Visitor Pattern. It can extend the visitor pattern to any object or collection of objects. Internally, it checks the type of the wrapped object against the desired type, and calls the visitor if the internal object can be cast to the desired type.

Usage

To get an IAcceptor, instantiate the appropriate class. For IAcceptors for single objects, use new Acceptor(Object) and pass in the object. For multiple objects in an Enumerable, use new EnumerableAcceptor(Enumerable) and pass in the Enumerable. Both of these methods return an IAcceptor<T> object, where T is the type of the objects to visit.

To visit the object(s) in an IAcceptor, call the IAcceptor#accept method with an Action<U>, where U : T, that is the behavior you want called on the object(s) to visit. The input parameter to the Action will be the object to visit, cast to U.

The IAcceptor API is fluent, meaning IAcceptor#accept returns another IAcceptor, usually the same acceptor, so that #accept can be called again in chain.

Here is an example of how to use a single-object IAcceptor:

using System;
using CBojar.Acceptors;

public class AcceptorsRunner
{
    public static void Main(string[] args) {
        IAcceptor<String> acceptor = new Acceptor("String");
        acceptor.accept((String s) => {
            ("It's a string! Size: {0:D} Value: {1}\n", s.Length, s);
        });
    }
}

Here is an example of how to use an EnumerableAcceptor:

using System;
using CBojar.Acceptors;

public class AcceptorsRunner
{
	public static void Main(string[] args) {
		IAcceptor<Object> acceptor = new EnumerableAcceptor<Object>(new Object[] {
			"String", "String 2", 1
		});
		acceptor.Accept((String s) => {
			Console.WriteLine(String.Format("It's a string! Size: {0:D} Value: {1}", s.Length, s));
		}).Accept((int i) => {
			Console.WriteLine(String.Format("It's an int! Value: {0:D}", i));
		});
	}
}

About

A generic, universal implementation of the Visitor Pattern.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages