Я никогда не был пользователем Diagram, и теперь мне нужно спроектировать диаграмму классов для фрагмента кода, который смешивает два разных шаблона проектирования (Abstract Factory + Builder).
Я пробовал проектироватьодин, но я думаю, что это не правильно, кто-то может мне помочь?
Вот код
namespace TestApp {
class Program {
static void Main(string[] args) {
//Default car factory is used
CarBuilder carBuilder = new CarBuilder();
CarFactory carFactory = carBuilder.GetCarFactory();
Console.WriteLine(carFactory.GetType());
//Customer now wants to plugin his new class SportsCarFactory
carBuilder.SetCarFactory(new SportsCarFactory());
carFactory = carBuilder.GetCarFactory();
Console.WriteLine(carFactory.GetType());
}
}
///
/// This is a object builder class which returns abstract Carfactory
///
public class CarBuilder {
//This is a delegate function
Func _factory;
public CarBuilder() {
//Lets set economical car factory as default car factory
SetCarFactory(new EconomicalCarFactory());
}
//Get car factory returns the instance of car factory that is set
public CarFactory GetCarFactory() {
return _factory();
}
//This is the place where user can plugin his custom implemented
//class that inherits CarFactory
public void SetCarFactory(CarFactory carFactory) {
_factory = () => carFactory;
}
}
//We got abstraction for Car
//Car can be sports car or economical car
public abstract class Car {
public abstract string CarType {
get;
}
}
//Economical car is more concrete, in the sense we have more details
//which is, we know its an economical car
public class EconomicalCar: Car {
public override string CarType {
get {
return " Economical car";
}
}
}
//Sports car is more concrete, in the sense we have more details
//which is, we know its a sports car
public class SportsCar: Car {
public override string CarType {
get {
return " Sports car";
}
}
}
//Car factory builds the car
//It knows its building the car,
//but doesnt know if the car is sports car or economical car
public abstract class CarFactory {
public abstract string FactoryType {
get;
}
protected abstract Car BuildCar();
//Both factories build cars
public Car Build() {
return this.BuildCar();
}
}
//Economical car factory builds economical cars
//More generalization of economical car factory is "car factory"
public class EconomicalCarFactory: CarFactory {
public override string FactoryType {
get {
return "Economical Car Factory";
}
}
protected override Car BuildCar() {
return new EconomicalCar();
}
}
//Sports car factory builds sports cars
//More generalization of sports car factory is "car factory"
public class SportsCarFactory: CarFactory {
public override string FactoryType {
get {
return "Sports Car Factory";
}
}
protected override Car BuildCar() {
return new SportsCar();
}
}
}
Вот что я сделал до сих пор