xin chào mn hiện em đang học về design pattern mong mn xem em vẽ design pattern và viết code vậy có đúng chưa?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Builder
{
public class Mobile
{
public string Id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public Mobile(string id, string name, string color)
{
Id = id;
Name = name;
Color = color;
}
public Mobile(string id)
{
Id = id;
}
public Mobile()
{
}
public override string ToString()
{
var content = "";
content += $"Id: \t {Id}\n";
content += $"Name: \t {Name}\n";
content += $"Color: \t {Color}\n";
return content;
}
}
public interface IMobileBuilder
{
MobileBuilder AddId(string id);
MobileBuilder AddName(string name);
MobileBuilder AddColor(string color);
Mobile Build();
}
public class MobileBuilder : IMobileBuilder
{
private readonly Mobile mobile;
public MobileBuilder()
{
mobile = new Mobile();
}
//Mobile id
public MobileBuilder AddId(string id)
{
mobile.Id = id;
return this;
}
public MobileBuilder AddName(string name)
{
mobile.Name = name;
return this;
}
public MobileBuilder AddColor(string color)
{
mobile.Color = color;
return this;
}
public Mobile Build()
{
return mobile;
}
}
public class Program
{
static void Main(string[] args)
{
var mobileBuilder = new MobileBuilder()
.AddId("1")
.AddName("Samsung")
.AddColor("Black")
.Build();
Console.WriteLine(mobileBuilder.ToString());
Console.ReadLine();
}
}
}