1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.cyrgue.designpatterns.chain;

public abstract class ConflictChecker {

protected UserModel anonymousUser;
protected UserModel registeredUser;

protected ConflictChecker nextChecker;

public ConflictChecker(UserModel anonymousUser, UserModel registeredUser) {
this.anonymousUser = anonymousUser;
this.registeredUser = registeredUser;
}

public void setNextChecker(ConflictChecker nextChecker) {
this.nextChecker = nextChecker;
}

public void check() throws Exception {
if (this.hasConflict()) {
process();
} // Possible d'avoir un else ici - La chaine s'arrete
if (nextChecker != null) {
nextChecker.check();
}
}

abstract protected void process() throws Exception;

abstract protected boolean hasConflict();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.cyrgue.designpatterns.chain;

public class CartConflictChecker extends ConflictChecker {

public CartConflictChecker(UserModel anonymousUser, UserModel registeredUser) {
super(anonymousUser, registeredUser);
}

@Override
protected void process() throws Exception {
System.out.println("Cart Conflicts !!!!!");
throw new Exception("Cart Conflicts !!!!!");
}

@Override
protected boolean hasConflict() {
return !anonymousUser.hasSameCart(registeredUser);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.cyrgue.designpatterns.chain;

public class StoreConflictChecker extends ConflictChecker {

public StoreConflictChecker(UserModel anonymousUser, UserModel registeredUser) {
super(anonymousUser, registeredUser);
}


@Override
protected void process() throws Exception {
System.out.println("Store Conflicts !!!!!");
throw new Exception("Store Conflicts !!!!!"); //Pour arreter mais pas obligé
}

@Override
protected boolean hasConflict() {
return !anonymousUser.hasSameShop(registeredUser);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.cyrgue.designpatterns.chain;

public class ConflictCheckerProgram {


private static ConflictChecker getChainOfConflicts(UserModel anonymous, UserModel registered){
ConflictChecker cartConflictChecker = new CartConflictChecker(anonymous, registered);
ConflictChecker storeConflictChecker = new StoreConflictChecker(anonymous, registered);
cartConflictChecker.setNextChecker(storeConflictChecker);
return cartConflictChecker;
}

public static void main(String[] args) {
UserModel anonymous = userBuilder().cartId("1").shopId(1).build();
UserModel registered = userBuilder().cartId("2").shopId(2).build();
ConflictChecker checkerChain = getChainOfConflicts(anonymous, registered);
try {
checkerChain.check();
} catch (Exception e) {
e.printStackTrace();
}
}

private static UserModel.UserModelBuilder userBuilder() {
return new UserModel.UserModelBuilder();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.cyrgue.designpatterns.chain;

import lombok.Builder;
import lombok.Data;

@Builder
@Data
public class UserModel {

private String cartId;
private int shopId;

public boolean hasSameCart(UserModel registeredUser) {
return this.getCartId().equals(registeredUser.getCartId());
}

public boolean hasSameShop(UserModel registeredUser) {
return shopId == registeredUser.getShopId();
}
}