public static void main(String[] args) { Singleton s1 = Singleton.createSingleton(); // Замена вызова конструктора на вызов статического метода. Singleton s2 = Singleton.createSingleton(); System.out.println(s1 == s2); // Будет ссылаться на один обьект. }
public static void main(String[] args) throws IOException, ClassNotFoundException { Singleton instance = Singleton.getInstance(); // Serializing the singleton instance ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("singleton.tmp")); oos.writeObject(instance); oos.close(); // Recreating the instance by reading the serialized object data store ObjectInputStream ois = new ObjectInputStream(new FileInputStream("singleton.tmp")); Singleton singleton = (Singleton) ois.readObject(); ois.close(); // Recreating the instance AGAIN by reading the serialized object data store ObjectInputStream ois2 = new ObjectInputStream(new FileInputStream("singleton.tmp")); Singleton singleton1 = (Singleton) ois2.readObject(); ois2.close(); // The singleton behavior have been broken System.out.println("Instance reference check : " + singleton.getInstance()); System.out.println("Instance reference check : " + singleton1.getInstance()); System.out.println("========================================================="); System.out.println("Object reference check : " + singleton); System.out.println("Object reference check : " + singleton1); }
public static void main(String[] args) { Singleton.getInstance().printSingleton(); Singleton.getInstance().printSingleton(); Singleton.getInstance().printSingleton(); Singleton.getInstance().printSingleton(); Singleton.getInstance().printSingleton(); }
public static void main(String[] args) { Singleton.getInstance(); Singleton.getInstance(); Singleton.getInstance(); Singleton.getInstance(); System.out.println(Singleton.getData()); }
public static void ImprimirFiguras() { Singleton x = Singleton.getInstance(); for (FiguraGeometrica figura : x.getFiguras()) { System.out.println(figura); System.out.println(); } }
public void run() { Singleton threadSingleton = Singleton.getInstance(); synchronized (BreakSingleton.class) { if (singleton == null) singleton = threadSingleton; } System.out.println("Thread 1st Object Hash:" + singleton.hashCode()); System.out.println("Thread 2nd Object Hash:" + threadSingleton.hashCode()); }
public static void main(String[] args) { // TODO Auto-generated method stub Singleton s = new Singleton(); s.getInstance(); System.out.println(s.getInstance().hashCode()); s = new Singleton(); s.getInstance(); System.out.println(s.getInstance().hashCode()); }
public static void main(String[] args) { Singleton singletonNesne1 = Singleton.getInstance(); Singleton singletonNesne2 = Singleton.getInstance(); // Singleton singletonNesne2=new Singleton(); // yukarýdaki kod hata verir. if (singletonNesne1 == singletonNesne2) System.out.println("Nesneler Ayný!"); else System.out.println("Nesneler Farklý!"); }
@Test public void test1() { System.out.println(Singleton.getInstance()); System.out.println(Singleton.getInstance()); System.out.println(); System.out.println(Singleton2.getInstance()); System.out.println(Singleton2.getInstance()); }
public static void main(String[] args) { Singleton s1 = Singleton.getInstance(0); Singleton s2 = Singleton.getInstance(1); Singleton s3 = Singleton.getInstance(2); s1.printData(); s2.printData(); s3.printData(); }
public static void main(String[] args) { System.out.println("Start."); Singleton obj1 = Singleton.getInstance(); Singleton obj2 = Singleton.getInstance(); if (obj1 == obj2) { System.out.println("obj1 と obj2 は同じインスタンスです。"); } else { System.out.println("obj1 と obj2 は同じインスタンスではありません。"); } System.out.println("End."); }
/** Initialize the contents of the frame. */ private void initialize() { Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); frame = new JFrame(); frame.setSize(1050, 781); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); int x = (dim.width - frame.getSize().width) / 2; int y = (dim.height - frame.getSize().height) / 2; frame.setLocation(x, y); // top AppMenu mn = new AppMenu(); JPanel container = new JPanel(); JPanel top = new JPanel(); preview = new Canvas("PREVIEW", false, false, new Dimension(200, 200)); top.add(preview); BorderLayout border = new BorderLayout(); container.setLayout(border); container.add(mn, BorderLayout.NORTH); container.add(top, BorderLayout.SOUTH); frame.getContentPane().add(container, BorderLayout.NORTH); // left JPanel leftContainner = new JPanel(); BorderLayout leftLyout = new BorderLayout(); leftContainner.setLayout(leftLyout); JPanel left = new JPanel(); move = new Canvas("MOVE", true, false, new Dimension(500, 500)); left.add(move); leftContainner.add(left, BorderLayout.SOUTH); frame.getContentPane().add(leftContainner, BorderLayout.WEST); // right JPanel rightContainner = new JPanel(); BorderLayout rightLyout = new BorderLayout(); rightContainner.setLayout(rightLyout); JPanel right = new JPanel(); zoom = new Canvas("ZOOM", false, true, new Dimension(500, 500)); right.add(zoom); rightContainner.add(right, BorderLayout.SOUTH); frame.getContentPane().add(rightContainner, BorderLayout.EAST); Singleton.getInstance().getPreview().register(preview); Singleton.getInstance().getEdit().register(move); Singleton.getInstance().getEdit().register(zoom); preview.setSubject(Singleton.getInstance().getPreview()); move.setSubject(Singleton.getInstance().getEdit()); zoom.setSubject(Singleton.getInstance().getEdit()); Singleton.getInstance().getWarpper().setTopCanvas(this.preview); Singleton.getInstance().getWarpper().setLeftCanvas(this.move); Singleton.getInstance().getWarpper().setRightCanvas(this.zoom); }
@Override public void currentChanged(ChangeEvent ce) { DataPointEntry currentEntry = Singleton.getState().getCurrenDataEntry(); if (currentEntry == null) { modelValueField.setText("--"); snpLocationField.setText("--"); pValueField.setText("--"); rsIdField.setText("--"); intronExonField.setText("--"); regulomeField.setText("--"); snpGeneField.setText("--"); } else { // System.out.println("currentChanged: " + currentEntry); SNP snp = currentEntry.getSnp(); Model model = currentEntry.getModel(); DataSet dataSet = currentEntry.getDataSet(); Double negLogPValue = dataSet.getPvalFromSnpModel(snp, model); modelValueField.setText(model.toString()); DecimalFormat formatter = new DecimalFormat("#,###"); // snpLocationField.setText(snp.getLoc() + ""); snpLocationField.setText(formatter.format(snp.getLoc())); String outputStr = myFormatter.format(negLogPValue); pValueField.setText(outputStr); rsIdField.setText("rs" + snp.getRsId()); intronExonField.setText(snp.getIntronExon().toString()); regulomeField.setText(snp.getRegulome()); snpGeneField.setText(snp.getAssociatedGene()); } }
public boolean createPhysicalBoatProxy(String host) { System.out.println("Creating physical boat proxy"); instance.createBoatProxy(host, Material.CYAN); return true; }
public static void breakSingletonWithSerialization() { System.out.println(); final String fileName = "singleton.ser"; // getting singleton instance Singleton instanceOne = Singleton.getInstance(); instanceOne.setValue(10); try { // Serialize to a file ObjectOutput objectOutput = new ObjectOutputStream(new FileOutputStream(fileName)); objectOutput.writeObject(instanceOne); objectOutput.close(); // now changed the value of singleton instanceOne.setValue(20); // Serialize to a file ObjectInput objectInput = new ObjectInputStream(new FileInputStream(fileName)); Singleton instanceTwo = (Singleton) objectInput.readObject(); objectInput.close(); System.out.println("Instance One Value= " + instanceOne.getValue()); System.out.println("Instance Two Value= " + instanceTwo.getValue()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
@Override public void run() { // TODO Auto-generated method stub try { System.out.println(Singleton.getInstance(a).getName()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public FormAddFolderFromButton(IHMcodeMirror IHM) { super(); this.IHMcodeMirror = IHM; WindowFactory.setParameters(this, "Create New Folder", 350, 280); this.addCloseClickHandler( new CloseClickHandler() { @Override public void onCloseClick(CloseClientEvent closeClientEvent) { hide(); } }); DynamicForm form = new DynamicForm(); form.setPadding(5); form.setLayoutAlign(VerticalAlignment.BOTTOM); textItem = new TextItem(); textItem.setTitle("Name"); textItem.setRequired(true); IButton ok = new IButton("Ok"); form.setFields(textItem); this.addItem(form); this.addItem(ok); treeGrid = Singleton.getInstance().loadFileSystemLight(IHMcodeMirror.getAbstractItemRoot()); this.addItem(treeGrid); ok.addClickHandler( new ClickHandler() { @Override public void onClick(ClickEvent clickEvent) { AbstractItem item = (AbstractItem) treeGrid.getSelectedRecord().getAttributeAsObject("abstractItem"); item.setPath(item.getPath() + textItem.getEnteredValue()); repositoryToolsServices.createFolderIntoLocalRepository( item, new AsyncCallback<Boolean>() { @Override public void onFailure(Throwable throwable) {} @Override public void onSuccess(Boolean bool) { hide(); Singleton.getInstance() .loadFileSystem( IHMcodeMirror.getAbstractItemRoot(), IHMcodeMirror.getSystemFileRoot()); } }); } }); }
public void update_profile(View view) { progressDialog.setMessage("Loading"); progressDialog.show(); phone_number = (TextView) findViewById(R.id.phone_number_update); location = (TextView) findViewById(R.id.location); phone_number1 = phone_number.getText().toString(); location1 = location.getText().toString(); if (phone_number1.length() < 10 || location1.length() < 3) { Toast.makeText(this, "Please ensure you have inserted all your details", Toast.LENGTH_LONG) .show(); } else { try { StringRequest theReq = new StringRequest( Request.Method.POST, "http://www.samuelagbede.com/coza_social/update_profile_details.php", new Response.Listener<String>() { @Override public void onResponse(String response) { progressDialog.hide(); Toast.makeText(Featured.this, "Successfully updated", Toast.LENGTH_LONG).show(); phone_number.setText(""); location.setText(""); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { progressDialog.hide(); Toast.makeText( Featured.this, "Not updated. Check your internet connection please", Toast.LENGTH_LONG) .show(); } }) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("username", username1); params.put("phone_number", phone_number1); params.put("location", location1); return params; } }; Singleton.getInstance(this).addToRequestQueue(theReq); } catch (Exception e) { e.printStackTrace(); } } }
public static void main(String[] args) { // test for EasySingleton EasySingleton es = EasySingleton.INSTANCE; out.println(es); // Test for DoubleCheckedLockingSingleton DoubleCheckedLockingSingleton dels = DoubleCheckedLockingSingleton.getInstance(); out.println(dels); // Test for Singleton Singleton singleton = Singleton.getSingleton(); out.println(singleton); }
public static void main(String[] args) { Singleton s = Singleton.getReference(); System.out.println(s.getValue()); Singleton s2 = Singleton.getReference(); s2.setValue(9); System.out.println(s.getValue()); try { // Can't do this: compile-time error. // Singleton s3 = (Singleton)s2.clone(); } catch (Exception e) { e.printStackTrace(System.err); } }
public static void main(String[] args) { System.out.println("Start der Tests..."); Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); if (s1 == null) { System.out.println("Fehler: getInstance liefert beim ersten Aufruf null zurueck."); return; } if (s2 == null) { System.out.println("Fehler: getInstance liefert beim zweiten Aufruf null zurueck."); return; } s1.i = 17; if (s2.i == 17) System.out.println("Ihre Singleton-Implementierung ist korrekt."); else System.out.println("Ihre Singleton-Implementierung ist fehlerhaft."); System.out.println("Testende"); }
public void comments_enter(View view) { progressDialog.setMessage("Please hold on"); progressDialog.show(); final TextView comments = (TextView) findViewById(R.id.devo_comments); comments1 = comments.getText().toString(); final String devo_id = Devotional.devotional_id; if (comments1.equals("") || comments1.length() < 4) { progressDialog.hide(); Toast.makeText(this, "Please enter a valid comment", Toast.LENGTH_LONG).show(); } else { connectUrl = "http://www.samuelagbede.com/coza_social/insert_devotional_comments.php"; StringRequest jor = new StringRequest( Request.Method.POST, connectUrl, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d("Comments Inserted", response); progressDialog.hide(); Toast.makeText(Featured.this, "Comments entered successfully", Toast.LENGTH_LONG) .show(); comments.setText(" "); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("Error", error.toString()); } }) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("username", username1); params.put("comments", comments1); params.put("email_add", email_address1); params.put("devotional", devo_id); return params; } }; Singleton.getInstance(this).addToRequestQueue(jor); } }
public static void breakSingletonWithClone() { System.out.println(); final String fileName = "singleton.ser"; // getting singleton instance Singleton singleton = Singleton.getInstance(); singleton.setValue(10); try { Singleton clonedObject = (Singleton) singleton.clone(); System.out.println(clonedObject); System.out.println(clonedObject.hashCode()); } catch (CloneNotSupportedException e) { e.printStackTrace(); } }
public static void breakSingletonWithReflection() { System.out.println(); Singleton instanceOne = Singleton.getInstance(); instanceOne.printHashCode(); System.out.println(instanceOne.hashCode()); Singleton instanceTwo = null; try { Constructor<?>[] constructors = Singleton.class.getDeclaredConstructors(); for (Constructor<?> constructor : constructors) { // should break the rule here. constructor.setAccessible(true); instanceTwo = (Singleton) constructor.newInstance(); break; } } catch (Exception e) { e.printStackTrace(); } instanceTwo.printHashCode(); System.out.println(instanceTwo.hashCode()); }
public static void main(String[] args) { Singleton s1 = instance; Singleton s2 = instance; System.out.println(s1.hashCode() + " " + s2.hashCode()); }
@Test public void singletonTest() { // create singleton instances final Singleton singleton1 = Singleton.getInstance(); final Singleton singleton2 = Singleton.getInstance(); final Singleton singleton3 = Singleton.getInstance(); // assert that the random number is the same thereby // ensuring we are dealing with a single object instance final int rand = singleton1.getRand(); assertEquals(rand, singleton2.getRand()); assertEquals(rand, singleton3.getRand()); // set a string message on a singleton final String message = "Singleton2 message"; singleton2.setMessage(message); // read the string message from the singletons assertEquals(message, singleton1.getMessage()); assertEquals(message, singleton3.getMessage()); }
void setConsole(OperatorConsole c) { instance.setConsole(c); }
public ArrayList<Marker> getMarkers() { return instance.getMarkers(); }
public boolean createVBSBoatProxy(int port) { instance.createBoatProxy("http://localhost:" + port, Material.YELLOW); return true; }
public boolean createSimulatedBoatProxy(int port) { instance.createBoatProxy("http://localhost:" + port, Material.CYAN); return true; }