C# design pattern — generator pattern (builder pattern), adapter pattern

Time:2021-12-26

1. Generator mode

We know that the factory pattern can return one of several different subclasses according to the passed to the construction method. Suppose we need not only an algorithm for calculation, but also different interfaces according to the data displayed, We know how to use the builder pattern. The builder pattern separates the construction of a complex object from its representation, so that the same construction process can create different representations. Let’s look at it with a chestnut. Or make a car.

/// 
///Manufacturing steps
/// 
public abstract class Builder
{
    /// 
    ///Make wheels
    /// 
    public abstract void MakeWheels();
    /// 
    ///Assemble
    /// 
    public abstract void Assemble();
    /// 
    ///Painting
    /// 
    public abstract void Painting();
}

Then write a class that makes big cars and small cars

/// 
///Build a cart
/// 
public class BigBuilder : Builder
{
    public override void Assemble()
    {
        Console. Writeline ("crane assembly completed");
    }

    public override void MakeWheels()
    {
        Console. Writeline ("the cart wheels are built!");
    }

    public override void Painting()
    {
        Console. Writeline ("crane painting completed!");
    }
}
/// 
///Car making
/// 
public class SmallBuilder : Builder
{
    public override void Assemble()
    {
        Console. Writeline ("trolley assembly completed");
    }

    public override void MakeWheels()
    {
        Console. Writeline ("trolley wheels are built!");
    }

    public override void Painting()
    {
        Console. Writeline ("trolley painting completed!");
    }
}

A factory built car is added at the back. It uses builder to create different kinds of cars

/// 
///Car factory
/// 
public class CarFactory
{
    public Builder Builder { get; set; }
    public CarFactory(Builder builder)
    {
        Builder=builder;
    }
    /// 
    ///Build a car
    /// 
    public void CreateCar()
    {
        Builder. MakeWheels();// wheel
        Builder. Assemble();// assemble
        Builder. Painting();// Painting
    }
    

}

//Client call
CarFactory cf = new(new BigBuilder());
cf.CreateCar();
cf.Builder = new SmallBuilder();
cf.CreateCar();

So you can see the benefits of the generator pattern:

Readers are allowed to change the internal representation of the product, and colleagues also hide the problem of how to install the product;

Each specific generator is independent of other generators, and colleagues are independent of other parts of the program. Therefore, it can accurately control the results of generator construction;

The pattern of the generator is similar to the abstract factory pattern, both of which return the same method class composed of two objects. The main difference between them is that the abstract factory returns a series of related classes one by one, while the generator builds a complex object step by step according to the data it provides.

Disadvantages of builder mode:

When there are many kinds of components, you need to create many specific builder classes.

2. Adapter mode

Adapter mode (adapter pattern) it can convert a programming interface to another interface. When we want unrelated classes to work together in the same program, we can use the adapter pattern. First, inheritance can be implemented. Derive a new class from an inconsistent class and add the required methods to make the derived class meet the required interface; second, object composition, Is to include the original class in the new class, and then create method conversion calls in the new class. These two methods are collectively referred to as class adapters and object adapters. It’s a bit like pointing a deer as a horse (that’s what happens in the program). There are three roles here.

Initial role: the function we need, but the interface does not match;

Target role: he represents the interface required by the customer

The adapter feels that the target interface is implemented

For example, I want to charge money for my Apple phone, but I only have an Android charging cable. At this time, do we need to find an adapter to charge it.

/// 
///Android charging
/// 
public class Android
{
    public void AndroidCharge()
    {
        Console. Writeline ("Android charging");
    }
}
/// 
///Apple interface charging
/// 
public interface IApple
{
    /// 
    ///Target method
    /// 
    void AppleChage();
}
public class AppleAdater : IApple
{
    Android ad = new Android();
    /// 
    ///Evaluating charging is actually calling Android's charging method
    /// 
    public void AppleChage()
    {
        ad.AndroidCharge();
    }
}

//Client call
IApple app = new AppleAdater();
app.AppleChage();

Advantages of adapter mode:

1. Improve code reuse and reuse the methods in Adaptee

2. flexibility is good, if there are no associated two classes A and class B, class A wants to borrow the method of class B, you can add an instance of class B in class A, and then call the method of class B instance.

Disadvantages of adapter mode:

1. Using too many adapters will make the system messy. For example, we explicitly call the function of interface a, but it is adapted to the function of interface B.

 

PS: everyone should regard his every day as a self creation, honestly reflect himself in this creation, and regard this day as an unrepeatable day, and live this day with an unrepeatable mood.