Avatar of purplesoup
purplesoup
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Can anyone help me give a class a helpful name related to a design pattern?

I have a class which makes it easier to run automated tests of order processing.


The class has properties and methods to allow different order settings to be specified, then a method is called to run the test and some objects returned to allow confirmation that the test was successful.

I want to know what pattern this might be so I can give the class a helpful name (I've been reading through the various patterns and I can't really identify a good match).

Examples:

var sut = new Tester().WithMugInCart().WithCreditCardPurchase().WithPurchaseSuccessful();

var result = sut.RunTest();

Assert.IsTrue(result.Order.Successful);
etc

C#.NET ProgrammingProgramming Theory

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
Jonathan Dahan

var sut = new Tester().WithMugInCart().WithCreditCardPurchase().WithPurchaseSuccessful();

As you can see, you're building the process that the automation has to go through by chaining multiple methods to it. This could potentially be the Chain of Responsibility pattern (not to confuse with creation pattern the Builder Pattern). You're creating the "request" (which is the Tester class) and immediately run through a chain of instructions, thus gives you the flexibility to choose which automation test to run at a specific sequence.

Also, it seems like you're inheriting a characteristic from the builder pattern where each method returns the current instance which then allows you to continue and chain them into a specific sequence. 
purplesoup

ASKER
Thanks - that's very interesting - I did wonder about the builder pattern but because it wasn't building and returning the object being tested I wasn't sure if it was legitimate - so with the Chain of Responsibility would a helpful suffix be "Handler" and the name something like "OrderProcessingTestHandler" and the method to run the test "Execute"?
Jonathan Dahan

so with the Chain of Responsibility would a helpful suffix be "Handler" and the name something like "OrderProcessingTestHandler" and the method to run the test "Execute"?

I'll give you my honest opinion here and tell you, no. Fancy names are just syntactic sugar, you're not forced to follow names exactly as the design pattern describes. Your method names are fine, more than enough. Don't let it worry you. Besides that, if you're worried about the names then you need to consider to refactor your implementation and follow the exact design as in the link I provided. Ending a method name with "TestHandler" wouldn't be right since it's not really a "handler", it's just a method. a handler is suppose to handle a specific event, so your correct approach would be to link multiple handlers in a specific sequence by having a method that accepts the "handler" method to it. Think of it like the builder pattern, you're "building" a set of "events" that occur at a specific sequence which when executed, they execute at the order they've been set.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
dpearson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
kaufmed

var sut = new Tester().WithMugInCart().WithCreditCardPurchase().WithPurchaseSuccessful();

This is known as "fluent" design.