feat(desIGN-pattERns): 实现抽象工厂模式并重构线程演示代码
- 新增AbstractFactoryPattern类,展示如何使用抽象工厂模式来创建不同风格的家具。 - 重构ThreadDemo1类,修正多线程示例,确保map变量的线程安全操作。 抽象工厂模式允许在不指定具体类的情况下创建一系列相关对象,本提交提供了一个应用示例。在ThreadDemo1类中,避免了在并发情况下潜在的map变量操作问题,确保线程安全。
This commit is contained in:
parent
030780afa8
commit
f40f87b246
@ -0,0 +1,102 @@
|
||||
package cn.whaifree.designPattern;
|
||||
|
||||
public class AbstractFactoryPattern {
|
||||
|
||||
// https://kamacoder.com/problempage.php?pid=1077
|
||||
public static void main(String[] args) {
|
||||
ModernFactory factory = new ModernFactory();
|
||||
Sofa sofa = factory.generateSofa();
|
||||
sofa.proSofa();
|
||||
Chair chair = factory.generateChair();
|
||||
chair.proChair();
|
||||
ClassicFactory classicFactory = new ClassicFactory();
|
||||
Sofa sofa1 = classicFactory.generateSofa();
|
||||
sofa1.proSofa();
|
||||
Chair chair1 = classicFactory.generateChair();
|
||||
chair1.proChair();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
interface Sofa{
|
||||
void proSofa();
|
||||
}
|
||||
|
||||
interface Chair{
|
||||
void proChair();
|
||||
}
|
||||
|
||||
class ClassSofa implements Sofa{
|
||||
|
||||
@Override
|
||||
public void proSofa() {
|
||||
System.out.println("class sofa");
|
||||
}
|
||||
}
|
||||
|
||||
class ModernSofa implements Sofa{
|
||||
|
||||
@Override
|
||||
public void proSofa() {
|
||||
System.out.println("modern sofa");
|
||||
}
|
||||
}
|
||||
|
||||
class ClassChair implements Chair{
|
||||
|
||||
@Override
|
||||
public void proChair() {
|
||||
System.out.println("class chair");
|
||||
}
|
||||
}
|
||||
|
||||
class ModernChair implements Chair{
|
||||
|
||||
@Override
|
||||
public void proChair() {
|
||||
System.out.println("modern chair");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
interface AbstractFactory{
|
||||
Sofa generateSofa();
|
||||
Chair generateChair();
|
||||
}
|
||||
|
||||
class ModernFactory implements AbstractFactory {
|
||||
|
||||
@Override
|
||||
public Sofa generateSofa() {
|
||||
return new ModernSofa();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Chair generateChair() {
|
||||
|
||||
return new ModernChair();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ClassicFactory implements AbstractFactory {
|
||||
|
||||
@Override
|
||||
public Sofa generateSofa() {
|
||||
|
||||
return new ClassSofa();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Chair generateChair() {
|
||||
|
||||
return new ClassChair();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
70
src/main/java/cn/whaifree/leetCode/String/LeetCode415.java
Normal file
70
src/main/java/cn/whaifree/leetCode/String/LeetCode415.java
Normal file
@ -0,0 +1,70 @@
|
||||
package cn.whaifree.leetCode.String;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class LeetCode415 {
|
||||
|
||||
@Test
|
||||
public void main() {
|
||||
System.out.println(new Solution().addStrings("111", "59999"));
|
||||
}
|
||||
|
||||
|
||||
class Solution {
|
||||
public String addStrings(String num1, String num2) {
|
||||
char[] c1 = num1.toCharArray();
|
||||
char[] c2 = num2.toCharArray();
|
||||
int index1 = c1.length - 1;
|
||||
int index2 = c2.length - 1;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
boolean retail = false;
|
||||
while (index1 >= 0 && index2 >= 0) {
|
||||
int char1 = c1[index1] - '0';
|
||||
int char2 = c2[index2] - '0';
|
||||
int tmpAns = char1 + char2;
|
||||
if (retail) {
|
||||
tmpAns += 1;
|
||||
retail = false;
|
||||
}
|
||||
if (tmpAns >= 10) {
|
||||
tmpAns %= 10;
|
||||
retail = true;
|
||||
}
|
||||
stringBuilder.append(tmpAns);
|
||||
index2--;
|
||||
index1--;
|
||||
}
|
||||
|
||||
while (index1 >= 0) {
|
||||
int tmpAns = c1[index1--] - '0';
|
||||
if (retail) {
|
||||
tmpAns += 1;
|
||||
retail = false;
|
||||
}
|
||||
if (tmpAns >= 10) {
|
||||
tmpAns %= 10;
|
||||
retail = true;
|
||||
}
|
||||
stringBuilder.append( tmpAns);
|
||||
}
|
||||
|
||||
while (index2 >= 0) {
|
||||
int tmpAns = c2[index2--] - '0';
|
||||
if (retail) {
|
||||
tmpAns += 1;
|
||||
retail = false;
|
||||
}
|
||||
if (tmpAns >= 10) {
|
||||
tmpAns %= 10;
|
||||
retail = true;
|
||||
}
|
||||
stringBuilder.append(tmpAns);
|
||||
}
|
||||
|
||||
if (retail) {
|
||||
stringBuilder.append(1);
|
||||
}
|
||||
return stringBuilder.reverse().toString();
|
||||
}
|
||||
}
|
||||
}
|
63
src/main/java/cn/whaifree/redo/redoAll/LeetCode93.java
Normal file
63
src/main/java/cn/whaifree/redo/redoAll/LeetCode93.java
Normal file
@ -0,0 +1,63 @@
|
||||
package cn.whaifree.redo.redoAll;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LeetCode93 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
System.out.println(new Solution().restoreIpAddresses("25525511135"));
|
||||
}
|
||||
|
||||
|
||||
class Solution {
|
||||
|
||||
List<String> path = new ArrayList<>();
|
||||
List<String> ans = new ArrayList<>();
|
||||
public List<String> restoreIpAddresses(String s) {
|
||||
backTracking(s, 0, 0);
|
||||
return ans;
|
||||
}
|
||||
|
||||
public void backTracking(String s, int start,int number) {
|
||||
if (number > 4 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (start >= s.length() && path.size() == 4) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (String string : path) {
|
||||
stringBuilder.append(string).append(".");
|
||||
}
|
||||
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
|
||||
ans.add(stringBuilder.toString());
|
||||
}
|
||||
|
||||
for (int i = start; i < s.length(); i++) {
|
||||
if (isValid(s, start, i + 1)) {
|
||||
path.add(s.substring(start, i + 1));
|
||||
backTracking(s, i + 1, number + 1);
|
||||
path.remove(path.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean isValid(String s, int start, int end) {
|
||||
String substring = s.substring(start, end);
|
||||
if (substring.length() > 3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (substring.length() >= 2 && substring.startsWith("0")) {
|
||||
return false;
|
||||
}
|
||||
int integer = Integer.parseInt(substring);
|
||||
return integer >= 0 && integer <= 255;
|
||||
}
|
||||
}
|
||||
}
|
@ -6,7 +6,9 @@ import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ThreadDemo1 {
|
||||
|
||||
@ -191,10 +193,46 @@ class p2{
|
||||
}
|
||||
}
|
||||
|
||||
class dMyException extends Exception {
|
||||
|
||||
class mockException{
|
||||
static ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Thread(() -> {
|
||||
for (int i = 0; i <100 ; i++) {
|
||||
if (map.containsKey("key")) {
|
||||
map.remove("key");
|
||||
}
|
||||
|
||||
}
|
||||
}).start();
|
||||
|
||||
new Thread(() -> {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
if (!map.containsKey("key")) {
|
||||
map.put("key", 1);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
}
|
||||
class mockException{
|
||||
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
|
||||
|
||||
|
||||
|
||||
List<Future<Integer>> futures = new ArrayList<>();
|
||||
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(
|
||||
() -> 1
|
||||
, Executors.newFixedThreadPool(10));
|
||||
futures.add(future);
|
||||
for (Future<Integer> integerFuture : futures) {
|
||||
System.out.println(integerFuture.get());
|
||||
}
|
||||
|
||||
|
||||
|
||||
ThreadLocal<Object> objectThreadLocal = new InheritableThreadLocal<>();
|
||||
|
Loading…
Reference in New Issue
Block a user