gpt4 book ai didi

java - 整洁架构。方案的理解

转载 作者:行者123 更新时间:2023-11-29 18:28:43 39 4
gpt4 key购买 nike

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

图一:

picture 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();
}
}

许可证.java

public interface License {
public void calcFee();
}

PersonalLicense.java

public class PersonalLicense implements License {

public PersonalLicense(){
//constructor implementation here
}

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

BusinessLicense.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
}
}

图二: enter image description 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: enter image description here

我的实现:

Permissions.java

public class Permissions{

public Permissions() { ... }

public classMethod() { ... }
}

User1.java

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

下划线实现

IPermissions.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();
}
}

这些方案的共同细节是: enter image description here

主.java

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

ITest.java

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

测试.java

public class Test implements ITest{

public Test() { ... }

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

我是否理解这些方案?

最佳答案

图1:

License, PersonalLicense, BusinessLicense 可以,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), 将它们中的每一个放入一个单独的类 (PersonalLicense,BusinessLicense), 然后使他们的对象可以互换。主要特点是Billing类只知道它有一些License对象,可以calcFee,不知 Prop 体的实现。稍后,为了支持新的许可类型,您将创建 License 的新实现并且不会修改 Billing

图2:

User1User2User3,必须是这样的,有相应的 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 的接口(interface)隔离原则示例它声明不应强制客户端(User1User2User3)依赖于它不使用的方法(User1 只需要 op1()).

图3:

与前面的示例一样,关联必须通过 User 实例字段来实现。这些图表演示了 Dependency inversion principle (上 - 不好的做法,下划线 - 好的做法)。根据它,User 必须只知道一些抽象的 Permissions 接口(interface)而不是具体的实现,Permissions 类只知道 Permissions 它实现的接口(interface)。使用此原则,Entities 模块创建自己的抽象级别(API)- Permissions 接口(interface)和使用它的Authorizer。相关术语是Dependency injection在 java 框架中常用(例如 Spring Framework )用于低 coupling模块之间

关于java - 整洁架构。方案的理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57527367/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com