battle programmers alliance

battle programminga forum for elite programmerswith extreme will powerto sharpen theire skills
 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in
Search
 
 

Display results as :
 
Rechercher Advanced Search
Latest topics
» java eclipse oxygen grimoire
Yesterday at 9:38 pm by Admin

» java concept input output class
Yesterday at 7:21 pm by Admin

» JAVA design patterns
Mon Feb 19, 2018 1:09 pm by Admin

» vb.net morse class coder + decoder
Sat Feb 17, 2018 11:20 am by Admin

» auto programming dev
Mon Feb 12, 2018 7:54 am by Moti Barski

» java brute force counter
Mon Feb 12, 2018 5:02 am by Moti Barski

» java class with learnability exampled with simple car class
Sat Feb 10, 2018 12:55 am by Moti Barski

» polymorphism in JAVA eclipse
Wed Jan 31, 2018 9:20 pm by Admin

» vb.net megazord string outputter
Tue Jan 09, 2018 7:11 pm by Admin

February 2018
SunMonTueWedThuFriSat
    123
45678910
11121314151617
18192021222324
25262728   
CalendarCalendar
Social bookmarking
Social bookmarking digg  Social bookmarking delicious  Social bookmarking reddit  Social bookmarking stumbleupon  Social bookmarking slashdot  Social bookmarking yahoo  Social bookmarking google  Social bookmarking blogmarks  Social bookmarking live      

Bookmark and share the address of battle programmers alliance on your social bookmarking website
Share | 
 

 JAVA design patterns

Go down 
AuthorMessage
Admin
Admin
avatar

Posts : 92
Join date : 2011-08-01

PostSubject: JAVA design patterns   Tue Feb 13, 2018 8:19 pm

l1 : hiding the code :

main :
Code:
package sellerBot;

public class Main {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      seller chi = new seller();
      chi.selectCar();
   }

}

car class :
Code:
package sellerBot;

public abstract class Car {
   protected double price, oil;
   String brand;
   public abstract void specs();
   @Override
   public String toString() {
      return "Car [price=" + price + ", oil=" + oil + ", brand=" + brand + "]";
   }
   
   
}
sub class delorean :
Code:
package sellerBot;

public class Delorean extends Car{
   private boolean flaxCapacitor;
   double plutonium;
   
   public Delorean() {
      super.brand = this.getClass().toString();
      super.oil = 5;
      super.price = 50000;
      this.flaxCapacitor = true;
      this.plutonium = 20;
   }
   @Override
   public void specs() {
      // TODO Auto-generated method stub
      System.out.println(this.toString());
   }
   @Override
   public String toString() {
      return super.toString() + " Delorean [flaxCapacitor=" + flaxCapacitor + ", plutonium=" + plutonium + "]";
   }
   

}   
factory class :
Code:
package sellerBot;

import java.util.Scanner;

public class seller {
   private Car selected;
   public void selectCar() {
      Scanner s1 = new Scanner(System.in); // after typing this intelliJ should add the import code anyways
      System.out.println("type car U want ");
      String x = s1.next(); // .next(); to input a string
      switch (x.toLowerCase()) {
      case "delorean":
         selected = new Delorean();
         break;

      default:
         break;
      }
      System.out.println(selected.toString());
   }
}


_________________
kurayami no kagi  _____    
Back to top Go down
View user profile https://yotamarker.justforum.net
Admin
Admin
avatar

Posts : 92
Join date : 2011-08-01

PostSubject: Re: JAVA design patterns   Wed Feb 14, 2018 10:03 pm


observer DB :
observer interface : (right click package explorer add new interface)
Code:
public interface Observer {
   void update(String... str);
}
pt2 : observable interface :
Code:

public interface Observable {
   void addObserver(Observer obs);
   void delObserver(Observer obs);
   void notifyObservers();
}
class that implements observer (equip spell):
Code:

public class Cashier implements Observer{
   public static int trackID=0;
   private int observerID=0;
   private String price1,price2,price3;
   
   public Cashier(Observable obs) {
      obs.addObserver(this);
      this.observerID = ++trackID;
      System.out.println("new observer created " + this.toString());
   }

   @Override
   public void update(String... str) {
      // TODO Auto-generated method stub
      price1 = str[0];
      price2 = str[1];
      price3 = str[2];
      System.out.println(toString());
   }

   @Override
   public String toString() {
      return "Cashier [observerID=" + observerID + ", price1=" + price1 + ", price2=" + price2 + ", price3=" + price3
            + "]";
   }
   

}
class that implements observable (equip spell):
Code:
import java.util.ArrayList;

public class MainCashier implements Observable{
   private String price1,price2,price3; // observer replica, enables summoning the update
   ArrayList<Observer> Cashiers; // the database of observers (the heart of the pattern)
   public  MainCashier() {
      // TODO Auto-generated constructor stub
      Cashiers = new ArrayList<>();
   }
   @Override
   public void addObserver(Observer obs) {
      // TODO Auto-generated method stub
      Cashiers.add(obs);
   }

   @Override
   public void delObserver(Observer obs) {
      // TODO Auto-generated method stub
      Cashiers.remove(obs);
   }

   @Override
   public void notifyObservers() {
      // TODO Auto-generated method stub
      for(Observer obs: Cashiers) {
         obs.update(price1,price2,price3);
      }
   }
   public void setPrice1(String p1) {this.price1=p1;notifyObservers();}
   public void setPrice2(String p2) {this.price2=p2;notifyObservers();}
   public void setPrice3(String p3) {this.price3=p3;notifyObservers();}

}
now the continues observable field spell makes sure any updates to its replica fields(see code comments)
are notified to all objects in the main cashier "DB"
the main class to test drive it:
Code:

