清洁架构。方案的理解

Clean Archtecture. Understanding of scheme

你能帮我理解接下来的事情吗?所以,我读了 R.Martin 的 Clean Architecture,里面有很多方案。

图片1:

我的实现:

Billing.java

public class Billing {

    public Billing(){

        //creating of licenses
        License personalLicense = new PersonalLicense();
        License businessLicense = new BusinessLicense();

        //method using
        personalLicense.calcFee();
        businessLicense.calcFee();
    }
}

License.java

public interface License {
    public void calcFee();
}

个人License.java

public class PersonalLicense implements License {

    public PersonalLicense(){
        //constructor implementation here
    }

    @Override
    public void calcFee(){
        //method implementation here
    }
}

商业License.java

public class BusinessLicense implements License {

    //private ? users - Unknown type just for example

    @Override
    public BusinessLicense(){
        //constructor implementation here
    }

    public void calcFee(){
        //method implementation here
    }
}

图片二:

我的实现:

U1Ops.java

public interface U1Ops{
    public void op1();
}

U2Ops.java

public interface U2Ops{
    public void op2();
}

U3Ops.java

public interface U3Ops{
    public void op3();
}

OPS.java

public class OPS implements U1Ops, U2Ops, U3Ops{
    public OPS(){ ... }

    @Override
    public void op1() { ... }

    @Override
    public void op2() { ... }

    @Override
    public void op3() { ... }
}

User1.java

public class User1 {
    public User1(){
        OPS u1Ops = new U1Ops();
        u1Ops.op1();
    }
}

User2.java

public class User2 {
    public User2(){
        OPS u2Ops = new U2Ops();
        u2Ops.op2();
    }
}

User3.java

public class User3 {
    public User3(){
        OPS u3Ops = new U3Ops();
        u3Ops.op3();
    }
}

图片3:

我的实现:

Permissions.java

public class Permissions{

    public Permissions() { ... }

    public classMethod() { ... }
}

User1.java

public class User1 {
    public User1(){
        Permissions p = new Permissions();
        p.classMethod();
    }
}

下划线实现

我Permissions.java

public interface IPermissions{
    public void interfaceMethod()();
}

Permissions.java

public class Permissions implements IPermissions{

    public Permissions() { ... }

    @Override
    public interfaceMethod() { ... }
}

User2.java

public class User2 {
    public User2(){
        IPermissions p = new Permissions();
        p.interfaceMethod();
    }
}

这些方案的共同细节是:

Main.java

public class Main {
    public Main(){
        ITest t = new Test();
        t.someMethod();
    }
}

ITest.java

public interface ITest{
    public void someMethod()();
}

Test.java

public class Test implements ITest{

    public Test() { ... }

    @Override
    public someMethod() { ... }
}

我理解这些方案对吗?

图片1:

LicensePersonalLicenseBusinessLicense可以,Billing一定是这样:

public class Billing {
    private Lisense license;

    public Billing(License license){
        this.license = license;
    }

    public void pay(){
       // some code
       this.license.calcFee();
       // some code
    }

    public void setLicense(License license){
        this.license = license;
    }
}

它看起来像 Strategy pattern,它允许您定义一系列算法 (License),将它们中的每一个放入单独的 class(PersonalLicense,BusinessLicense), 并使它们的对象可以互换。主要特点是 Billing class 只知道它有一些 License 对象,可以 calcFee,不知道具体的实现。稍后,为了支持新的许可证类型,您将创建 License 的新实现并且不会修改 Billing.

图片二:

User1, User2, User3, 必须是这样的,对应的 U*Ops:

public class User1 {
    private U1Ops u1Ops;
    public User1(U1Ops u1Ops){
        this.u1Ops = u1Ops;
    }
}

// usage of classes
OPS ops = new OPS();
User1 user1 = new User1(ops);
User2 user2 = new User2(ops);

看起来像来自 SOLID 的接口隔离原则示例,它声明不应强制客户端(User1User2User3)依赖于它的方法不用(User1只需要op1()).

图3:

与前面的示例一样,必须使用 User 实例字段实现关联。这些图表演示了模块之间的 Dependency inversion principle (upper - bad practice, underline - good practice). According it, User must know only about some abstract Permissions interface and not about specific implementation, Permissions class know only about Permissions interface which it implements. With usage of this principle, Entities module create their own abstractions level(API) - Permissions interface and Authorizer using it. Related term to it is Dependency injection with commonly used in java frameworks (e.g. Spring Framework) for low coupling