• Java的设计模式:


    1:工厂模式

    1.1简单工厂模式

    优点:
    
    简单工厂模式,封装了创建对象的逻辑,完成了创建对象逻辑与业务代码逻辑的解耦。
    试想客户端是多个service层的文件,对比不使用简单工厂模式,当我们要改变产生对象的逻辑时,
    需要在多个service文件中找到这部分代码进行修改。在使用简单工厂模式后,只需要修改简单工厂中生成对象的逻辑即可,
    不需要修改业务代码。完成了解耦。
    
    缺点:
    
    每当具体产品类的抽象产品类增多时,会需要在简单工厂类中新增关于新增产品类对象生成的方法。
    当抽象产品类很多时,抽象工厂会很臃肿。并且在这种情形下,SimpleFactory类也不符合开闭原则。
    1. public class SimpleFactory {
    2. public static void main(String[] args) {
    3. A a = getFactory("A1");
    4. a.method();
    5. a = getFactory("A2");
    6. a.method();
    7. }
    8. public static A getFactory(String type){
    9. if("A1".equals(type)){
    10. return new A1();
    11. }else
    12. return new A2();
    13. }
    14. }
    15. interface A{
    16. void method();
    17. }
    18. class A1 implements A{
    19. public void method(){
    20. System.out.println("A1-----------");
    21. }
    22. }
    23. class A2 implements A{
    24. public void method(){
    25. System.out.println("A2-----------");
    26. }
    27. }

     

    1.2工厂模式

    从简单工厂模式的讲述知道:简单工厂的一个缺点在于,每当需要新增产品时,
    都需要修改负责生产产品的SimpleFactory类,违背了“开闭原则”,
    并且会使SimpleFactory类十分的臃肿。
    
    而使用工厂方法模式后,当新增ProductC时,
    只需要对应创建具体产品类ProductC和负责生产ProductC的具体工厂FactoryC即可。符合“开闭原则”,便于扩展。
    
    它的缺点在于:
    (1)类的个数容易过多,增加复杂度
    (2)实现抽象工厂接口的具体工厂只能生产出一种产品(可以用抽象工厂模式解决)
    1. public class Factory {
    2. public static void main(String[] args) {
    3. B b1 = new B1Factory().getB();
    4. b1.method();
    5. B b2 = new B2Factory().getB();
    6. b2.method();
    7. }
    8. }
    9. interface B {
    10. void method();
    11. }
    12. class B1 implements B {
    13. @Override
    14. public void method() {
    15. System.out.println("B1----------");
    16. }
    17. }
    18. class B2 implements B {
    19. @Override
    20. public void method() {
    21. System.out.println("B2----------");
    22. }
    23. }
    24. interface Factorys {
    25. B getB();
    26. }
    27. class B1Factory implements Factorys {
    28. @Override
    29. public B getB() {
    30. return new B1();
    31. }
    32. }
    33. class B2Factory implements Factorys {
    34. @Override
    35. public B getB() {
    36. return new B2();
    37. }
    38. }

    1.3抽象工厂模式

    抽象工厂模式是用于解决“一类产品”的创建问题(在上述场景中,可以把“李宁篮球”,“李宁足球”,“安踏篮球”,“安踏足球”归纳为:“篮球”,“足球”这两类商品)
     
    1. public class AbstractFactory {
    2. public static void main(String[] args) {
    3. AbstractFactorys lnFactory = new LNFactory();
    4. lnFactory.getFootballFactory().makeFootBall();
    5. lnFactory.getBasketballFactory().makeBasketBall();
    6. AbstractFactorys nkFactory = new NKFactory();
    7. nkFactory.getFootballFactory().makeFootBall();
    8. nkFactory.getBasketballFactory().makeBasketBall();
    9. }
    10. }
    11. //抽象工厂
    12. interface AbstractFactorys{
    13. Football getFootballFactory();
    14. Basketball getBasketballFactory();
    15. }
    16. //抽象产品
    17. interface Football{
    18. void makeFootBall();
    19. }
    20. interface Basketball{
    21. void makeBasketBall();
    22. }
    23. //具体产品
    24. class LNFootball implements Football{
    25. @Override
    26. public void makeFootBall() {
    27. System.out.println("makeLNFootball-----------");
    28. }
    29. }
    30. class NKFootball implements Football{
    31. @Override
    32. public void makeFootBall() {
    33. System.out.println("makeNKFootball-----------");
    34. }
    35. }
    36. class LNBasketball implements Basketball{
    37. @Override
    38. public void makeBasketBall() {
    39. System.out.println("makeLNBasketball-----------");
    40. }
    41. }
    42. class NKBasketball implements Basketball{
    43. @Override
    44. public void makeBasketBall() {
    45. System.out.println("makeNKBasketball-----------");
    46. }
    47. }
    48. //具体工厂
    49. class LNFactory implements AbstractFactorys{
    50. @Override
    51. public Football getFootballFactory() {
    52. return new LNFootball();
    53. }
    54. @Override
    55. public Basketball getBasketballFactory() {
    56. return new LNBasketball();
    57. }
    58. }
    59. class NKFactory implements AbstractFactorys{
    60. @Override
    61. public Football getFootballFactory() {
    62. return new NKFootball();
    63. }
    64. @Override
    65. public Basketball getBasketballFactory() {
    66. return new NKBasketball();
    67. }
    68. }

  • 相关阅读:
    华为历届mate版本
    达梦数据库整合在springboot的使用教程
    FreeRTOS基础(如何学好FreeRTOS?)
    QJsonObject的使用示例
    dhtmlx-gantt甘特图数据展示
    Springboot 集成 WebSocket
    Flutter 项目实战 高德定位计算距离并展示首页数据 (六)
    Android 自定义view中根据状态修改drawable图片
    Cannot add foreign key constraint全网唯一全面正解
    PEG/蛋白Protein/抗体antibody 功能化修饰硫化锌量子点 ZnS QDs
  • 原文地址:https://blog.csdn.net/tianruozhaomi/article/details/125908818