public class Main {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      MainCashier mainCashier = new MainCashier();
      System.out.println();
      Cashier c1 = new Cashier(mainCashier);
      System.out.println();
      mainCashier.setPrice1("500");
      Cashier c2 = new Cashier(mainCashier);
      System.out.println();
      mainCashier.setPrice2("900");
      Cashier c3 = new Cashier(mainCashier);
      mainCashier.setPrice3("9001");
      System.out.println();
      mainCashier.delObserver(c2);
      System.out.println();
      mainCashier.setPrice2("101");
   }

}

_________________
kurayami no kagi  _____    
Back to top Go down
View user profile https://yotamarker.justforum.net
Admin
Admin
avatar

Posts : 92
Join date : 2011-08-01

PostSubject: builder design pattern JAVA   Mon Feb 19, 2018 9:07 am


the following design pattern works like naruto kage bunshin. the nested pcbuilder bunshin is a replica class that gets all the needed data
for the pc class constructor. after the pcbuilder fields are set(see main), his assembler methode passes its data to the pc constructor, which are final
and can be set once only.
the jutsu can be used for graphic objecs and other permanant data field objects.

add class :

Code:
public class PC {
   private final String cpu, gpu;
   private final int ram, ssd, hdd;
   
   private PC(PCBuilder pBuilder) {
      this.cpu = pBuilder.cpu;
      this.gpu = pBuilder.gpu;
      this.ram = pBuilder.ram;
      this.ssd = pBuilder.ssd;
      this.hdd = pBuilder.hdd;   
   }
   
   @Override
   public String toString() {
      return "PC [cpu=" + cpu + ", gpu=" + gpu + ", ram=" + ram + ", ssd=" + ssd + ", hdd=" + hdd + "]";
   }

   static class PCBuilder{
      private String cpu, gpu; // replica
      private int ram, ssd, hdd; // replica
      
      public PCBuilder setCpu(String cpu) {
         this.cpu = cpu;
         return this;
      }
      public PCBuilder setGpu(String gpu) {
         this.gpu = gpu;
         return this;
      }
      public PCBuilder setRam(int ram) {
         this.ram = ram;
         return this;
      }
      public PCBuilder setSsd(int ssd) {
         this.ssd = ssd;
         return this;
      }
      public PCBuilder setHdd(int hdd) {
         this.hdd = hdd;
         return this;
      }
      
      public PC assemble() {
         return new PC(this);
      }
      
   }
}

main
Code:
public class Main {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      PC HPpc = new PC.PCBuilder().setCpu("I7 7700K")
            .setGpu("nVidia 1060GTX") // == PCBuilder.setGPU()
            .setRam(5)
            .assemble();
      System.out.println(HPpc);
      
      // Second Option:
      
      PC.PCBuilder pBuilder = new PC.PCBuilder();
      pBuilder.setCpu("I5 lame");
      pBuilder.setGpu("Raedon lame");
      pBuilder.setRam(4); // Oh no.
      pBuilder.setHdd(500); // Don't do it to yourself
      
      PC lenovo = pBuilder.assemble();
      System.out.println(lenovo);
   }

}

_________________
kurayami no kagi  _____    
Back to top Go down
View user profile https://yotamarker.justforum.net
Admin
Admin
avatar

Posts : 92
Join date : 2011-08-01

PostSubject: inner classes   Mon Feb 19, 2018 1:09 pm


inner class, a class beefup henshin power up.
add class :
Code:
public class Dog {
   String dogFood = "Meat";

   public Dog() {
      System.out.println("New Dog Created!");
      
   }
   
   public void printDogFood() {
      System.out.println(dogFood);
      
   }
   
   public class DogHelper{
      String dogName = "Sami";
      public DogHelper() {
         System.out.println("New DogHelper created!");
      }
      
      void printDogName() {
         System.out.println(this.dogName);
      }
      
   }
}
main :
Code:
public class Main {

   public static void main(String[] args) {
         Dog sami = new Dog();
         sami.printDogFood();
         
         Dog.DogHelper dogHelper = sami.new DogHelper(); // summon inner class
         
         dogHelper.printDogName();
         

   }

}
ex 2 :
setting the inner class modifier from public to private makes it usable only to its wrapper class :
add class :
Code:
public class serverManager {
   public serverManager() {
      System.out.println("New ServerManager Created!"); // constructor
   }
   
   public void connect() {
      SSLHandler sHandler = new SSLHandler();
      sHandler.i = 15;
      System.out.println("Establishing connection...");
      System.out.println("Creating SSL Signature...");
      
      System.out.println("Using SSL Signature to make server requests");
   }
   
   
   private class SSLHandler{
      private int i = 5;
      public SSLHandler() {
         System.out.println("New SSLHandler created!");
      }
   }   
}
main :
Code:
public class Main {

   public static void main(String[] args) {
         
         serverManager serverManager = new serverManager();
          serverManager.connect();
   }

}

_________________
kurayami no kagi  _____    
Back to top Go down
View user profile https://yotamarker.justforum.net
 
JAVA design patterns
Back to top 
Page 1 of 1
 Similar topics
-
» Verify command when used with Java,JUNIT and Eclipse
» opera browser not loading the page using selenium Rc(Java)
» How to design UI testing using selenium 2
» JAVA tunnel Based UAE Solution Available in Just $149 @ VoIP
» [Help] Java input Username field as a timestamp

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: chit chat-
Jump to: