Czy da się przeciążyć jedną metodę żeby nie robić kilku o tej samej nazwie?

0

Pytanie w sumie tak jak w temacie.
Staram się pisać program zgodnie z zasadą DROP i próbuję przeciążyć metodę tak żeby nie pisać kilku metod od tej samej nazwie bo blok logiki będzie się tak czy siak powtarzał i wówczas nie ma już dla mnie większego zanaczenia czy te metody mają tę samą nazwę czy inną.

package testuj;

public class Testuj {

    public static void main(String[] args) {
        Testuj test = new Testuj();
        test.start();
    }

    void start() {
        int przyklad1 = przyklad();
        int przyklad2 = przyklad(3, 2);
        int przyklad3 = przyklad("jakis text...");
        System.out.println(przyklad1);
        System.out.println(przyklad2);
        System.out.println(przyklad3);
    }

    int przyklad() {
   /**********************************************/
   /*******                                *******/
   /*******          BLOK LOGIKI           *******/
   /*******                                *******/
   /**********************************************/        
        int wynik = 0;
        int x = 4;
        int y = 5;
        wynik = x + y;
        return wynik;
    }

    int przyklad(int x, int y) {
   /**********************************************/
   /*******                                *******/
   /*******          BLOK LOGIKI           *******/
   /*******                                *******/
   /**********************************************/
        int wynik = 0;
        wynik = x + y;
        return wynik;
    }
    int przyklad(String txt) {
   /**********************************************/
   /*******                                *******/
   /*******          BLOK LOGIKI           *******/
   /*******                                *******/
   /**********************************************/       
        return 100;
    }
    // Chciałbym zapisać to w jednej metodzie - coś takiego....

    int zlyPrzykladwJednejMetodzie() {
   /**********************************************/
   /*******                                *******/
   /*******          BLOK LOGIKI           *******/
   /*******                                *******/
   /**********************************************/

    public zlyPrzyklad() {
        int wynik = 0;
        int x = 4;
        int y = 5;
        wynik = x + y;
        return wynik;
    }

    public zlyPrzyklad(int x, int y) {
        int wynik = 0;
        wynik = x + y;
        return wynik;
    }

    public zlyPrzyklad(String txt) {
        return 100;
    }
}

}

 
1

http://www.acronymfinder.com/DROP.html
chyba chodziło ci o DRY

public int dobryPrzyklad() {
    return dobryPrzyklad(3, 4);
}
0

Jeśli blok logiki będzie się powtarzał niezależnie od metody, to ... powinien być wydzielony do oddzielnej metody ;-)

0

właśnie chodzi o to że ten blok logiki to inicjalizacja komponentów, stworzenie obiektów itp. więc nie bardzo może być w innej metodzie bo wtedy reszta kodu nie będzie działać

0
TomDom napisał(a):

właśnie chodzi o to że ten blok logiki to inicjalizacja komponentów, stworzenie obiektów itp. więc nie bardzo może być w innej metodzie bo wtedy reszta kodu nie będzie działać
Dlaczego? Podaj przykład, kiedy to nie działa.

0
    BufferedImage snapShot() throws AWTException { 
//ma zrobic printScreena obrazu
        robot = new Robot();
        width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        screenRct = new Rectangle(0, 0, width, height);
        
        BufferedImage snapShot = robot.createScreenCapture(screenRct);
        

        return snapShot;
    }
    
    BufferedImage snapShot(BufferedImage img, int positionX, int positionY, int widthI,int heightI,int sizeW,int sizeH) throws AWTException {
//ma zwrócić fragment z przekazanego obrazka
        img  = img.getSubimage(positionX, positionY, widthI, heightI);
        ImageIcon imgIcon = new ImageIcon(img);
        Image imgI = imgIcon.getImage();  
        imgI = imgI.getScaledInstance(sizeW, sizeH,  java.awt.Image.SCALE_SMOOTH);  
        img=(BufferedImage) imgI;

        return img;
    }
    BufferedImage snapShot(int positionX, int positionY, int widthI,int heightI,int sizeW,int sizeH) throws AWTException {
//ma zwrócić fragment z printScreena
        robot = new Robot();
        width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        screenRct = new Rectangle(0, 0, width, height);
        
        BufferedImage snapShot = robot.createScreenCapture(screenRct);
 
        BufferedImage img  = snapShot.getSubimage(positionX, positionY, widthI, heightI);
        ImageIcon imgIcon = new ImageIcon(img);
        Image imgI = imgIcon.getImage();  
        imgI = imgI.getScaledInstance(sizeW, sizeH,  java.awt.Image.SCALE_SMOOTH);  
        img=(BufferedImage) imgI;  
        
        return img;
    }
 
0
    BufferedImage snapShot(int positionX, int positionY, int widthI,int heightI,int sizeW,int sizeH) throws AWTException {
//ma zwrócić fragment z printScreena
        BufferedImage snapShot = snapShot();
 
        BufferedImage img  = snapShot.getSubimage(positionX, positionY, widthI, heightI);
        ImageIcon imgIcon = new ImageIcon(img);
        Image imgI = imgIcon.getImage();  
        imgI = imgI.getScaledInstance(sizeW, sizeH,  java.awt.Image.SCALE_SMOOTH);  
        img=(BufferedImage) imgI;  
 
        return img;
    }

Metody snapShot powinny nazywać się getSnapShot - metoda "robi".

0

Przykładowo to można wywalić do osobnej funkcji:

robot = new Robot();
width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
screenRct = new Rectangle(0, 0, width, height);
0

Ewidentnie widać, że można ponownie użyć pokrewne metody, w jednej z wersji:

BufferedImage snapShot(int positionX, int positionY, int widthI,int heightI,int sizeW,int sizeH) throws AWTException {
        BufferedImage snapShot = snapShot();
 
        return snapShot(snapShot, positionX, positionY, widthI, heightI, sizeW, sizeH);
    }

Swoją drogą, ja bym oczekiwał od API, że przeładowana funkcja zwraca zawsze ten sam typ. Jeśli z jakiejś przyczyny tak nie mogło by być, to dałbym na siłę inne nazwy.

0

Od momentu otrzymania img cała reszta jest wspólna - wniosek: Extract Method:

https://blogs.oracle.com/geertjan/entry/extract_method_functionality_in_netbeans

0

Ja bym to potraktował wzorcem strategii.

0

Rozumiem, że odpowiedzią na postawione pytanie jest:

NIE DA SIĘ PRZECIĄŻYĆ POJEDYNCZEJ METODY

można jedynie rozwiązać to w inny sposób.

1 użytkowników online, w tym zalogowanych: 0, gości: 1