royosherove / aout2 Goto Github PK
View Code? Open in Web Editor NEWCode Samples for the book The Art Of Unit Testing 2nd Edition
Home Page: http://ArtOfUnitTesting.com
Code Samples for the book The Art Of Unit Testing 2nd Edition
Home Page: http://ArtOfUnitTesting.com
Welcome to the .NET code samples for "The Art Of Unit Testing - Second Edition" By Roy Osherove Lots of videos and the book itself can be found at http://ArtOfUnitTesting.com or at http://manning.com/osherove2 You can also find a NodeJS version of these samples, courtesy of @DevCorpio at https://github.com/devcorpio/the-art-of-unit-testing
The Simple Factory and Factory Method patterns are both design patterns used in object-oriented programming to create objects. The main difference between the two is that Simple Factory is a single factory class that creates objects of different classes based on an input, whereas Factory Method is a method within a class that creates objects of the class or its subclasses.
Here's a simple example of Simple Factory in C#:
public class SimpleFactory
{
public static ICar CreateCar(string carType)
{
switch (carType)
{
case "Sports":
return new SportsCar();
case "Sedan":
return new SedanCar();
default:
throw new ArgumentException("Invalid car type");
}
}
}
public interface ICar
{
string GetType();
}
public class SportsCar : ICar
{
public string GetType()
{
return "Sports Car";
}
}
public class SedanCar : ICar
{
public string GetType()
{
return "Sedan Car";
}
}
// Usage
ICar car = SimpleFactory.CreateCar("Sports");
Console.WriteLine(car.GetType());
Here's a simple example of Factory Method in C#:
public abstract class CarFactory
{
public abstract ICar CreateCar();
}
public class SportsCarFactory : CarFactory
{
public override ICar CreateCar()
{
return new SportsCar();
}
}
public class SedanCarFactory : CarFactory
{
public override ICar CreateCar()
{
return new SedanCar();
}
}
public interface ICar
{
string GetType();
}
public class SportsCar : ICar
{
public string GetType()
{
return "Sports Car";
}
}
public class SedanCar : ICar
{
public string GetType()
{
return "Sedan Car";
}
}
// Usage
CarFactory factory = new SportsCarFactory();
ICar car = factory.CreateCar();
Console.WriteLine(car.GetType());
In general, the Simple Factory pattern is simpler and easier to understand, but the Factory Method pattern is more flexible and allows for better abstraction and encapsulation of the creation process. The choice between the two will depend on the specific requirements and constraints of your project.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.