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;
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
Admin Admin
Posts : 144 Join date : 2011-08-01
Subject: 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;
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
Admin Admin
Posts : 144 Join date : 2011-08-01
Subject: 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!"); }
edo tensei no jutsu student: name age address grades
managementSys
students table DB BackUp DB // Mcode state marker ? working state ? changed hashset create changed hashset affected del student ch2 add grade(student name) ch avgAll clone student saveAll changes (clone STDB to BUDB) clear ch restore (clone BUDB to STDB) clear ch
the following (mini)project also contains the codes to: iterate a dictionary (hashtable) referring to a value in a dictionary using the shallow clone jutsu using deep clone method (see inner class student) by overriding the super class objects clone methode a simple save or restore algorithm
public class StudentManagementSys { // Student Data Base : Hashtable<String, Student> SDB = new Hashtable<>(); // BackUpDataBase : Hashtable<String, Student> BUDB = new Hashtable<>(); HashSet<String> changedKeys = new HashSet<>(); HashSet<String> deleteddKeys = new HashSet<>(); public void createStudent(String name, String address, int age) { SDB.put(name, new Student(name, address, age)); changedKeys.add(name); } public void delStudent(String name) { if(!SDB.containsKey(name)) {SDB.remove(name);deleteddKeys.add(name); changedKeys.remove(name); }
} public void addGrade(String name, int grade) { if(SDB.containsKey(name)) {Student temp = SDB.get(name);temp.addGrade(grade);}//shallow copy jutsu } public double getAvg() { int sum = 0; int counter = 0; Set<String> setOfStudents = SDB.keySet(); for(String key : setOfStudents) { Student shallowStudent = SDB.get(key); for (int x : shallowStudent.grades) { sum += x; counter ++; } } return (double)sum / (double)counter; } public void saveAll() { // pukhamize SDB to BUBD for (String string : deleteddKeys) { BUDB.remove(string); } for (String string : changedKeys) { BUDB.put(string, SDB.get(string).clone()); } deleteddKeys.clear(); changedKeys.clear(); } public void restore() { // pukhamize BUBD to SDB for (String string : deleteddKeys) { SDB.put(string, BUDB.get(string).clone()); } for (String string : changedKeys) { SDB.put(string, BUDB.get(string).clone()); } deleteddKeys.clear(); changedKeys.clear(); } public class Student implements Cloneable{ private String name,address; private int age; private List<Integer> grades = new ArrayList<>(); public Student(String name, String address, int age) { super(); this.name = name; this.address = address; this.age = age; } private Student(List<Integer> l1, String name, String address, int age) { this.grades = new ArrayList<>(); this.grades.addAll(l1); this.name = name; this.address = address; this.age = age;
} public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void addGrade(int grade) { grades.add(grade); } @Override protected Student clone() { // TODO Auto-generated method stub return new Student(this.grades, this.name, this.address, this.age); } } }
Simply put, a facade encapsulates a complex subsystem behind a simple interface. It hides much of the complexity and makes the subsystem easy to use. Also, if we need to use the complex subsystem directly, we still can do that; we aren’t forced to use the facade all the time.
java example :
Code:
public class CarEngineFacade { private static int DEFAULT_COOLING_TEMP = 90; private static int MAX_ALLOWED_TEMP = 50; private FuelInjector fuelInjector = new FuelInjector(); private AirFlowController airFlowController = new AirFlowController(); private Starter starter = new Starter(); private CoolingController coolingController = new CoolingController(); private CatalyticConverter catalyticConverter = new CatalyticConverter();