简体   繁体   中英

3 Layers architecture in vending machine, while keeping OCP

I'm working on a vending machine project, and i tried to split it into UI and BL layers, But I'm getting into problem. For example I have this function for paying in coins, which derives from an abstract class:

public override void Pay(decimal amount)
{
    while (currentCoins < amount)
    {
        // Print instructions
        // Get input

        if (Valid)
        {
            // logic
        }
        else
        {
            // Print error
        }
    }
}   

So the problem is that i dont have access to the UI inside the BL, but i need continuous communication with the UI. I thought of making function for each payment method in the UI, but it violates OCP...

I'm looking for an elegant solution ideas that won't break SOLID principles.

Is there any desgin pattern or something that can solve my problem? (strategy pattern?) I would appreciate any guidance / idea.

Thanks:)

You can use the Observer pattern

using System;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
using System.Collections.Concurrent;

public class program
{
    public static void Main()
    {
        var robot = new Robot();

        robot.Died += (sender, eventArgs) => Console.WriteLine("Robot Died");

        robot.TakeDamage();
        robot.TakeDamage();
    }
}

// --- Everything above this line would be in the UI layer
// --- Everything below this line would be in the business/domain layer     

public class Robot
{
    private int _health = 2;
    public EventHandler Died;

    public Robot(){}

    public void TakeDamage()
    {
        _health--;

        if (_health == 0)
            Died.Invoke(this, new EventArgs());
    }
}

You must create at least 3 layers for a uniform layered architecture. It is important to divide business and data communication. For example, if it is necessary to create a structure;

  • Entities
  • Data Logic Layer
  • Business Logic layer
  • Presentation Layer

So your sturcture should be like that;

  • /Connections /entities /DLL
  • /iDLL (interfaces/abstract classes)
  • /BLL
  • /iBLL (interfaces/abstract classes)
  • /UI

You should define your functions in the iDLL for data communication. For example save or calculate or payment etc. Then implement to DLL these functions. After then BLL class should be extends DLL class. In the BLL you create your program logic.For Example; check until the current money is equal to the fee, if it is equal, send it to the data layer. While doing this, you can hold the coin in the sturctur and call it from the presentation layer, or you can set up a reverse logic, check it in the presentation layer, and then validate it in the business layer. I think second logic the best. Almost web project runs like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM