0% found this document useful (0 votes)
10 views29 pages

C# Concepts 2

Uploaded by

musabinshabeer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views29 pages

C# Concepts 2

Uploaded by

musabinshabeer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

C# Concepts

Abstract Factory vs. Simple Factory Design Patterns


C#: Factory Patterns

• Factory patterns are creational design patterns that


deal with object creation mechanisms, aiming to
create objects without specifying the exact class of
the object that will be created.
C#: Simple Factory Pattern

• Definition: Not officially classified as one of the Gang


of Four (GoF) design patterns, the Simple Factory
pattern provides a static method to create instances of
different classes based on provided parameters.

• Purpose: To centralize object creation logic, making it


easier to manage and extend.
Simple Factory Pattern:
Characteristics

• Single Responsibility: A dedicated class


handles the creation of objects.
• Static Method: Often implemented using static
methods.
• Limited Flexibility: Can become cumbersome
with many product types.
Simple Factory Pattern - Example

Scenario: Creating different types of notifications.

// Product Interface
public interface INotification
{
void Send(string message);
}
Simple Factory Pattern - Example
// Concrete Products
public class EmailNotification : INotification
{
public void Send(string message)
{
Console.WriteLine($"Email sent:
{message}");
}
}
Simple Factory Pattern - Example

public class SMSNotification : INotification


{
public void Send(string message)
{
Console.WriteLine($"SMS sent:
{message}");
}
}
Simple Factory Pattern - Example
// Simple Factory
public static class NotificationFactory
{
public static INotification CreateNotification(string type)
{
return type switch
{
"Email" => new EmailNotification(),
"SMS" => new SMSNotification(),
_ => throw new ArgumentException("Invalid notification
type")
};
}
}
Simple Factory Pattern - Example
// Usage
class Program
{
static void Main(string[] args)
{
var email =
NotificationFactory.CreateNotification("Email");
email.Send("Hello via Email!");

var sms = NotificationFactory.CreateNotification("SMS");


sms.Send("Hello via SMS!");
}
}
Simple Factory Pattern - Example
Simple Factory Pattern - Advantages

• Centralized Creation Logic: All object creation


logic is in one place.

• Ease of Use: Simplifies client code by abstracting


object creation.
Simple Factory Pattern -
Disadvantages

• Scalability Issues: Adding new product types


requires modifying the factory method.

• Not Polymorphic: The factory can't be extended


through inheritance.
C#: Abstract Factory Pattern

• Definition: An Abstract Factory provides an interface


for creating families of related or dependent objects
without specifying their concrete classes.

• Purpose: To create groups of related objects, ensuring


that products from one family are compatible with
products from another family.
Abstract Factory Pattern:
Characteristics
• Multiple Factories: Allows for multiple factory
implementations, each creating a different family of
products.

• Polymorphic Creation: Factories can be extended


through inheritance.

• Enhanced Flexibility: Easily add new product families


without altering existing code.
Abstract Factory Pattern - Example
Scenario: Creating UI components for different
operating systems (Windows and macOS).

// Abstract Products
public interface IButton
{
void Render();
}
Abstract Factory Pattern - Example
// Concrete Products for Windows
public class WindowsButton : IButton
{
public void Render()
{
Console.WriteLine("Rendering
Windows Button");
}
}
Abstract Factory Pattern - Example
public class WindowsCheckbox : ICheckbox
{
public void Render()
{
Console.WriteLine("Rendering
Windows Checkbox");
}
}
Abstract Factory Pattern - Example
// Concrete Products for macOS
public class MacButton : IButton
{
public void Render()
{
Console.WriteLine("Rendering
Mac Button");
}
}
Abstract Factory Pattern - Example

public class MacCheckbox : ICheckbox


{
public void Render()
{
Console.WriteLine("Rendering
Mac Checkbox");
}
}
Abstract Factory Pattern - Example

// Abstract Factory
public interface IGUIFactory
{
IButton CreateButton();
ICheckbox CreateCheckbox();
}
Abstract Factory Pattern - Example
// Concrete Factories
public class WindowsFactory : IGUIFactory
{
public IButton CreateButton()
{
return new WindowsButton();
}

public ICheckbox CreateCheckbox()


{
return new WindowsCheckbox();
}
Abstract Factory Pattern - Example
public class MacFactory : IGUIFactory
{
public IButton CreateButton()
{
return new MacButton();
}

public ICheckbox CreateCheckbox()


{
return new MacCheckbox();
}
}
Abstract Factory Pattern - Example
// Client
public class Application
{
private readonly IButton _button;
private readonly ICheckbox _checkbox;

public Application(IGUIFactory factory)


{
_button = factory.CreateButton();
_checkbox = factory.CreateCheckbox();
Abstract Factory Pattern - Example
public void RenderUI()
{
_button.Render();
_checkbox.Render();
}
}
Abstract Factory Pattern - Example
// Usage
class Program
{
static void Main(string[] args)
{
IGUIFactory factory;

// Suppose we detect the OS is


Windows
factory = new WindowsFactory();
Abstract Factory Pattern - Example
var app = new Application(factory);
app.RenderUI();
// Output:
// Rendering Windows Button
// Rendering Windows Checkbox
// Alternatively, for macOS
factory = new MacFactory();
app = new Application(factory);
app.RenderUI();
// Output:
// Rendering Mac Button
// Rendering Mac Checkbox
}
}
Abstract Factory Pattern - Example
Abstract Factory Pattern -
Advantages
• Consistency Among Products: Ensures that products
created by the factory are compatible.

• Extensibility: Adding new product families involves


creating new factory implementations without modifying
existing code.

• Decoupled Code: Clients depend on abstractions


rather than concrete classes.
Abstract Factory Pattern -
Disadvantages

• Complexity: More classes and interfaces to


manage.

• Inflexibility with Single Products: Not ideal if


you need to create unrelated products.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy