13 changed files with 521 additions and 0 deletions
@ -0,0 +1,31 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2023 niushuai233 niushuai.cc |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01; |
||||||
|
|
||||||
|
/** |
||||||
|
* 电子产品抽象类 |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 15:10 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
public interface IProduct { |
||||||
|
|
||||||
|
void start(); |
||||||
|
|
||||||
|
void shutdown(); |
||||||
|
} |
@ -0,0 +1,34 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2023 niushuai233 niushuai.cc |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01; |
||||||
|
|
||||||
|
import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile.IMobileProduct; |
||||||
|
import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc.IPcProduct; |
||||||
|
|
||||||
|
/** |
||||||
|
* 电子产品抽象工厂类 |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 15:10 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
public interface IProductFactory { |
||||||
|
|
||||||
|
IMobileProduct producePhone(); |
||||||
|
|
||||||
|
IPcProduct producePc(); |
||||||
|
} |
@ -0,0 +1,43 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2023 niushuai233 niushuai.cc |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01; |
||||||
|
|
||||||
|
import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile.OPPOPhone; |
||||||
|
import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc.OPPOPc; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
|
/** |
||||||
|
* oppo factory |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 15:24 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
public class OPPOFactory implements IProductFactory { |
||||||
|
@Override |
||||||
|
public OPPOPhone producePhone() { |
||||||
|
log.info("Producing phone by oppo"); |
||||||
|
return new OPPOPhone(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public OPPOPc producePc() { |
||||||
|
log.info("Producing pc by oppo"); |
||||||
|
return new OPPOPc(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,45 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2023 niushuai233 niushuai.cc |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01; |
||||||
|
|
||||||
|
import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile.IMobileProduct; |
||||||
|
import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile.XiaomiPhone; |
||||||
|
import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc.IPcProduct; |
||||||
|
import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc.XiaomiPc; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
|
/** |
||||||
|
* xiaomi factory |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 15:24 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
public class XiaomiFactory implements IProductFactory { |
||||||
|
@Override |
||||||
|
public IMobileProduct producePhone() { |
||||||
|
log.info("Producing phone by xiaomi"); |
||||||
|
return new XiaomiPhone(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public IPcProduct producePc() { |
||||||
|
log.info("Producing pc by xiaomi"); |
||||||
|
return new XiaomiPc(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,33 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2023 niushuai233 niushuai.cc |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile; |
||||||
|
|
||||||
|
import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.IProduct; |
||||||
|
|
||||||
|
/** |
||||||
|
* 手机产品类 |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 15:10 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
public interface IMobileProduct extends IProduct { |
||||||
|
|
||||||
|
void call(String number); |
||||||
|
|
||||||
|
void sendSMS(String number, String message); |
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2023 niushuai233 niushuai.cc |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile; |
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
|
/** |
||||||
|
* oppo phone |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 15:13 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
public class OPPOPhone implements IMobileProduct { |
||||||
|
@Override |
||||||
|
public void start() { |
||||||
|
log.info("oppo phone started"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void shutdown() { |
||||||
|
log.info("oppo phone shutdown"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void call(String number) { |
||||||
|
log.info("oppo phone call {} say hello", number); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void sendSMS(String number, String message) { |
||||||
|
log.info("oppo phone send SMS {} to {}", message, number); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2023 niushuai233 niushuai.cc |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile; |
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
|
/** |
||||||
|
* vivo phone |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 15:13 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
public class VIVOPhone implements IMobileProduct { |
||||||
|
@Override |
||||||
|
public void start() { |
||||||
|
log.info("vivo phone started"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void shutdown() { |
||||||
|
log.info("vivo phone shutdown"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void call(String number) { |
||||||
|
log.info("vivo phone call {} say hello", number); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void sendSMS(String number, String message) { |
||||||
|
log.info("vivo phone send SMS {} to {}", message, number); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2023 niushuai233 niushuai.cc |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile; |
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
|
/** |
||||||
|
* 小米手机 |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 15:12 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
public class XiaomiPhone implements IMobileProduct { |
||||||
|
@Override |
||||||
|
public void start() { |
||||||
|
log.info("xiaomi phone started"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void shutdown() { |
||||||
|
log.info("xiaomi phone shutdown"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void call(String number) { |
||||||
|
log.info("xiaomi phone call {} say hello", number); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void sendSMS(String number, String message) { |
||||||
|
log.info("xiaomi phone send SMS {} to {}", message, number); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,31 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2023 niushuai233 niushuai.cc |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc; |
||||||
|
|
||||||
|
import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.IProduct; |
||||||
|
|
||||||
|
/** |
||||||
|
* pc interface
|
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 15:16 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
public interface IPcProduct extends IProduct { |
||||||
|
|
||||||
|
void surfing(); |
||||||
|
} |
@ -0,0 +1,44 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2023 niushuai233 niushuai.cc |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc; |
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
|
/** |
||||||
|
* oppo pc |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 15:19 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
public class OPPOPc implements IPcProduct { |
||||||
|
@Override |
||||||
|
public void start() { |
||||||
|
log.info("oppo pc Starting"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void shutdown() { |
||||||
|
log.info("oppo pc shutdown"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void surfing() { |
||||||
|
log.info("oppo pc surfing on the internet"); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,45 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2023 niushuai233 niushuai.cc |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc; |
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
|
/** |
||||||
|
* xiaomi pc |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 15:19 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
public class XiaomiPc implements IPcProduct { |
||||||
|
|
||||||
|
@Override |
||||||
|
public void start() { |
||||||
|
log.info("xiaomi pc Starting"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void shutdown() { |
||||||
|
log.info("xiaomi pc shutdown"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void surfing() { |
||||||
|
log.info("xiaomi pc surfing on the internet"); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,8 @@ |
|||||||
|
/** |
||||||
|
* 抽象工厂模式 |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 11:41 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory; |
@ -0,0 +1,60 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2023 niushuai233 niushuai.cc |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01; |
||||||
|
|
||||||
|
import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile.IMobileProduct; |
||||||
|
import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc.IPcProduct; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
/** |
||||||
|
* product factory |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 15:28 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
public class ProductFactoryTest { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testProduct() { |
||||||
|
|
||||||
|
log.info("================xiaomi product demo================"); |
||||||
|
XiaomiFactory xiaomiFactory = new XiaomiFactory(); |
||||||
|
testPhone(xiaomiFactory.producePhone()); |
||||||
|
testPc(xiaomiFactory.producePc()); |
||||||
|
|
||||||
|
log.info("================OPPO product demo================"); |
||||||
|
OPPOFactory oppoFactory = new OPPOFactory(); |
||||||
|
testPhone(oppoFactory.producePhone()); |
||||||
|
testPc(oppoFactory.producePc()); |
||||||
|
} |
||||||
|
|
||||||
|
private void testPc(IPcProduct pc) { |
||||||
|
pc.start(); |
||||||
|
pc.surfing(); |
||||||
|
pc.shutdown(); |
||||||
|
} |
||||||
|
|
||||||
|
private void testPhone(IMobileProduct phone) { |
||||||
|
phone.start(); |
||||||
|
phone.call("13800138000"); |
||||||
|
phone.sendSMS("13800138000", "pls say hello."); |
||||||
|
phone.shutdown(); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue