Design Principles

  • Published on
    15-May-2015

  • View
    4.577

  • Download
    0

Embed Size (px)

DESCRIPTION

Presentation from UNETA (Kharkov, Ukraine) Design Principles

Transcript

  • 1. AltexSoft

2. 3. 4. : ? 5. 6. Project timeline , 7. % % % : , 8. , (extensibility, agility) (maintainability) (simplicity) , (readability, clarity) (testability) , - , , . 9. ? , ? - code smell : code review TDD/BDD 10. . 11. 12. SoC: Separation of Concerns (, , , ), . : (tiers) (layers) 13. DRY: Dont Repeat Yourself , , . : //. , : copy-paste , AOP 14. Low Coupling Coupling () , , , . : dont talk to the strangers 15. High Cohesion Cohesion () , / . : 16. KISS: Keep It Simple and Stupid . : : , , : , : , 17. YAGNI: You Aint Gonna Need It . : : : ! 18. - 19. SRP: Single Responsibility Principle 1. . 2. . : Separation of Concerns : 20. SRP: Single Responsibility Principle 21. SRP: public class Account { public string Number; public decimal CurrentBalance; public void Deposit(decimal amount) { ... } public void Withdraw(decimal amount) { ... } public void Transfer(decimal amount, Account recipient) { ... } public TaxTable CalculateTaxes(int year) { ... } public void GetByNumber(string number) { ... } public void Save() { ... } } () Igor Tamaschuk 22. SRP: public class Account { public string Number; public decimal CurrentBalance; public void Deposit(decimal amount) { ... } public void Withdraw(decimal amount) { ... } public void Transfer(decimal amount, Account recipient) { ... } } public class AccountRepository { public Account GetByNumber(string number) { ... } public void Save(Account account) { ... } } public class TaxCalculator { public TaxTable CalculateTaxes(int year) { ... } } 23. OCP: Open-Closed Principle (, , ) , . : : 24. OCP: public class SmtpMailer { private readonly Logger logger; public SmtpMailer() { logger = new Logger(); } public void SendMessage(string message) { // logger.Log(message); } } () Alexander Byndyu : public class DatabaseLogger { public void Log(string logText) { // } } public class SmtpMailer { private readonly DatabaseLogger logger; public SmtpMailer() { logger = new DatabaseLogger(); } public void SendMessage(string message) { // logger.Log(message); } } 25. OCP: public class SmtpMailer { private readonly ILogger logger; public SmtpMailer(ILogger logger) { this.logger = logger; } public void SendMessage(string message) { // logger.Log(message); } } 26. LSP: Liskov Substitution Principle 1. . 2. : , , : 27. LSP: Liskov Substitution Principle 28. LSP: class Rectangle { public virtual int Width { get; set; } public virtual int Height { get; set; } public int CalculateArea() { return Width * Height; } } class Square : Rectangle { public override int Height { get { return base.Height; } set { base.Height = value; base.Width = value; } } public override int Width { get { return base.Width; } set { base.Width = value; base.Height = value; } } } class Program { static void Main() { Rectangle r = new Square(); r.Width = 3; r.Height = 2; Assert.AreEqual(6, r.CalculateArea()); } } () Igor Tamaschuk 29. LSP: class Rectangle : IFigure { public virtual int Width { get; set; } public virtual int Height { get; set; } public int CalculateArea() { return Width * Height; } } class Square : IFigure { public virtual int Side { get; set; } public int CalculateArea() { return Side * Side; } } class Program { static void Main() { Rectangle r = new Square(); // fail on compilation r.Width = 3; r.Height = 2; Assert.AreEqual(6, r.CalculateArea()); } } 30. ISP: Interface Segregation Principle 1. , . 2. . : : , 31. ISP: public interface IBird { void Eat(); void Sing(); void Fly(); } public class Parrot : IBird { // } public class Pigeon : IBird { // , , } public class Penguin : IBird { // ... ? } 32. ISP: public interface ICommonBird { void Eat(); } public interface ISingingBird { void Sing(); } public interface IFlyingBird { void Fly(); } public class Parrot : ICommonBird, ISingingBird, IFlyingBird { // , } public class Pigeon : ICommonBird, IFlyingBird { // , , } public class Penguin : ICommonBird { // ! } 33. IoC: Inversion of Control DIP: Dependency Inversion Principle 1. . . 2. . . : (coupling) : Hollywood principle: Dont call us, well call you : Dependency Injection, Service Locator IoC frameworks 34. IoC, DIP: class Developer { public void WriteApplication() { IApplication helloWorld = new HelloWorldApp(); WriteCode(helloWorld); } private void WriteCode() { ... }; } public interface IApplication { string GetCode(); } public class HelloWorldApp : IApplication { // } public class VeryBigApp : IApplication { // } 35. IoC, DIP: class Developer { public void WriteApplication(IApplication app) { WriteCode(app); } private void WriteCode() { ... }; } public static void Main() { Developer dev = new Developer(); IApplication app = new VeryBigApp(); // dev.WriteApplication(app); } Dependency Injection (Method Injection), (, Service Locator), IoC Frameworks 36. 37. It depends ! 38. !