Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions project_1/adapter_design_pattern/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Motor {
public void spin(){
System.out.println("the rotor is spinning");
}
}

class Wheel {
public void moveCar(){
System.out.println("the wheel moves the car in a certain direction");
}
}

class TransmissionBeltAdapter extends Wheel {
Motor motor;
public TransmissionBeltAdapter(Motor motor){
// reference to the object we are adapting (i.e. the motor)
this.motor = motor;
}

public void moveCar () {
motor.spin();
}
}

class Main
{
public static void main(String args[])
{
Motor motor = new Motor();
Wheel wheel = new Wheel();

Wheel transmissionBeltAdapter = new TransmissionBeltAdapter(motor);

motor.spin();
wheel.moveCar();
// the adapter will do the same thing as the motor, it will spin the wheel
System.out.println("The transmission belt will transfer the energy of the motor to the wheel while");
transmissionBeltAdapter.moveCar();
}
}
38 changes: 38 additions & 0 deletions project_1/singleton_design_pattern/SocketClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package singleton_design_pattern;
import java.io.Serializable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectInput;

public class SocketClass implements Serializable {

private static volatile SocketClass sSoleInstance;

//private constructor.
private SocketClass(){

//Prevent from the reflection api.
if (sSoleInstance != null){
throw new RuntimeException("Use getInstance() method to get the single instance of this class.");
}
}

public static SocketClass getInstance() {
if (sSoleInstance == null) { //if there is no instance available create one
synchronized (SocketClass.class) {
if (sSoleInstance == null) sSoleInstance = new SocketClass();
}
}

return sSoleInstance;
}

//Make singleton from serialize and deserialize operation.
protected SocketClass readResolve() {
return getInstance();
}
}
38 changes: 38 additions & 0 deletions project_1/singleton_design_pattern/SocketTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package singleton_design_pattern;
import java.io.Serializable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectInput;


public class SocketTester {
public static void main(String[] args) {

try {
SocketClass instance1 = SocketClass.getInstance();
ObjectOutput out = null;

out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
out.writeObject(instance1);
out.close();

//deserialize from file to object
ObjectInput in = new ObjectInputStream(new FileInputStream("filename.ser"));
SocketClass instance2 = (SocketClass) in.readObject();
in.close();

//makes sure the object was not instantiated twice from deserialization
instance2 = instance2.readResolve();

System.out.println("instance1 hashCode=" + instance1.hashCode());
System.out.println("instance2 hashCode=" + instance2.hashCode());

} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}