Commit 195dc221 authored by sz-test's avatar sz-test
Browse files

Merge branch 'init' into 'main'

init

See merge request !1
parents ac7cbb43 7f583445
package factory.static1;
import factory.BMW;
/**
* Copyright (c) 2021, ustchcs and its affiliates. All rights reserved. More info see www.ustchcs.com
*
* @author cch
* @package Thymeleaf1 com.example.demo.design
* @node
*/
public class StaticFactory {
public static void main(String[] args) {
BMW car730 = BMWFactory.productBMW("730");
BMW car840 = BMWFactory.productBMW("840");
if (car730 != null) {
car730.run();
}
if (car840 != null) {
car840.run();
}
}
}
package strategy;
/**
* Copyright (c) 2023, ustchcs and its affiliates. All rights reserved. More info see www.ustchcs.com
*
* @author cch
* @package java-test strategy
* @node
*/
public class CustomAuthenticator {
private IStrategy strategy;
public CustomAuthenticator(IStrategy strategyInput) {
this.strategy = strategyInput;
}
public void authenticate(String name, String password) {
if (this.strategy == null) {
System.out.println("no strategy");
return;
}
this.strategy.authenticate(name, password);
}
public void setStrategy(IStrategy strategy) {
this.strategy = strategy;
}
}
package strategy;
/**
* Copyright (c) 2023, ustchcs and its affiliates. All rights reserved. More info see www.ustchcs.com
*
* @author cch
* @package java-test strategy
* @node
*/
public interface IStrategy {
void authenticate(String name, String password);
}
package strategy;
import tool.StrUtils;
/**
* Copyright (c) 2023, ustchcs and its affiliates. All rights reserved. More info see www.ustchcs.com
*
* @author cch
* @package java-test strategy
* @node
*/
public class LocalStrategy implements IStrategy {
@Override
public void authenticate(String name, String password) {
System.out.println(StrUtils.concatNullToEmpty("local login:name=", name));
}
}
package strategy;
/**
* Copyright (c) 2021, ustchcs and its affiliates. All rights reserved. More info see www.ustchcs.com
*
* @author cch
* @package Thymeleaf1 com.example.demo.design.strategy
* @node
*/
public class StrategyDesign {
public static void main(String[] args) {
WechatStrategy wechatStrategy = new WechatStrategy();
CustomAuthenticator customAuthenticator = new CustomAuthenticator(wechatStrategy);
customAuthenticator.authenticate("cch", "1234556");
customAuthenticator.setStrategy(new LocalStrategy());
customAuthenticator.authenticate("cch2", "12345567");
}
}
package strategy;
import tool.StrUtils;
/**
* Copyright (c) 2023, ustchcs and its affiliates. All rights reserved. More info see www.ustchcs.com
*
* @author cch
* @package java-test strategy
* @node
*/
public class WechatStrategy implements IStrategy {
@Override
public void authenticate(String name, String password) {
System.out.println(StrUtils.concatNullToEmpty("wechat login:name=", name));
}
}
package tool;
/**
* Copyright (c) 2023, ustchcs and its affiliates. All rights reserved. More
* info see www.ustchcs.com
*
* @author cch
* @package backend com.ustchcs.qualityhub.system.utils
* @node
*/
public class StrUtils {
public static final String EMPTY = "";
public static String concatNullToEmpty(CharSequence... strs) {
return concat(true, strs);
}
public static String concat(boolean isNullToEmpty, CharSequence... strs) {
final StringBuilder sb = new StringBuilder();
for (CharSequence str : strs) {
if (isNullToEmpty) {
sb.append(nullToEmpty(str));
} else {
sb.append(str);
}
}
return sb.toString();
}
public static String nullToEmpty(CharSequence str) {
return nullToDefault(str, EMPTY);
}
public static String nullToDefault(CharSequence str, String defaultStr) {
if (str == null) {
return defaultStr;
}
return str.toString();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment