标签: solid-principles

微服务共享域层

我对微服务架构有疑问。我们正在开发一个 ERP,有几个微服务,如人力资源、身份、订单等。

我们为所有这些层通用的实体实现了一个共享域层,包括公司、位置和一些值对象的抽象(接口)。

我的问题是:微服务的共享项的边界是什么,这有多糟糕?

在这种情况下,这些共享实体对于每个微服务都是相同的,这样有助于我们编写更少的代码,但同时会创建一个小的耦合级别。

soa domain-driven-design solid-principles microservices

1
推荐指数
1
解决办法
2016
查看次数

里氏替换原则如何应用于函数返回类型?

里氏替换原则指出:

程序中的对象应该可以用其子类型的实例替换,而不改变该程序的正确性。

假如说:

interface Iterable<T> {
    fun getIterator(): Iterator<T>
}

interface Collection<T> : Iterable<T> {
    val size: Int
}

interface List<T> : Collection<T> {
    fun get(index: Int): T
}

interface MutableList<T> : List<T> {
    fun set(index: Int, item: T): Unit
}
Run Code Online (Sandbox Code Playgroud)

当 LSP 应用于输入参数时,应应用最低级别的抽象:

fun foo(items: Iterable<Any>) { ... }
Run Code Online (Sandbox Code Playgroud)

fun foo(items: List<Any>) { ... }
Run Code Online (Sandbox Code Playgroud)

但是,LSP 是否适用于函数返回类型?如果适用,则反之亦然吗?

fun bar(): Iterable<Any> { ... }
Run Code Online (Sandbox Code Playgroud)

或者

fun bar(): List<Any> { ... }
Run Code Online (Sandbox Code Playgroud)

liskov-substitution-principle solid-principles

1
推荐指数
1
解决办法
840
查看次数

这是反模式吗?

目前,我经常遇到遵循以下代码演示的模式的代码。这是一种策略模式。

我不喜欢这个,感觉有点臭。它打破了实际的策略模式,额外的间接性令人困惑。此外,它通常会导致类似于示例中的方法,因为派生类中方法的用途与基类中的方法非常相似。另一方面,我无法用手指指向问题核心。

只有我一个人觉得这很可疑吗?如果没有,这段代码会导致什么问题,特别是在 SOLID 原则方面?

namespace MyTest
{
    abstract class Base
    {
        public void DoSomething()
        {
            var param = Prepare();
            DoItReally(param);
        }

        private string Prepare()
        {
            return "Hallo Welt";
        }

        protected abstract void DoItReally(string param);
    }

    class DerivedOne : Base
    {
        protected override void DoItReally(string param)
        {
            Console.WriteLine(param);
        }
    }

    class DerivedTwo : Base
    {
        protected override void DoItReally(string param)
        {
            Console.WriteLine(param.ToUpper());
        }
    }
    
    static class Program
    {
        public static void Main(string[] args)
        {
            Base strategy = new DerivedOne();
            strategy.DoSomething(); …
Run Code Online (Sandbox Code Playgroud)

c# oop design-patterns anti-patterns solid-principles

1
推荐指数
1
解决办法
106
查看次数

这是Liskov替换原则违规吗?

我的自定义按钮实际上是一个按钮,它是否违反了LSP?

class ConditionalButton : Button
{
    protected override void OnClick(EventArgs e)
    {
        if (Condition())
            base.OnClick(e);
    }
    private bool Condition()
    {
        //return true or false
    }
}
Run Code Online (Sandbox Code Playgroud)

c# oop solid-principles

0
推荐指数
1
解决办法
167
查看次数

避免LSP违规的最佳方法

让我们考虑以下示例.我有这样的类的层次结构:

abstract class Base
{
    public abstract void DoSomething();
}

class Foo : Base
{
    public override void DoSomething()
    {
        Console.WriteLine("Foo. DoSomething...");
    }
}

class Bar : Base
{
    public override void DoSomething()
    {
        Console.WriteLine("Bar. DoSomething...");

        if (ShouldDoSomethingElse)
        {
            DoSomethingElse();
        }
    }

    public void DoSomethingElse()
    {
        Console.WriteLine("Bar. DoSomething else...");
    }

    public bool ShouldDoSomethingElse { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的客户端是这样的:

class Program
{
    static void Main(string[] args)
    {
        var foo = new Foo();
        var bar = new Bar();
        var items = …
Run Code Online (Sandbox Code Playgroud)

c# oop solid-principles

0
推荐指数
1
解决办法
110
查看次数

如何避免强制转换派生类型-违反Liskov原则

我想避免使用将基类类型转换为派生类类型的方法,我可以成功完成此操作。

我已经编写了代码来演示我已经尝试过的内容。

public abstract class Animal : IAnimal
{
    public void Move()
    {        
    }
}

public interface IAnimal
{
     void Move();
}

public interface IDog:IAnimal
{
    void bark();
}

public class Dog : IDog
{
    public void Move()
    {

    }

    public void bark()
    {

    }
}

static void Main(string[] args)
{
    Animal animal = null;
    IDog dog = animal as IDog;

    dog.bark(); // can access specialized method

    IAnimal puppy = new Dog();
    puppy.Move(); // can only access generic functions    
} …
Run Code Online (Sandbox Code Playgroud)

c# liskov-substitution-principle solid-principles

0
推荐指数
1
解决办法
96
查看次数

如何在Java中创建自注册工厂?

工厂模式违反了 OCP 原则,因为它使用if()语句,这意味着如果添加任何类,则工厂类必须更改,这违反了 SOLID 原则。自注册类应该根据以下资源解决此问题:http://www.jkfill.com/2010/12/29/self-registering-factories-in-c-sharp/。问题是我不懂C#。有人可以用 Java 举一个例子吗?提前致谢。

public class ShapeFactory {

   //The purpose of self registering classes is to avoid if's
   public Shape getShape(String shapeType){
      if(shapeType == null){ //Get rid of this
         return null;
      }     
      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();

      } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();

      } else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }

      return null;
   }
}
Run Code Online (Sandbox Code Playgroud)

java design-patterns solid-principles

0
推荐指数
1
解决办法
2208
查看次数