代码之家  ›  专栏  ›  技术社区  ›  Brody Gore

我对如何在代码中实现单例模式并让它调用我的对象类和方法感到困惑

  •  1
  • Brody Gore  · 技术社区  · 7 年前

    正如标题所示,我正试图在代码中实现单例设计模式。我的代码有一个主类和3个继承类。我只想创建一个能够使用代码中所有函数的对象。这是我第一次处理设计模式,所以我的代码可能无处不在。代码如下:

    class geniusATM {
    
        private String name, address;
        private int pin, ficoScore;
        double checkingBalance, savingBalance, mortgageBalance;
    
        public geniusATM() {
    
        }
    
        geniusATM(String nam, String addr, int pn, int fs, double cB, double sB, double mB) {
            name = nam;
            address = addr;
            ficoScore = fs;
            checkingBalance = cB;
            savingBalance = sB;
            mortgageBalance = mB;
        }
    
             //setters and getters are here
            }
    
           class Checkings extends geniusATM {
            //stuff here
              }
    
           class Savings extends geniusATM {
            //stuff here
             }
    
           class billPay extends Checkings {
           //stuff here
    
           }
    
         public class singletonObject {
    
          private static singletonObject ob;
    
        private  singletonObject() {
            geniusATM matt = new geniusATM("Matt", "124 Road Drive.", 1234, 3462, 560.00, 500.50, 472.29);
    
        }
    
        public static singletonObject getObject() {
            if (ob == null) {
                ob = new singletonObject();
            }
            return ob;
        }
    
        public static void main(String[] args) {
    
    
        }
    
    
    }
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   Sideeg MoHammed    7 年前

    现在,使用ob变量,您可以从4个类中的任何类中获得封装在其中的all函数

    class geniusATM {
    
    private String name, address;
    private int pin, ficoScore;
    double checkingBalance, savingBalance, mortgageBalance;
    
    public geniusATM() {
    
    }
    
    geniusATM(String nam, String addr, int pn, int fs, double cB, double sB, double mB) {
        name = nam;
        address = addr;
        ficoScore = fs;
        checkingBalance = cB;
        savingBalance = sB;
        mortgageBalance = mB;
    }
    
         //setters and getters are here
        }
    
       class Checkings extends geniusATM {
           public Checkings() {
    
    }
         Checkings (String nam, String addr, int pn, int fs, double cB, double sB, double mB) {super( nam, addr,  pn,  fs,cB, sB, mB);}
          }
    
       class Savings extends Checkings {
           public Savings() {
    
    }
         Savings (String nam, String addr, int pn, int fs, double cB, double sB, double mB) {super( nam, addr,  pn,  fs,cB, sB, mB);}
          }
    
    
       class billPay extends Savings {
           public billPay() {
    
    }
       billPay (String nam, String addr, int pn, int fs, double cB, double sB, 
    double mB) {super( nam, addr,  pn,  fs,cB, sB, mB);}
          }
    
    
     public class singletonObject {
    
      private static Checkings ob;
    
    private  singletonObject () {
        billPay matt = new billPay ("Matt", "124 Road Drive.", 1234, 3462, 560.00, 500.50, 472.29);
    
    }
    
    public static Checkings getObject() {
        if (ob == null) {
            ob = new billPay();
        }
        return ob;
    }
    
    public static void main(String[] args) {
    
    
    }
    

    }

        2
  •  0
  •   Glim    7 年前

    首先,使用java类命名约定,您的类应该 GeniusAtm (类名应该以大写字母开头,并且是一个名词,例如。 String ,则, Color ,则, System ,等等)

    要在此类中应用singleton模式,需要将构造函数设置为 private 并创建 getInstance() 方法此方法检查是否已创建此对象的实例。如果为true,则返回该实例,否则将创建一个新实例,然后返回它。

    它将是这样的:

    public class GeniusAtm {
        private static GeniusAtm instance; // singleton instance here
    
        private String name, address;
        private int pin, ficoScore;
        double checkingBalance, savingBalance, mortgageBalance;
    
        private geniusATM() {
    
        }
    
        public static synchronized GeniusAtm getInstance() {
            if (instance == null)
                instance = new GeniusAtm();
            return instance;
        }
    
        // other stuffs
    }
    

    这个 synchronized 确保 getInstance() 将是线程安全的。

        3
  •  0
  •   Jacob    7 年前

    使用java命名约定和@Dvorog建议。。。

    public class GeniusAtm() {
    
        private static GeniusAtm gAtm;
    
        private geniusATM() {
    
        }
    
        public static GeniusAtm instance() {
          if(null == gAtm) {
             gAtm = new GeniusAtm();
          }
    
          return gAtm;
        }
    
        //Your other methods here
    }
    

    如果你想用你的例子来说明如何通过单身来接触孩子,你必须做一些像这样丑陋的事情。。。

    public class GeniusAtm {
    
        private String name, address;
        private int pin, fico;
        private double checkingBalance, savingsBalance, mortgageBalance;
        private static GeniusAtm geniusAtm;
        private Checkings checking;
    
        private GeniusAtm (String name, String address, int pin, int fico, double checkingBalance, double savingsBalance, double mortgageBalance) {
            this.name = name;
            this.address = address;
            this.pin = pin;
            this.fico = fico;
            this.checkingBalance = checkingBalance;
            this.savingsBalance = savingsBalance;
            this.mortgageBalance = mortgageBalance;
    
            checking = new Checkings(name,address,pin,fico,checkingBalance,savingsBalance,mortgageBalance);
        }
    
        public static GeniusAtm instance() {
            if(null == geniusAtm) {
                geniusAtm = new GeniusAtm("Matt", "124 Road Drive.", 1234, 3462, 560.00, 500.50, 472.29);
            }
    
            return geniusAtm;
        }
    
        private class Checkings extends GeniusAtm {
    
            Checkings(String name, String address, int pin, int fico, double checkingBalance, double savingsBalance, double mortgageBalance) {
                super(name,address,pin,fico,checkingBalance,savingsBalance,mortgageBalance);
            }
    
            public void updateName(String name) {
    
            }
        }
    
        //OTHER IMPLEMENTATIONS HERE
    
        public Checkings getCheckings() {
            return checking;
        }
    
        public static void main(String[] args) {
            GeniusAtm atm = GeniusAtm.instance();
    
            atm.getCheckings().updateName("That Guy");
        }
    }
    

    然而,我绝不会建议在任何实际意义上做这样的事情,主要是因为冗余和缺乏对OOP继承和组合概念的坚持。你有没有可能 Person Account ,也许 Fico 对象在这种情况下 账户 会是你的单身汉。有几种不同的方法可以做到这一点,但我不推荐使用GeniusTM的子类实现。