<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hicham</title>
    <description>The latest articles on DEV Community by Hicham (@buildwithhicham).</description>
    <link>https://dev.to/buildwithhicham</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3280858%2F3c5876c7-1592-4196-9147-4edc5f702751.png</url>
      <title>DEV Community: Hicham</title>
      <link>https://dev.to/buildwithhicham</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/buildwithhicham"/>
    <language>en</language>
    <item>
      <title>Strategy Pattern</title>
      <dc:creator>Hicham</dc:creator>
      <pubDate>Mon, 11 Aug 2025 13:29:59 +0000</pubDate>
      <link>https://dev.to/buildwithhicham/strategy-pattern-1dpm</link>
      <guid>https://dev.to/buildwithhicham/strategy-pattern-1dpm</guid>
      <description>&lt;p&gt;Strategy design pattern is one of the behavioral design pattern. Strategy pattern is used when we have multiple algorithm for a specific task and client decides the actual implementation to be used at runtime&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Strategy interface
interface PaymentStrategy {
    void pay(double amount);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;// Concrete strategies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CreditCardPayment implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("Paid $" + amount + " using Credit Card.");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class PayPalPayment implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("Paid $" + amount + " using PayPal.");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CashPayment implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("Paid $" + amount + " in cash.");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;// Context&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Checkout {
    private PaymentStrategy paymentStrategy;

    public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    public void processOrder(double amount) {
        if (paymentStrategy == null) {
            throw new IllegalStateException("Payment strategy not set");
        }
        paymentStrategy.pay(amount);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;// Client&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main {
    public static void main(String[] args) {
        Checkout checkout = new Checkout();

        checkout.setPaymentStrategy(new CreditCardPayment());
        checkout.processOrder(25.50);

        checkout.setPaymentStrategy(new PayPalPayment());
        checkout.processOrder(13.75);

        checkout.setPaymentStrategy(new CashPayment());
        checkout.processOrder(7.00);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output will be :&lt;/p&gt;

&lt;p&gt;Paid $25.5 using Credit Card.&lt;br&gt;
Paid $13.75 using PayPal.&lt;br&gt;
Paid $7.0 in cash.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Factory Method vs Abstract Factory — What's the Difference?</title>
      <dc:creator>Hicham</dc:creator>
      <pubDate>Sun, 10 Aug 2025 17:20:36 +0000</pubDate>
      <link>https://dev.to/buildwithhicham/factory-method-vs-abstract-factory-whats-the-difference-glb</link>
      <guid>https://dev.to/buildwithhicham/factory-method-vs-abstract-factory-whats-the-difference-glb</guid>
      <description>&lt;p&gt;Factory Method and Abstract Factory are two common ways to handle object creation. They sound similar, but their purpose and structure are a bit different.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Factory Method Pattern&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Idea: A single factory class decides which object to create, often based on input like a String or enum.&lt;/p&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Simple and centralized.

Good for small product sets.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Adding new products requires modifying the factory (more if/else).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Coffee Shop Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CoffeeFactory has makeCoffee(String type).

You pass "latte" or "espresso", and it returns the right coffee object.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;// Coffee interface&lt;br&gt;
public interface Coffee {&lt;br&gt;
    void prepare();&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Concrete coffee types&lt;br&gt;
public class Espresso implements Coffee {&lt;br&gt;
    &lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt;&lt;br&gt;
    public void prepare() {&lt;br&gt;
        System.out.println("Grinding beans, brewing strong espresso...");&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;public class Latte implements Coffee {&lt;br&gt;
    &lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt;&lt;br&gt;
    public void prepare() {&lt;br&gt;
        System.out.println("Brewing coffee, adding steamed milk...");&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Factory Method&lt;br&gt;
public class CoffeeFactory {&lt;br&gt;
    public Coffee makeCoffee(String type) {&lt;br&gt;
        if (type.equalsIgnoreCase("espresso")) {&lt;br&gt;
            return new Espresso();&lt;br&gt;
        } else if (type.equalsIgnoreCase("latte")) {&lt;br&gt;
            return new Latte();&lt;br&gt;
        } else {&lt;br&gt;
            throw new IllegalArgumentException("Unknown coffee type: " + type);&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Client&lt;br&gt;
public class Main {&lt;br&gt;
    public static void main(String[] args) {&lt;br&gt;
        CoffeeFactory factory = new CoffeeFactory();&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Coffee coffee1 = factory.makeCoffee("espresso");
    coffee1.prepare();

    Coffee coffee2 = factory.makeCoffee("latte");
    coffee2.prepare();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Abstract Factory Pattern&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Idea:&lt;br&gt;
Instead of one factory with conditionals, you have an abstract factory interface with multiple concrete factories, each producing one specific type or a family of related products.&lt;/p&gt;

&lt;p&gt;The client chooses which factory to use — no if/else needed inside the factory.&lt;/p&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;No big conditional logic.

Easy to add new product types by creating a new factory.

Supports creating families of related products (e.g., coffee + tea sets).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;More classes to maintain.

May be overkill for small projects.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Coffee Shop Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EspressoFactory always makes Espresso.

LatteFactory always makes Latte.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;// Coffee interface&lt;br&gt;
public interface Coffee {&lt;br&gt;
    void prepare();&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Concrete coffee types&lt;br&gt;
public class Espresso implements Coffee {&lt;br&gt;
    &lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt;&lt;br&gt;
    public void prepare() {&lt;br&gt;
        System.out.println("Grinding beans, brewing strong espresso...");&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;public class Latte implements Coffee {&lt;br&gt;
    &lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt;&lt;br&gt;
    public void prepare() {&lt;br&gt;
        System.out.println("Brewing coffee, adding steamed milk...");&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Abstract factory&lt;br&gt;
public abstract class CoffeeFactory {&lt;br&gt;
    public abstract Coffee createCoffee();&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Concrete factories&lt;br&gt;
public class EspressoFactory extends CoffeeFactory {&lt;br&gt;
    &lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt;&lt;br&gt;
    public Coffee createCoffee() {&lt;br&gt;
        return new Espresso();&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;public class LatteFactory extends CoffeeFactory {&lt;br&gt;
    &lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt;&lt;br&gt;
    public Coffee createCoffee() {&lt;br&gt;
        return new Latte();&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Client&lt;br&gt;
public class Main {&lt;br&gt;
    public static void main(String[] args) {&lt;br&gt;
        CoffeeFactory espressoFactory = new EspressoFactory();&lt;br&gt;
        Coffee espresso = espressoFactory.createCoffee();&lt;br&gt;
        espresso.prepare();&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    CoffeeFactory latteFactory = new LatteFactory();
    Coffee latte = latteFactory.createCoffee();
    latte.prepare();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
