<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Eren OKUR on Medium]]></title>
        <description><![CDATA[Stories by Eren OKUR on Medium]]></description>
        <link>https://medium.com/@erenokur.eo?source=rss-4d75a52a76da------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uIUmfEe6eOxrB-7qLan_2g.jpeg</url>
            <title>Stories by Eren OKUR on Medium</title>
            <link>https://medium.com/@erenokur.eo?source=rss-4d75a52a76da------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 02:08:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@erenokur.eo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mutable and Immutable concepts in C# and Node.js]]></title>
            <link>https://blog.devgenius.io/mutable-and-immutable-concepts-in-c-and-node-js-f65c3dd17537?source=rss-4d75a52a76da------2</link>
            <guid isPermaLink="false">https://medium.com/p/f65c3dd17537</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[data-type]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Eren OKUR]]></dc:creator>
            <pubDate>Tue, 12 Mar 2024 18:22:14 GMT</pubDate>
            <atom:updated>2024-03-19T13:25:55.023Z</atom:updated>
            <content:encoded><![CDATA[<p>In software languages like C#, Node.js, and many others, data types play a crucial role in managing and manipulating information. To effectively work with data, it’s essential to understand the concepts of mutable and immutable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g7HxahRypQXiG5qCJfJoWQ.jpeg" /><figcaption>Designed by vectorjuice / Freepik</figcaption></figure><p>Mutable data types are those whose values can be changed after they are created. This means that you can modify the content or properties of mutable data types during the execution of a program. For example, if you have a mutable list of numbers, you can add or remove elements from the list, or change the values of existing elements.</p><pre>List&lt;int&gt; mutableList = new List&lt;int&gt;() { 1, 2, 3 };<br>mutableList.Add(4); // Mutable operation: adding a number to the list</pre><p>On the other hand, immutable data types are those whose values cannot be changed after they are created. Once an immutable data type is created, its value remains fixed and cannot be modified. Instead of modifying existing immutable data, you create new instances with the desired changes.</p><pre>string immutableString = &quot;hello&quot;;<br>string newString = immutableString.ToUpper(); // Immutable operation: creating a new string with uppercase characters</pre><h3>Why Mutable and Immutable Types Matter</h3><ul><li><strong>Predictability: </strong>Immutable data types ensure predictability in code behavior because their values cannot change unexpectedly. This makes it easier to reason about the behavior of programs, especially in complex systems.</li><li><strong>Concurrency: </strong>In concurrent programming, where multiple threads or processes may access and modify shared data simultaneously, immutability helps prevent data races and synchronization issues. Immutable data types can be safely shared among multiple threads without the need for locks or synchronization mechanisms.</li><li><strong>Functional Programming</strong>: Immutability is a fundamental principle in functional programming paradigms. Functional programming languages, like Haskell or Clojure, rely heavily on immutable data types and pure functions, which do not have side effects and always return the same output for the same input.</li></ul><p>Mutable C#:</p><p>In C#, objects are mutable by default. Let’s consider a simple example of a Person class with mutable properties for their name and age.</p><pre>class Person<br>{<br>    public string Name { get; set; }<br>    public int Age { get; set; }<br>}<br><br>Person mutablePerson = new Person() { Name = &quot;Alice&quot;, Age = 30 };<br>mutablePerson.Age = 31; // Mutable operation: changing the age</pre><p>Immutable C#:</p><p>In C#, you can achieve immutability by using readonly properties or by making your class immutable with read-only properties and a constructor to set them.</p><pre>class ImmutablePerson<br>{<br>    public string Name { get; }<br>    public int Age { get; }<br><br>    public ImmutablePerson(string name, int age)<br>    {<br>        Name = name;<br>        Age = age;<br>    }<br>}<br><br>ImmutablePerson immutablePerson = new ImmutablePerson(&quot;Bob&quot;, 25);<br>// immutablePerson.Age = 26; // This would result in a compile-time error because Age is read-only</pre><p>Mutable Node.js:</p><p>In Node.js, JavaScript objects are also mutable by default. Here’s a similar example using JavaScript:</p><pre>class Person {<br>    constructor(name, age) {<br>        this.name = name;<br>        this.age = age;<br>    }<br>}<br><br>let mutablePerson = new Person(&quot;Alice&quot;, 30);<br>mutablePerson.age = 31; // Mutable operation: changing the age</pre><p>Immutable Node.js:</p><p>In Node.js, achieving immutability is a bit different. You can use libraries like Immutable.js or simply follow a functional programming approach to create new objects with the desired changes instead of modifying existing ones.</p><pre>const ImmutablePerson = function (name, age) {<br>    return Object.freeze({ name, age });<br>};<br><br>let immutablePerson = ImmutablePerson(&quot;Bob&quot;, 25);<br>// immutablePerson.age = 26; // This would result in an error in strict mode due to object freezing</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f65c3dd17537" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/mutable-and-immutable-concepts-in-c-and-node-js-f65c3dd17537">Mutable and Immutable concepts in C# and Node.js</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SOLID principles]]></title>
            <link>https://medium.com/@erenokur.eo/solid-principles-fecc2ab8b815?source=rss-4d75a52a76da------2</link>
            <guid isPermaLink="false">https://medium.com/p/fecc2ab8b815</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[solid-principles]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Eren OKUR]]></dc:creator>
            <pubDate>Sun, 17 Dec 2023 11:17:08 GMT</pubDate>
            <atom:updated>2023-12-17T11:17:08.890Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zjKjIXqlWgfbSbR_SayHHA.jpeg" /><figcaption>Image by upklyak on Freepik</figcaption></figure><p>Computers and software systems are essential components in our daily lives. We rely on them for many daily tasks, from checking the weather, browsing news, socializing and connecting us to the world. Software undergoes constant evolution, with developers regularly adding new features and updating existing functionalities.</p><p>While maintaining small software projects is relatively straightforward, the intricate architecture of most complex systems makes their maintenance challenging. To address this challenge, Robert C. Martin, also known as Uncle Bob, developed the SOLID principles. These principles, initially introduced in Martin’s various publications, became a pivotal point in the field of software development and continue to serve as essential guidelines for creating robust and maintainable systems.</p><p>Here is the list of SOLID principles:</p><ol><li>Single Responsibility Principle (SRP)</li><li>Open/Closed Principle (OCP)</li><li>Liskov Substitution Principle (LSP)</li><li>Interface Segregation Principle (ISP)</li><li>Dependency Inversion Principle (DIP)</li></ol><p>The SOLID principles not only enable programmers to grasp the code base more readily, but also empower them to effortlessly add new features and maintain existing code. Let’s go deeper in to these principles.</p><p><strong>Single Responsibility Principle (SRP)</strong></p><p>This principle a fundamental concept in object-oriented design that promotes code maintainability, test-ability, and readability by ensuring that each class has only one reason to change and only have one responsibility. This means a class should only have one job or function.</p><p>SRP principle encourages developers to break down their systems into smaller more manageable components by assigning them specific responsibilities to individual classes or modules. This focused single responsibility of each class promotes modularity. So the base code more readable, understandable, easy to maintain and easy to extend.</p><p>Why use SRP in software systems;</p><ul><li>Maintainability: Classes easier to understand and modify. Changes in the code less likely effect other unrelated parts of the source.</li><li>Test-ability: Classes become more smaller and focused to its objectives. This makes tests more well-defined and isolated.</li><li>Re-usability: Simple one purpose codes are easy to reuse in different contexts.</li></ul><p>Single responsibility is very useful on software systems and mainly those systems are usually uses design patterns. SRP is a principle that can be applied more frequently with observer, command and decorator patterns to achieve separation of concerns and loose coupling between objects.</p><ul><li>Observer Pattern: The subject is only has one responsibility of managing its own state and notifying the observers. On the other hand observers only have the responsibility of updating themselves according to the change. This way subject does not know anything about the observes and the observes do not need to know anything about the subject’s state. This makes the design more modular, flexible and easy to maintain.</li></ul><pre>using System;<br>using System.Collections.Generic;<br><br>// Subject<br>public interface IAnimal<br>{<br>    void Attach(ZooKeeper zooKeeper);<br>    void Detach(ZooKeeper zooKeeper);<br>    void Notify();<br>}<br><br>// Concrete Subject<br>public class Animal : IAnimal<br>{<br>    private List&lt;ZooKeeper&gt; _zooKeepers = new List&lt;ZooKeeper&gt;();<br>    private bool _isHungry = false;<br><br>    public void Attach(ZooKeeper zooKeeper)<br>    {<br>        _zooKeepers.Add(zooKeeper);<br>    }<br><br>    public void Detach(ZooKeeper zooKeeper)<br>    {<br>        _zooKeepers.Remove(zooKeeper);<br>    }<br><br>    public void Notify()<br>    {<br>        foreach (var zooKeeper in _zooKeepers)<br>        {<br>            zooKeeper.Update(this);<br>        }<br>    }<br><br>    public void IsHungry(bool value)<br>    {<br>        _isHungry = value;<br>        Notify();<br>    }<br>}<br><br>// Observer<br>public abstract class ZooKeeper<br>{<br>    public abstract void Update(Animal animal);<br>}<br><br>// Concrete Observer<br>public class ConcreteZooKeeper : ZooKeeper<br>{<br>    public override void Update(Animal animal)<br>    {<br>        Console.WriteLine(&quot;The animal is hungry. Feeding the animal now.&quot;);<br>    }<br>}<br><br>// Client<br>public class Program<br>{<br>    static void Main(string[] args)<br>    {<br>        Animal animal = new Animal();<br>        ZooKeeper zooKeeper = new ConcreteZooKeeper();<br>        animal.Attach(zooKeeper);<br>        animal.IsHungry(true);<br>    }<br>}</pre><pre>// Output:<br><br>The animal is hungry. Feeding the animal now.</pre><ul><li>Command Pattern: This pattern needs to encapsulate each command as an object with single responsibility. This way, the invoker does not know the details of how the command is implemented, and receiver does not need to know the identity of the invoker.</li></ul><pre>using System;<br><br>// Command Interface<br>public interface ICommand<br>{<br>    void Execute();<br>}<br><br>// Concrete Command<br>public class MakeSoundCommand : ICommand<br>{<br>    private Animal _animal;<br><br>    public MakeSoundCommand(Animal animal)<br>    {<br>        _animal = animal;<br>    }<br><br>    public void Execute()<br>    {<br>        _animal.MakeSound();<br>    }<br>}<br><br>// Invoker<br>public class AnimalSoundInvoker<br>{<br>    private ICommand _command;<br><br>    public AnimalSoundInvoker(ICommand command)<br>    {<br>        _command = command;<br>    }<br><br>    public void MakeSound()<br>    {<br>        _command.Execute();<br>    }<br>}<br><br>// Receiver<br>public abstract class Animal<br>{<br>    public abstract void MakeSound();<br>}<br><br>// Concrete Receiver<br>public class Dog : Animal<br>{<br>    public override void MakeSound()<br>    {<br>        Console.WriteLine(&quot;Woof!&quot;);<br>    }<br>}<br><br>public class Cat : Animal<br>{<br>    public override void MakeSound()<br>    {<br>        Console.WriteLine(&quot;Meow!&quot;);<br>    }<br>}<br><br>// Client<br>public class Program<br>{<br>    static void Main(string[] args)<br>    {<br>        Animal dog = new Dog();<br>        ICommand dogSoundCommand = new MakeSoundCommand(dog);<br>        AnimalSoundInvoker dogInvoker = new AnimalSoundInvoker(dogSoundCommand);<br>        dogInvoker.MakeSound(); <br><br>        Animal cat = new Cat();<br>        ICommand catSoundCommand = new MakeSoundCommand(cat);<br>        AnimalSoundInvoker catInvoker = new AnimalSoundInvoker(catSoundCommand);<br>        catInvoker.MakeSound(); <br>    }<br>}</pre><pre>// Output: <br>Woof!<br>Meow!</pre><ul><li>Decorator Pattern: New functionality need to be added to the existing code without altering its structure so with the help of SRP decorator does not need to know the details of how the object is implemented, and the object does not need to know identity of the decorator.</li></ul><pre>using System;<br><br>// Abstract Component<br>public abstract class Animal<br>{<br>    public abstract string MakeSound();<br>}<br><br>// Concrete Component<br>public class Dog : Animal<br>{<br>    public override string MakeSound()<br>    {<br>        return &quot;Woof!&quot;;<br>    }<br>}<br><br>public class Cat : Animal<br>{<br>    public override string MakeSound()<br>    {<br>        return &quot;Meow!&quot;;<br>    }<br>}<br><br>// Abstract Decorator<br>public abstract class AnimalDecorator : Animal<br>{<br>    protected Animal _animal;<br><br>    public AnimalDecorator(Animal animal)<br>    {<br>        _animal = animal;<br>    }<br><br>    public override string MakeSound()<br>    {<br>        return _animal.MakeSound();<br>    }<br>}<br><br>// Concrete Decorator<br>public class LoudAnimal : AnimalDecorator<br>{<br>    public LoudAnimal(Animal animal) : base(animal) { }<br><br>    public override string MakeSound()<br>    {<br>        return _animal.MakeSound().ToUpper() + &quot;!!!&quot;;<br>    }<br>}<br><br>// Client<br>public class Program<br>{<br>    static void Main(string[] args)<br>    {<br>        Animal dog = new Dog();<br>        Animal loudDog = new LoudAnimal(dog);<br>        Console.WriteLine(loudDog.MakeSound()); <br><br>        Animal cat = new Cat();<br>        Animal loudCat = new LoudAnimal(cat);<br>        Console.WriteLine(loudCat.MakeSound()); <br>    }<br>}</pre><pre>// Output: <br>WOOF!!!<br>MEOW!!!</pre><p>SRP is very important for code maintainability, because with it classes that adhere to SRP are easier to modify and extend without affecting unrelated functionality. SRP also help collaboration in teams minimizing conflict and allow parallel programming.</p><p>With the help of SRP Code will be more readable, classes are more clear and has single responsible. Dependency injection of dependencies into a class is more easier to manage and test. SRP also allow aspect-oriented programming (AOP) to be implemented to business logic to modularize cross-cutting concerns such as logging or security. This principle introduced by Robert C. Martin.</p><p><strong>Open/Closed Principle (OCP)</strong></p><p>OCP principle promotes the creation of flexible and maintainable software by allowing the extension of functionality. it is states that a class should be open for extension but closed for modification. So the behavior of a module can be extended without modifying its source code. This pattern is very important for building software that can evolve over time to meet changing requirements.</p><p>Why use OCP in software systems;</p><ul><li>Flexibility: New features can be added by creating new classes rather than modifying existing code.</li><li>Maintainability: Existing code does not change in this pattern so the risk of new bugs or unintended side effects are minimized.</li><li>Re-usability: The code created with OCP principle is generally is re-usable in different context. But they can be extended without modifying their core implementation.</li></ul><p>OCP pattern is frequently used by strategy pattern, decorator pattern and command pattern to archive extension of functionality without modifying existing code.</p><ul><li>Strategy Pattern: This pattern is a encapsulating different algorithms or behaviors in separate classes that implement a common interface. This way strategy pattern can switch between different strategies at runtime without modifying the class that uses them. OCR help this pattern to extent the functionality of a class by adding new strategies rather than modifying the existing ones. OCP with strategy pattern examples could be sorting algorithms(bubble sort, quick sort, merge sort, etc.) and payment methods(cash, credit card, etc.)</li></ul><pre>using System;<br><br>// Strategy Interface<br>public interface ISoundStrategy<br>{<br>    void MakeSound();<br>}<br><br>// Concrete Strategies<br>public class WoofSoundStrategy : ISoundStrategy<br>{<br>    public void MakeSound()<br>    {<br>        Console.WriteLine(&quot;Woof!&quot;);<br>    }<br>}<br><br>public class MeowSoundStrategy : ISoundStrategy<br>{<br>    public void MakeSound()<br>    {<br>        Console.WriteLine(&quot;Meow!&quot;);<br>    }<br>}<br><br>// Context<br>public class Animal<br>{<br>    private ISoundStrategy _soundStrategy;<br><br>    public Animal(ISoundStrategy soundStrategy)<br>    {<br>        _soundStrategy = soundStrategy;<br>    }<br><br>    public void MakeSound()<br>    {<br>        _soundStrategy.MakeSound();<br>    }<br>}<br><br>// Client<br>public class Program<br>{<br>    static void Main(string[] args)<br>    {<br>        Animal dog = new Animal(new WoofSoundStrategy());<br>        dog.MakeSound(); <br><br>        Animal cat = new Animal(new MeowSoundStrategy());<br>        cat.MakeSound(); <br>    }<br>}</pre><pre>// Output: <br>Woof!<br>Meow!</pre><ul><li>Decorator Pattern: The idea in this pattern to wrap and object with another object that has the same interface, but adds some additional behavior or functionality. With the help of OCP and decorator pattern creating different combinations of behaviors by using different decorators, without changing the original class or interface available.</li></ul><pre>using System;<br><br>// Abstract Component<br>public abstract class Animal<br>{<br>    public abstract string MakeSound();<br>}<br><br>// Concrete Component<br>public class Dog : Animal<br>{<br>    public override string MakeSound()<br>    {<br>        return &quot;Woof!&quot;;<br>    }<br>}<br><br>public class Cat : Animal<br>{<br>    public override string MakeSound()<br>    {<br>        return &quot;Meow!&quot;;<br>    }<br>}<br><br>// Abstract Decorator<br>public abstract class AnimalDecorator : Animal<br>{<br>    protected Animal _animal;<br><br>    public AnimalDecorator(Animal animal)<br>    {<br>        _animal = animal;<br>    }<br><br>    public override string MakeSound()<br>    {<br>        return _animal.MakeSound();<br>    }<br>}<br><br>// Concrete Decorator<br>public class LoudAnimal : AnimalDecorator<br>{<br>    public LoudAnimal(Animal animal) : base(animal) { }<br><br>    public override string MakeSound()<br>    {<br>        return _animal.MakeSound().ToUpper() + &quot;!!!&quot;;<br>    }<br>}<br><br>// Client<br>public class Program<br>{<br>    static void Main(string[] args)<br>    {<br>        Animal dog = new Dog();<br>        Animal loudDog = new LoudAnimal(dog);<br>        Console.WriteLine(loudDog.MakeSound()); <br><br>        Animal cat = new Cat();<br>        Animal loudCat = new LoudAnimal(cat);<br>        Console.WriteLine(loudCat.MakeSound()); <br>    }<br>}</pre><pre>// Output:<br>WOOF!!!<br>MEOW!!!</pre><ul><li>Command Pattern: This pattern is a behavioral pattern that encapsulates a request as an object. This behavior allows us to parameterize, queue, log and undoing different actions. The command pattern extents the functionality of a system by adding new commands, without changing the existing classes that invoke them. For example, suppose we have a class that represents a remote control for a TV, with methods like turnOn, turnOff, volumeUp, and volumeDown. Now, we want to add some new features like mute, channelUp, and channelDown. Instead of modifying the remote control class or creating subclasses for each feature, we can use the command pattern to create separate classes that implement a common interface for commands, such as execute and undo. Then, we can assign different commands to different buttons on the remote control, depending on the features we need. This way, we can respect the OCP principle and avoid modifying the existing code.</li></ul><pre>using System;<br><br>// Command Interface<br>public interface ICommand<br>{<br>    void Execute();<br>}<br><br>// Concrete Command<br>public class MakeSoundCommand : ICommand<br>{<br>    private Animal _animal;<br><br>    public MakeSoundCommand(Animal animal)<br>    {<br>        _animal = animal;<br>    }<br><br>    public void Execute()<br>    {<br>        _animal.MakeSound();<br>    }<br>}<br><br>// Invoker<br>public class AnimalSoundInvoker<br>{<br>    private ICommand _command;<br><br>    public AnimalSoundInvoker(ICommand command)<br>    {<br>        _command = command;<br>    }<br><br>    public void MakeSound()<br>    {<br>        _command.Execute();<br>    }<br>}<br><br>// Receiver<br>public abstract class Animal<br>{<br>    public abstract void MakeSound();<br>}<br><br>// Concrete Receiver<br>public class Dog : Animal<br>{<br>    public override void MakeSound()<br>    {<br>        Console.WriteLine(&quot;Woof!&quot;);<br>    }<br>}<br><br>public class Cat : Animal<br>{<br>    public override void MakeSound()<br>    {<br>        Console.WriteLine(&quot;Meow!&quot;);<br>    }<br>}<br><br>// Client<br>public class Program<br>{<br>    static void Main(string[] args)<br>    {<br>        Animal dog = new Dog();<br>        ICommand dogSoundCommand = new MakeSoundCommand(dog);<br>        AnimalSoundInvoker dogInvoker = new AnimalSoundInvoker(dogSoundCommand);<br>        dogInvoker.MakeSound();<br><br>        Animal cat = new Cat();<br>        ICommand catSoundCommand = new MakeSoundCommand(cat);<br>        AnimalSoundInvoker catInvoker = new AnimalSoundInvoker(catSoundCommand);<br>        catInvoker.MakeSound();<br>    }<br>}</pre><pre>// Output:<br>Woof!<br>Meow!</pre><p>Using abstract factories or interfaces can provide a way to define and extend behavior without modifying existing code, following OCP principles. Also OCP is often used in conjunction with DI to inject dependencies into a class makes it easier to extend behavior without modifying the class itself.</p><p>OCP is reduces the risk for new bugs or unintended behavioral changes in the existing code. Make code base to maintainable more easily and more adaptable for the changes. This principle introduced by Bertrand Meyer.</p><p><strong>Liskov Substitution Principle (LSP)</strong></p><p>This principle emphasizes that objects of a supper class should be replaceable with objects of a subclass without affecting the correctness of the program. So the derived class should extend the base class without changing its behavior. This principle promotes the design of class hierarchies that allow objects of derived classes to be used interchangeably with objects of the base class. This provides building flexible, extensible systems while, creating consistency, polymorphism and ease of maintenance code sources.</p><p>Why use LSP in software systems;</p><ul><li>Polymorphism: With use of LSP objects of a derived class to be treated as objects of the base class. This behavior promotes code flexibility and re-usability while enabling polymorphism.</li><li>Maintainability: New derived classes can be added without modifying existing code that relies on the base class. This way existing codes will remain unchanged so the risk of new bugs or unintended side effects will be minimized.</li><li>Consistency: Substituting derived class objects for base objects ensures a consistent and predictable behavior.</li></ul><p>LSP is very useful when designing class hierarchies where inheritance is used. It’s very important for the systems that heavily rely on polymorphism and interface-based programming. If code need to add new classes or modify existing ones in a way that violates substitutability of objects we need to use LSP to reduce the impact of the source code. LSP mostly used by strategy, state and template method patterns in the software systems.</p><ul><li>Strategy Pattern: LSP ensures that the subclass that implement the different algorithms adhere to the contract defined by the super-class or interface. This way, the client code may use any subclass without worrying any unexpected behavior or side effects. LSP enables strategy pattern to be more open for extension because new sub-classes can be added easily without modifying existing code and define clear and specific implementations for the algorithms.</li></ul><pre>using System;<br><br>// Define an interface for animals<br>public interface IAnimal<br>{<br>    // Define common methods for all animals<br>    void Eat();<br>    void Sleep();<br>    void MakeSound();<br>}<br><br>// Define concrete classes for animals<br>public class Dog : IAnimal<br>{<br>    // Define a strategy for dogs<br>    public void Eat()<br>    {<br>        Console.WriteLine(&quot;Dog eats&quot;);<br>    }<br><br>    public void Sleep()<br>    {<br>        Console.WriteLine(&quot;Dog sleeps&quot;);<br>    }<br><br>    public void Bark()<br>    {<br>        Console.WriteLine(&quot;Dog barks&quot;);<br>    }<br>}<br><br>public class Cat : IAnimal<br>{<br>    // Define a strategy for cats<br>    public void Eat()<br>    {<br>        Console.WriteLine(&quot;Cat eats&quot;);<br>    }<br><br>    public void Sleep()<br>    {<br>        Console.WriteLine(&quot;Cat sleeps&quot;);<br>        // Add some extra logic here if needed<br>        // For example, cat sleeps on the sofa<br>        Console.WriteLine(&quot;Cat sleeps on the sofa&quot;);<br>    }<br><br>    public void Meow()<br>    {<br>        Console.WriteLine(&quot;Cat meows&quot;);<br>    }<br>}<br><br>public class Bird : IAnimal<br>{<br>    // Define a strategy for birds<br>    public void Eat()<br>    {<br>        Console.WriteLine(&quot;Bird eats&quot;);<br>        // Add some extra logic here if needed<br>        // For example, bird eats seeds from the feeder<br>        Console.WriteLine(&quot;Bird eats seeds from the feeder&quot;);<br>     }<br>  <br>     public void Sleep()<br>     {<br>         Console.WriteLine(&quot;Bird sleeps&quot;);<br>         // Add some extra logic here if needed<br>         // For example, bird sleeps on a branch in the tree<br>         Console.WriteLine(&quot;Bird sleeps on a branch in the tree&quot;);<br>     }<br>  <br>     public void Sing()<br>     {<br>         Console.WriteLine(&quot;Bird sings&quot;);<br>     }<br>}<br><br>public class Program<br>{<br>    public static void Main(string[] args)<br>    {<br>      // Use polymorphism to create an array of animals and assign different strategies to each element<br>      <br>      // Create an array of animals with different strategies <br>      IAnimal[] animals = new IAnimal[3];<br>      <br>      // Assign different strategies to each element <br>      animals[0] = new Dog();<br>      animals[1] = new Cat();<br>      animals[2] = new Bird();<br>      <br>      // Use polymorphism to call common methods on each element <br>      foreach (IAnimal animal in animals)<br>      {<br>         animal.Eat(); // Call Eat method defined by IAnimal interface <br>         animal.Sleep(); // Call Sleep method defined by IAnimal interface <br>         animal.MakeSound(); // Call MakeSound method defined by IAnimal interface <br>      }<br>    }<br>}</pre><pre>// Output:<br>Dog eats<br>Dog sleeps<br>Dog barks<br><br>Cat eats<br>Cat sleeps on the sofa<br><br>Cat meows<br><br>Bird eats seeds from the feeder<br><br>Bird sleeps on a branch in the tree<br><br>Bird sings</pre><ul><li>Template Method Pattern: The template method pattern defines the skeleton of an algorithm in a super-class method and lets super-classes to override some steps of the algorithm without changing its structure. LSP usage with this pattern guarantees that the sub-classes override the steps of the algorithm to conform the contract defined by the super-class method. LSP is mostly used with this pattern to enable the polymorphism and flexibility of the pattern. The Template Method Pattern allows the client code to use any subclass object that implements the algorithm, and invoke the super-class method to execute it.</li></ul><pre>using System;<br><br>// This is an abstract class named Animal. An abstract class cannot be instantiated.<br>public abstract class Animal<br>{<br>    // This is a public method named DailyRoutine. This method calls the methods Eat, Sleep, and MakeSound.<br>    public void DailyRoutine()<br>    {<br>        Eat();<br>        Sleep();<br>        MakeSound();<br>    }<br><br>    // These are abstract methods that do not have a body. The classes that inherit from Animal must implement these methods.<br>    public abstract void Eat();<br>    public abstract void Sleep();<br>    public abstract void MakeSound();<br>}<br><br>// This is a class named Dog that inherits from the Animal class.<br>public class Dog : Animal<br>{<br>    // These methods override the abstract methods in the Animal class.<br>    public override void Eat()<br>    {<br>        Console.WriteLine(&quot;Dog eats&quot;);<br>    }<br><br>    public override void Sleep()<br>    {<br>        Console.WriteLine(&quot;Dog sleeps&quot;);<br>    }<br><br>    public override void MakeSound()<br>    {<br>        Console.WriteLine(&quot;Dog barks&quot;);<br>    }<br>}<br><br>// This is a class named Cat that inherits from the Animal class.<br>public class Cat : Animal<br>{<br>    // These methods override the abstract methods in the Animal class.<br>    public override void Eat()<br>    {<br>        Console.WriteLine(&quot;Cat eats&quot;);<br>    }<br><br>    public override void Sleep()<br>    {<br>        Console.WriteLine(&quot;Cat sleeps&quot;);<br>    }<br><br>    public override void MakeSound()<br>    {<br>        Console.WriteLine(&quot;Cat meows&quot;);<br>    }<br>}<br><br>// This is a class named Bird that inherits from the Animal class.<br>public class Bird : Animal<br>{<br>    // These methods override the abstract methods in the Animal class.<br>    public override void Eat()<br>    {<br>        Console.WriteLine(&quot;Bird eats&quot;);<br>    }<br><br>    public override void Sleep()<br>    {<br>        Console.WriteLine(&quot;Bird sleeps&quot;);<br>    }<br><br>    public override void MakeSound()<br>    {<br>        Console.WriteLine(&quot;Bird sings&quot;);<br>    }<br>}<br><br>public class Program<br>{<br>    public static void Main(string[] args)<br>    {<br>        // Here we are creating instances of Dog, Cat, and Bird classes.<br>        Animal dog = new Dog();<br>        Animal cat = new Cat();<br>        Animal bird = new Bird();<br><br>        // We are calling the DailyRoutine method for each instance.<br>        dog.DailyRoutine();<br>        cat.DailyRoutine();<br>        bird.DailyRoutine();<br><br>        Console.ReadKey();<br>    }<br>}</pre><pre>// Output:<br>Dog eats<br>Dog sleeps<br>Dog barks<br>Cat eats<br>Cat sleeps<br>Cat meows<br>Bird eats<br>Bird sleeps<br>Bird sings</pre><ul><li>State Pattern: The State Pattern encapsulates the state-dependent behavior in separate state classes that implement a common interface or inherit from a common super-class. LSP mostly used with the State Pattern because it ensures that the state classes implement state dependent behavior to their interface or super-class. LSP also help State Pattern to use polymorphism and flexibility in its implementation.</li></ul><pre>using System;<br><br>// This is an interface that represents the state of an animal.<br>public interface IAnimalState<br>{<br>    void Eat();<br>    void Sleep();<br>    void MakeSound();<br>}<br><br>// These are classes that implement the IAnimalState interface.<br>public class DogState : IAnimalState<br>{<br>    public void Eat()<br>    {<br>        Console.WriteLine(&quot;Dog eats&quot;);<br>    }<br><br>    public void Sleep()<br>    {<br>        Console.WriteLine(&quot;Dog sleeps&quot;);<br>    }<br><br>    public void MakeSound()<br>    {<br>        Console.WriteLine(&quot;Dog barks&quot;);<br>    }<br>}<br><br>public class CatState : IAnimalState<br>{<br>    public void Eat()<br>    {<br>        Console.WriteLine(&quot;Cat eats&quot;);<br>    }<br><br>    public void Sleep()<br>    {<br>        Console.WriteLine(&quot;Cat sleeps&quot;);<br>    }<br><br>    public void MakeSound()<br>    {<br>        Console.WriteLine(&quot;Cat meows&quot;);<br>    }<br>}<br><br>public class BirdState : IAnimalState<br>{<br>    public void Eat()<br>    {<br>        Console.WriteLine(&quot;Bird eats&quot;);<br>    }<br><br>    public void Sleep()<br>    {<br>        Console.WriteLine(&quot;Bird sleeps&quot;);<br>    }<br><br>    public void MakeSound()<br>    {<br>        Console.WriteLine(&quot;Bird sings&quot;);<br>    }<br>}<br><br>// This is a class named Animal that uses the state pattern.<br>public class Animal<br>{<br>    private IAnimalState _state;<br><br>    public Animal(IAnimalState state)<br>    {<br>        _state = state;<br>    }<br><br>    public void SetState(IAnimalState state)<br>    {<br>        _state = state;<br>    }<br><br>    public void DailyRoutine()<br>    {<br>        _state.Eat();<br>        _state.Sleep();<br>        _state.MakeSound();<br>    }<br>}<br><br>// This is the main program class.<br>public class Program<br>{<br>    public static void Main(string[] args)<br>    {<br>        // Here we are creating an instance of Animal and setting its state to DogState, CatState, and BirdState.<br>        Animal animal = new Animal(new DogState());<br>        animal.DailyRoutine();<br><br>        animal.SetState(new CatState());<br>        animal.DailyRoutine();<br><br>        animal.SetState(new BirdState());<br>        animal.DailyRoutine();<br><br>        Console.ReadKey();<br>    }<br>}</pre><pre>// Output:<br>Dog eats<br>Dog sleeps<br>Dog barks<br>Cat eats<br>Cat sleeps<br>Cat meows<br>Bird eats<br>Bird sleeps<br>Bird sings</pre><p>The Liskov Substitutin Principle also enables the contracts and interfaces and this formalize the expected behavior of classes. LSP also closely related to the principles of Design by Contract, which uses contracts to define the expected behavior of a class. LSP enables more consistent, maintainability codes while enabling polymorphism. With this principle it can be more easy to build flexible and extensible systems. This principle introduced by Barbara Liskov.</p><p>Interface Segregation Principle (ISP)</p><p>This principle states that a class should not be forced to implement interfaces it does not use. So client should not be forced to depend on interfaces it does not use. This principle promotes small and specific purposed classes that tailored to the needs of the clients. We use this principle to use</p><p>Why use ISP in software systems;</p><ul><li>Reduced Dependencies: ISP principle helps to archieve a high level od cohesion and low coupling in the design of software systems. Reducing dependency aims to minimize number of dependencies between different components or modules so with the help of ISP code become more easily changed, tested and reused.</li><li>Flexibility: Classes have the flecibility to depend only on the specific interfaces that are relevant to their use causes rather than monolithic interface.</li><li>Maintability: Smaller and more specific interfaces are easier to maintain without effecting unrelated parts of the system.</li></ul><p>ISP principles is about creating tailored to specific needs of the client so this small, specific and only neccessary mehods for the client is good for some design patterns. ISP is closely related to the use of interface-based programming. While there may not be specific design patterns associated with ISP, its principles are often applied when designing interfaces in various patterns, such as:</p><ul><li>Adapter pattern: ISP principle involve creating interfaces that need to be toilored for client purposes. This reduce the coupling between classes that have different interfaces. The adapter pattern allows you to transform an interface into another, ensuring compatibility between classes that could not work together. In the context of ISP, this pattern can be helpful when trying to transition from fat interfaces to more segregated ones without major code changes.</li></ul><pre>using System;<br><br>// Existing interface<br>public interface IAnimal<br>{<br>    void Eat();<br>    void Sleep();<br>}<br><br>// Existing class<br>public class Dog : IAnimal<br>{<br>    public void Eat()<br>    {<br>        Console.WriteLine(&quot;Dog eats&quot;);<br>    }<br><br>    public void Sleep()<br>    {<br>        Console.WriteLine(&quot;Dog sleeps&quot;);<br>    }<br><br>    public void Bark()<br>    {<br>        Console.WriteLine(&quot;Dog barks&quot;);<br>    }<br>}<br><br>// New interface<br>public interface IAnimalSound<br>{<br>    void MakeSound();<br>}<br><br>// Adapter class<br>public class DogAdapter : IAnimalSound<br>{<br>    private Dog _dog;<br><br>    public DogAdapter(Dog dog)<br>    {<br>        _dog = dog;<br>    }<br><br>    public void MakeSound()<br>    {<br>        _dog.Bark();<br>    }<br>}<br><br>// Client code<br>public class Program<br>{<br>    public static void Main(string[] args)<br>    {<br>        // Create an instance of Dog<br>        var dog = new Dog();<br><br>        // Use only the methods from IAnimal interface<br>        dog.Eat(); // Dog eats<br>        dog.Sleep(); // Dog sleeps<br><br>        // Use an adapter object to access Bark method from Dog class<br>        var dogAdapter = new DogAdapter(dog);<br>        dogAdapter.MakeSound(); // Dog barks<br><br>        Console.ReadKey();<br>    }<br>}</pre><pre>// Output:<br>Dog eats<br>Dog sleeps<br>Dog barks</pre><p>ISP helps reducing the coupling between classes and modules by allowing them to depend only on the specific interfaces. Also Clients have the flexibility to use only the methods they require this behaviour make extensions more adaptable to changes. Smaller codes are easier to maintain and modify they also more focused and their effect of the whole system will be limited.</p><p>Composing classes with interfaces that provide only the necessary functionality is often more flexible and adheres better to ISP than relying on a rigid inheritance hierarchy. When applying dependency injection, adherence to ISP allows injecting only the necessary dependencies, reducing unnecessary coupling. Designing interfaces and contracts in an object-oriented system. It’s especially important when working with large systems where maintaining a clear separation of concern is crucial.</p><p>In summary, the Interface Segregation Principle promotes the creation of small, focused interfaces that cater to the specific needs of clients. This reduces dependencies, increases flexibility, and enhances maintainability.</p><p><strong>Dependency Inversion Principle (DIP)</strong></p><p>The Dependency inversion principle is about inverting the traditional dependency flow in software design. DIP suggest that high-level modules (abstractions) should not depend on low-level modules (concrete implementations), but both should depend on abstractions.</p><p>Why use <strong>DIP </strong>in software systems;</p><ul><li>Reduced Coupling: DIP is used to reduce coupling by depending on abstractions rather than concrete impelementations. This make the code more flecible and less tightly coupled.</li><li>Ease of Maintenance: Inverting dependencies also allows for easier maintenance and modification because changes in lover level datails do not affect higher level abstractions.</li><li>Testability: DIP allows better isolation for the code that causes to more effective unit testing.</li></ul><p>Several design patterns are closely related to DIP because it provide mechanisms for implementing external sources, promise loose coupling and it’s ensures that abstractions are not tied to concrete implementations.</p><ul><li>Depencency Injection (DI): DI pattern is ofen used with DIP to allow dependencies to be injected into a class, rather than having the class construct them itself. This makes the code more flexible, easier to test, and better organized. It also reduces the coupling between classes, making the system more modular and easier to understand and maintain.</li></ul><pre>// High-level module<br>public interface IMovement<br>{<br>    void Move();<br>}<br><br>// Low-level modules<br>public class Walk : IMovement<br>{<br>    public void Move()<br>    {<br>        Console.WriteLine(&quot;The animal is walking.&quot;);<br>    }<br>}<br><br>public class Swim : IMovement<br>{<br>    public void Move()<br>    {<br>        Console.WriteLine(&quot;The animal is swimming.&quot;);<br>    }<br>}<br><br>public class Fly : IMovement<br>{<br>    public void Move()<br>    {<br>        Console.WriteLine(&quot;The animal is flying.&quot;);<br>    }<br>}<br><br>public class Animal<br>{<br>    private IMovement _movement;<br><br>    public Animal(IMovement movement)<br>    {<br>        _movement = movement;<br>    }<br><br>    public void PerformMovement()<br>    {<br>        _movement.Move();<br>    }<br>}<br><br>// Client<br>public class Program<br>{<br>    static void Main(string[] args)<br>    {<br>        IMovement walk = new Walk();<br>        Animal lion = new Animal(walk);<br>        lion.PerformMovement();<br><br>        IMovement swim = new Swim();<br>        Animal fish = new Animal(swim);<br>        fish.PerformMovement();<br><br>        IMovement fly = new Fly();<br>        Animal bird = new Animal(fly);<br>        bird.PerformMovement();<br>    }<br>}</pre><pre>// Output:<br>The animal is walking.<br>The animal is swimming.<br>The animal is flying.</pre><ul><li>Inversion of Control (IoC): IoC control of objects or portions of a program is transferred to a container or framework. It’s most often used in the context of object-oriented programming. By decoupling the execution of a task from implementation, it provides a way to decouple software modules. This makes the system more modular and easier to understand and maintain. IoC is often used with DIP to make it easier to give each class a single responsibility and to adhere to the DIP. It also makes the code more flexible, easier to test, and better organized. It reduces the coupling between classes, making the system more modular and easier to understand and maintain.</li></ul><pre>// High-level module<br>public interface IAnimal<br>{<br>    void Eat();<br>}<br><br>// Low-level module<br>public class Lion : IAnimal<br>{<br>    public void Eat()<br>    {<br>        Console.WriteLine(&quot;The lion is eating.&quot;);<br>    }<br>}<br><br>// Inversion of Control (IoC) Container<br>public class ZooKeeper<br>{<br>    private IAnimal _animal;<br><br>    public ZooKeeper(IAnimal animal)<br>    {<br>        _animal = animal;<br>    }<br><br>    public void FeedAnimal()<br>    {<br>        _animal.Eat();<br>    }<br>}<br><br>// Client<br>public class Program<br>{<br>    static void Main(string[] args)<br>    {<br>        IAnimal lion = new Lion();<br>        ZooKeeper zooKeeper = new ZooKeeper(lion);<br>        zooKeeper.FeedAnimal();<br>    }<br>}</pre><pre>// Output:<br>The lion is eating.</pre><p>DIP is also helpfull with aspect oriented programming it allows the modularization of cross-cutting concerns, promoting the separation of concerns and adhering to DIP. While not always recommended, the Service Locator pattern is sometimes used in conjunction with DIP. It involves a central registry that provides dependencies to other components.</p><p>DIP promotes flexibility by allowing the easy substitution of components or modules without affecting the overall structure of the system. Loosely coupled systems are easier to maintain because changes in one part of the system are less likely to impact other parts. Systems adhering to DIP are often more scalable because they can evolve and accommodate changes without major disruptions.</p><p>In summary, the Dependency Inversion Principle is a key guideline for writing modular, maintainable, and flexible software. Design patterns such as Dependency Injection and Inversion of Control provide practical ways to implement DIP in real-world applications. Adhering to DIP is crucial for building software systems that can adapt to changing requirements and are easier to test and maintain.</p><p><strong>MCQ</strong></p><p>Which SOLID principle states that a class should have only one reason to change?</p><p>A) Single Responsibility Principle (SRP)</p><p>B) Open/Closed Principle (OCP)</p><p>C) Liskov Substitution Principle (LSP)</p><p>D) Interface Segregation Principle (ISP)</p><p>E) Dependency Inversion Principle (DIP)</p><p>Solution: A) Single Responsibility Principle (SRP). The Single Responsibility Principle states that a class should have only one reason to change, meaning it should only have one responsibility. This helps in making the class more maintainable and understandable, as each class is focused on a specific task.</p><p>Which SOLID principle suggests that a class should be open for extension but closed for modification?</p><p>A) Single Responsibility Principle (SRP)</p><p>B) Open/Closed Principle (OCP)</p><p>C) Liskov Substitution Principle (LSP)</p><p>D) Interface Segregation Principle (ISP)</p><p>Solution: B) Open/Closed Principle (OCP). The Open/Closed Principle states that a class should be open for extension but closed for modification. This means that you should be able to extend a class’s behavior without modifying its source code. This is typically achieved through the use of interfaces and abstract classes, allowing for new functionality to be added through inheritance rather than modifying existing code.</p><p>Which SOLID principle emphasizes that objects of a superclass should be able to replace objects of a subclass without affecting the correctness of the program?</p><p>A) Single Responsibility Principle (SRP)</p><p>B) Open/Closed Principle (OCP)</p><p>C) Liskov Substitution Principle (LSP)</p><p>D) Interface Segregation Principle (ISP)</p><p>E) Dependency Inversion Principle (DIP)</p><p>Solution: C) Liskov Substitution Principle (LSP). The Liskov Substitution Principle states that objects of a superclass should be able to replace objects of a subclass without affecting the correctness of the program. In other words, if a class is a subclass of another class, it should be usable interchangeably with its superclass without introducing errors.</p><p>Which SOLID principle suggests that a class should not be forced to implement interfaces it does not use?</p><p>A) Single Responsibility Principle (SRP)</p><p>B) Open/Closed Principle (OCP)</p><p>C) Liskov Substitution Principle (LSP)</p><p>D) Interface Segregation Principle (ISP)</p><p>E) Dependency Inversion Principle (DIP)</p><p>Solution: D) Interface Segregation Principle (ISP). The Interface Segregation Principle suggests that a class should not be forced to implement interfaces it does not use. In other words, it promotes the idea that a class should not be burdened with implementing interfaces that contain methods irrelevant to its purpose. Instead, classes should have specific interfaces tailored to their needs.</p><p>Which SOLID principle advocates that high-level modules should not depend on low-level modules, but both should depend on abstractions?</p><p>A) Single Responsibility Principle (SRP)</p><p>B) Open/Closed Principle (OCP)</p><p>C) Liskov Substitution Principle (LSP)</p><p>D) Interface Segregation Principle (ISP)</p><p>E) Dependency Inversion Principle (DIP)</p><p>Solution: E) Dependency Inversion Principle (DIP). The Dependency Inversion Principle advocates that high-level modules should not depend on low-level modules, but both should depend on abstractions. In simpler terms, this principle encourages the use of interfaces or abstract classes to define the dependencies between high-level and low-level modules. This helps in creating a more flexible and maintainable system by decoupling the higher-level policy from the lower-level details.</p><p>According to the Single Responsibility Principle (SRP), a class should have only one reason to change. Which of the following is NOT a valid reason for a class to change?</p><p>A) Change in business rules</p><p>B) Change in the user interface</p><p>C) Change in the database schema</p><p>D) Change in the performance requirements</p><p>E) Change in the logging mechanism</p><p>Solution: E) Change in the logging mechanism. According to the Single Responsibility Principle (SRP), a class should have only one reason to change. Changes in logging mechanisms are typically considered orthogonal to the main responsibilities of a class. Therefore, handling changes in logging should not be a primary reason for a class to change based on SRP.</p><p>In the context of the Open/Closed Principle (OCP), what does it mean for a class to be “open for extension”?</p><p>A) All of its methods are marked as abstract.</p><p>B) It allows adding new methods without changing existing code.</p><p>C) It can be instantiated with a variety of parameters.</p><p>D) It implements an interface with multiple methods.</p><p>E) It can be extended with additional properties.</p><p>Solution: B) It allows adding new methods without changing existing code. In the context of the Open/Closed Principle (OCP), a class being “open for extension” means that you can add new functionality to it without modifying its existing code. This is typically achieved through techniques such as inheritance and interfaces.</p><p>According to the Liskov Substitution Principle (LSP), what should be preserved when substituting a derived class object for a base class object?</p><p>A) The number of methods</p><p>B) The order of method calls</p><p>C) The correctness of the program</p><p>D) The class hierarchy</p><p>E) The use of abstract classes</p><p>Solution: C) The correctness of the program. According to the Liskov Substitution Principle (LSP), when substituting a derived class object for a base class object, it is crucial to preserve the correctness of the program. This means that the derived class should behave in such a way that it does not break the expected behavior of the base class.</p><p>The Interface Segregation Principle (ISP) suggests that a class should not be forced to implement interfaces it does not use. Which design pattern is commonly associated with ISP?</p><p>A) Singleton Pattern</p><p>B) Factory Method Pattern</p><p>C) Adapter Pattern</p><p>D) Strategy Pattern</p><p>E) Command Pattern</p><p>Solution: D) Strategy Pattern. The Interface Segregation Principle (ISP) is often associated with the Strategy Pattern. In the Strategy Pattern, interfaces are defined for each algorithm or strategy, and a class can choose to implement only the strategies it needs. This aligns with the ISP’s idea that a class should not be forced to implement interfaces it does not use.</p><p>According to the Dependency Inversion Principle (DIP), which of the following statements is true?</p><p>A) High-level modules should depend on low-level modules.</p><p>B) Abstractions should depend on details.</p><p>C) Concrete classes should depend on abstract classes.</p><p>D) Details should depend on abstractions.</p><p>E) The database schema should depend on the business rules.</p><p>Solution: D) Details should depend on abstractions. According to the Dependency Inversion Principle (DIP), high-level modules should not depend on low-level modules, but both should depend on abstractions. This means that the high-level policies and abstractions should be defined first, and the low-level details should depend on these abstractions.</p><p>In the context of SOLID principles, which principle promotes designing classes in a way that they depend on abstractions rather than concrete implementations?</p><p>A) Single Responsibility Principle (SRP)</p><p>B) Open/Closed Principle (OCP)</p><p>C) Liskov Substitution Principle (LSP)</p><p>D) Interface Segregation Principle (ISP)</p><p>E) Dependency Inversion Principle (DIP)</p><p>Solution: E) Dependency Inversion Principle (DIP). The Dependency Inversion Principle promotes designing classes in a way that they depend on abstractions (interfaces or abstract classes) rather than concrete implementations. This helps in achieving flexibility and decoupling in the system.</p><p>Which SOLID principle suggests that a class should not have a method that uses only a subset of the parameters?</p><p>A) Single Responsibility Principle (SRP)</p><p>B) Open/Closed Principle (OCP)</p><p>C) Liskov Substitution Principle (LSP)</p><p>D) Interface Segregation Principle (ISP)</p><p>E) Dependency Inversion Principle (DIP)</p><p>Solution: A) Single Reponsibility Principle (SRP) The Single Responsibility Principle suggests that a class should have only one reason to change. If a class has a method that uses only a subset of the parameters, it may indicate that the class is handling multiple responsibilities. This violates SRP because a change in one aspect of the class (e.g., the method’s parameters) may necessitate a change in the class for reasons unrelated to its main responsibility.</p><p>In the context of the Open/Closed Principle (OCP), which design pattern is commonly used to achieve extensibility without modifying existing code?</p><p>A) Observer Pattern</p><p>B) Adapter Pattern</p><p>C) Decorator Pattern</p><p>D) Command Pattern</p><p>E) Strategy Pattern</p><p>Solution: C) Decorator Pattern. . The Decorator Pattern is commonly used in the context of the Open/Closed Principle (OCP) to achieve extensibility without modifying existing code. It allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.</p><p>Which SOLID principle is most closely associated with the idea that a class should be designed with a specific, well-defined purpose and not take on additional responsibilities?</p><p>A) Single Responsibility Principle (SRP)</p><p>B) Open/Closed Principle (OCP)</p><p>C) Liskov Substitution Principle (LSP)</p><p>D) Interface Segregation Principle (ISP)</p><p>E) Dependency Inversion Principle (DIP)</p><p>Solution: A) Single Responsibility Principle (SRP). The Single Responsibility Principle suggests that a class should have only one reason to change and should be designed with a specific, well-defined purpose. By adhering to SRP, a class is less likely to take on additional responsibilities, making the code more maintainable and modular.</p><p>Which SOLID principle is violated if a subclass introduces a method that is not present in the superclass?</p><p>A) Single Responsibility Principle (SRP)</p><p>B) Open/Closed Principle (OCP)</p><p>C) Liskov Substitution Principle (LSP)</p><p>D) Interface Segregation Principle (ISP)</p><p>E) Dependency Inversion Principle (DIP)</p><p>Solution: C) Liskov Substitution Principle (LSP). The Liskov Substitution Principle states that objects of a superclass should be able to replace objects of a subclass without affecting the correctness of the program. Introducing a method in a subclass that is not present in the superclass can break this principle, as clients expecting objects of the superclass may not be able to correctly substitute objects of the subclass.</p><p>In the context of the Interface Segregation Principle (ISP), what term is commonly used to describe a large interface that contains more methods than a class implementing the interface needs?</p><p>A) Bloated Interface</p><p>B) Monolithic Interface</p><p>C) Comprehensive Interface</p><p>D) Massive Interface</p><p>E) Complete Interface</p><p>Solution: D) Massive Interface. In the context of the Interface Segregation Principle (ISP), a massive interface refers to an interface that contains more methods than a class implementing the interface needs. This situation violates the ISP, as classes are forced to implement methods they don’t use.</p><p>According to the Dependency Inversion Principle (DIP), which of the following statements is true regarding high-level modules and low-level modules?</p><p>A) High-level modules should depend on low-level modules.</p><p>B) Low-level modules should depend on high-level modules.</p><p>C) Both high-level and low-level modules should depend on abstractions.</p><p>D) Neither high-level nor low-level modules should depend on abstractions.</p><p>E) High-level modules should only depend on abstractions.</p><p>Solution: E) High-level modules should only depend on abstractions. According to the Dependency Inversion Principle (DIP), high-level modules should not depend on low-level modules but should depend on abstractions (interfaces or abstract classes). This promotes flexibility and decoupling in the system.</p><p>Which design pattern is often associated with solving the problem of having multiple algorithms and allowing the client to choose from them dynamically?</p><p>A) Observer Pattern</p><p>B) Strategy Pattern</p><p>C) Factory Method Pattern</p><p>D) Decorator Pattern</p><p>E) Proxy Pattern</p><p>Solution: B) Strategy Pattern. The Strategy Pattern is commonly associated with solving the problem of having multiple algorithms and allowing the client to choose from them dynamically. In this pattern, each algorithm is encapsulated in a separate class, and the client can switch between them at runtime.</p><p>According to the Single Responsibility Principle (SRP), what is the primary reason for a class to change?</p><p>A) Change in the user interface</p><p>B) Change in the business rules</p><p>C) Change in the database schema</p><p>D) Change in the logging mechanism</p><p>E) Change in the performance requirements</p><p>Solution: B) Change in the business rules. According to the Single Responsibility Principle (SRP), a class should have only one reason to change. In the context of SRP, the primary reason for a class to change is a change in the business rules. This principle helps in keeping classes focused on specific responsibilities.</p><p>Which SOLID principle suggests that a class should be open for extension but closed for modification, and is often associated with the use of abstract classes and interfaces?</p><p>A) Single Responsibility Principle (SRP)</p><p>B) Open/Closed Principle (OCP)</p><p>C) Liskov Substitution Principle (LSP)</p><p>D) Interface Segregation Principle (ISP)</p><p>E) Dependency Inversion Principle (DIP)</p><p>Solution: B) Open/Closed Principle (OCP). The Open/Closed Principle suggests that a class should be open for extension but closed for modification. This means that the behavior of a class can be extended without altering its source code. Abstract classes and interfaces are commonly used to implement the Open/Closed Principle by providing a basis for extension through inheritance.</p><p>In the context of SOLID principles, which principle emphasizes that the client should not be forced to depend on interfaces it does not use?</p><p>A) Single Responsibility Principle (SRP)</p><p>B) Open/Closed Principle (OCP)</p><p>C) Liskov Substitution Principle (LSP)</p><p>D) Interface Segregation Principle (ISP)</p><p>E) Dependency Inversion Principle (DIP)</p><p>Solution: D) Interface Segregation Principle (ISP). The Interface Segregation Principle emphasizes that the client should not be forced to depend on interfaces it does not use. This principle encourages the creation of smaller, specific interfaces tailored to the needs of the clients, preventing them from being burdened with unnecessary methods.</p><p>According to the Liskov Substitution Principle (LSP), what is the fundamental idea that should be preserved when substituting a derived class for a base class?</p><p>A) Method names should match between the base and derived classes.</p><p>B) The derived class should have additional methods compared to the base class.</p><p>C) The derived class should have fewer methods compared to the base class.</p><p>D) The correctness of the program should be maintained.</p><p>E) The base class should be marked as final.</p><p>Solution: D) The correctness of the program should be maintained. According to the Liskov Substitution Principle (LSP), when substituting a derived class for a base class, it is essential to preserve the correctness of the program. This principle ensures that the derived class can be used interchangeably with the base class without introducing errors.</p><p>According to the Dependency Inversion Principle (DIP), what is the primary benefit of depending on abstractions rather than concrete implementations?</p><p>A) Increased performance</p><p>B) Greater code simplicity</p><p>C) Improved flexibility and maintainability</p><p>D) Reduced development time</p><p>E) Enhanced security</p><p>Solution: C) Improved flexibility and maintainability. According to the Dependency Inversion Principle (DIP), depending on abstractions (interfaces or abstract classes) rather than concrete implementations provides improved flexibility and maintainability. This approach allows for easier changes and updates to the system without affecting high-level modules.</p><p>In the context of SOLID principles, which principle suggests that a class should have only one reason to change, and is often associated with the idea that a class should have a single, well-defined responsibility?</p><p>A) Single Responsibility Principle (SRP)</p><p>B) Open/Closed Principle (OCP)</p><p>C) Liskov Substitution Principle (LSP)</p><p>D) Interface Segregation Principle (ISP)</p><p>E) Dependency Inversion Principle (DIP)</p><p>Solution: A) Single Responsibility Principle (SRP). The Single Responsibility Principle suggests that a class should have only one reason to change and should have a single, well-defined responsibility. It aims to keep classes focused and less prone to changes due to multiple reasons.</p><p>Which design pattern is often associated with creating an interface for creating families of related or dependent objects without specifying their concrete classes?</p><p>A) Singleton Pattern</p><p>B) Abstract Factory Pattern</p><p>C) Observer Pattern</p><p>D) Builder Pattern</p><p>E) Proxy Pattern</p><p>Solution: B) Abstract Factory Pattern. The Abstract Factory Pattern is associated with creating an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fecc2ab8b815" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debugging C# csx files with VSCode]]></title>
            <link>https://medium.com/@erenokur.eo/debugging-c-csx-files-with-vscode-7a63c2de9b20?source=rss-4d75a52a76da------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a63c2de9b20</guid>
            <dc:creator><![CDATA[Eren OKUR]]></dc:creator>
            <pubDate>Sat, 09 Dec 2023 17:36:31 GMT</pubDate>
            <atom:updated>2023-12-09T17:36:31.144Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GFybufbAeEWDMfLJqhEIeQ.jpeg" /><figcaption>Image by sentavio on Freepik</figcaption></figure><p>In C#, we can can create files with csx extension and run those scripts in our terminal. We can also debug those files with VSCode which is very faster than creating a new console application.</p><p>To make this debugging operations you need to be sure .Net is installed on your computer. To install .Net following guide is very easy <a href="https://dotnet.microsoft.com/en-us/download">https://dotnet.microsoft.com/en-us/download</a>.</p><p>If .Net is already installed than dotnet-script tool also need to be installed. This is very straight forward process because only one terminal command is enough.</p><pre>dotnet tool install -g dotnet-script</pre><p>After this installation finished, simple test application can be written with a file named test.csx.</p><pre>Console.WriteLine(&quot;Hello World, this is a .NET script!&quot;);</pre><p>after code saved we need to open a terminal in the same directory with the file. And write following code to the terminal and press enter.</p><pre>dotnet script ./test.csx</pre><p>This code will write “Hello World, this is a .NET script!” in to the terminal. After this worked VSCode debug settings need to be created to do this new launch.json file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/405/1*N7Aai9c-1p80gfKV4yDVIQ.jpeg" /><figcaption>to create launch.json file click to the button.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*i6lmpo7WgFRGNGn9Em40xg.jpeg" /><figcaption>Choice .Net 5+ and .Net Core.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*874xrQThAG2AZvD1vQ6kiQ.jpeg" /><figcaption>When first launch created, this file will look like this.</figcaption></figure><p>In the newly created launch.json file, we need to set ‘coreclr’ for the Configurations section for dotnet and provide the path to the newly installed dotnet-script executable.</p><p>For Windows, the dotnet-script.exe file should be located under %USERPROFILE%/.dotnet/tools. After finding the executable path, we edit our launch file as follows.</p><pre>{<br>  // Use IntelliSense to learn about possible attributes.<br>  // Hover to view descriptions of existing attributes.<br>  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387<br>  &quot;version&quot;: &quot;0.2.0&quot;,<br>  &quot;configurations&quot;: [<br>    {<br>      &quot;name&quot;: &quot;Launch&quot;,<br>      &quot;type&quot;: &quot;coreclr&quot;,<br>      &quot;request&quot;: &quot;launch&quot;,<br>      &quot;program&quot;: &quot;[dotnet-script.exe nin yolu]&quot;,<br>      &quot;args&quot;: [&quot;test.csx&quot;],<br>      &quot;cwd&quot;: &quot;${workspaceRoot}&quot;,<br>      &quot;preLaunchTask&quot;: &quot;&quot;,<br>      &quot;runtimeExecutable&quot;: null,<br>      &quot;env&quot;: {},<br>      &quot;externalConsole&quot;: false<br>    }<br>  ]<br>}</pre><p>After saving the prepared launch.json file, we return to our test.csx file. We can start debugging by placing a breakpoint on the first line of our code and pressing the F5 key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lol5H4U_Ov8Dde3nIqab4A.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a63c2de9b20" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[C# csx dosyalarını VSCode ile debug yapmak]]></title>
            <link>https://medium.com/@erenokur.eo/c-csx-dosyalar%C4%B1n%C4%B1-vscode-ile-debug-yapmak-64edf8628009?source=rss-4d75a52a76da------2</link>
            <guid isPermaLink="false">https://medium.com/p/64edf8628009</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[vscode]]></category>
            <dc:creator><![CDATA[Eren OKUR]]></dc:creator>
            <pubDate>Sat, 18 Nov 2023 10:56:54 GMT</pubDate>
            <atom:updated>2023-11-18T19:13:43.008Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LnihSwpiKA6gwjx-bhlCEw.jpeg" /><figcaption>Image by sentavio on Freepik</figcaption></figure><p>C# consol üzerinden indirilen dotnet-script aracı ile csx uzantılı dosyalar yaratarak bunları VSCode üzerinden debug yapabiliriz.</p><p>Bu işlem için öncelikle bilgisayarınızda .NET yüklü olduğuna emin olmanız gerekemektedir. Yükleme işlemleri için <a href="https://dotnet.microsoft.com/en-us/download">https://dotnet.microsoft.com/en-us/download</a> adresindeki işlemler takip edilebilir.</p><p>.NET yüklü ise öncelikle dotnet dotnet-script tool indirmemiz gerekiyor. bu işlemi terminal üzerinden aşağıdaki komutu girerek yapabilirsiniz.</p><pre>dotnet tool install -g dotnet-script</pre><blockquote>dotnet script indirildikten sonra küçük bir test yapmak için <strong>test.csx</strong> dosyası yaratarak içine aşağıdaki gibi bir kod bloğu ekleyelim.</blockquote><pre>Console.WriteLine(&quot;Hello World, this is a .NET script!&quot;);</pre><p>dosyayı kaydettikten sonra ilk önce test amaçlı VSCode terminali üzerinden scriptin doğru çalıştığına emin olalım. Bu işlem için terminale şu komutu verelim.</p><pre>dotnet script ./test.csx</pre><p>Bu kod ile terminalde “Hello World, this is a .NET script!” yazısını görüyor olmanız gerekiyor.</p><p>Kurulum işlemleri tamamlandıktan sonra VSCode üzerinde debug kısmına gelerek yeni bir launch.json dosyası üreteceğiz.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/405/1*5-tmcfPzC54wDCM6S6cYSQ.png" /><figcaption>create a launch.json butonuna tıklayın.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*yLopxZSbVPrTUpwzI27jTA.png" /><figcaption>.Net 5+ and .Net Core seçeneğini seçin.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*NRKoovI-Ktl7KuVGjdE1sQ.png" /><figcaption>Launch dosyası ilk açıldığında bu şekilde görülüyor olacaktır.</figcaption></figure><p>Yeni oluşturduğumuz launch.json dosyasındaki Configurations kızmına dotnet için coreclr yazmamız ve yeni yüklediğimiz dotnet-script exesinin yolunu vermemiz gerekiyor.</p><p>Windows için dotnet-script. exe dosyarı %USERPROFILE%/.dotnet/tools altında olması gerekiyor. Exe yolunu bulduktan sonra launch dosyamızı şu şekilde edit ediyoruz.</p><pre>{<br>  // Use IntelliSense to learn about possible attributes.<br>  // Hover to view descriptions of existing attributes.<br>  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387<br>  &quot;version&quot;: &quot;0.2.0&quot;,<br>  &quot;configurations&quot;: [<br>    {<br>      &quot;name&quot;: &quot;Launch&quot;,<br>      &quot;type&quot;: &quot;coreclr&quot;,<br>      &quot;request&quot;: &quot;launch&quot;,<br>      &quot;program&quot;: &quot;[dotnet-script.exe nin yolu]&quot;,<br>      &quot;args&quot;: [&quot;test.csx&quot;],<br>      &quot;cwd&quot;: &quot;${workspaceRoot}&quot;,<br>      &quot;preLaunchTask&quot;: &quot;&quot;,<br>      &quot;runtimeExecutable&quot;: null,<br>      &quot;env&quot;: {},<br>      &quot;externalConsole&quot;: false<br>    }<br>  ]<br>}</pre><p>Bu şekilde hazırladığımız launch.json dosyasını kaydedip test.csx dosyamıza geri dönüyoruz. Kodumuzun ilk satırına break point koyarak F5 tuşuna basarak debug işlemlerine başlayabiliriz.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y7ILhZJakNfAvsNvW0SGXw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64edf8628009" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>