diff --git a/project_1/adapter_design_pattern/Main.java b/project_1/adapter_design_pattern/Main.java new file mode 100644 index 000000000..27e648046 --- /dev/null +++ b/project_1/adapter_design_pattern/Main.java @@ -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(); + } +} \ No newline at end of file diff --git a/project_1/singleton_design_pattern/SocketClass.java b/project_1/singleton_design_pattern/SocketClass.java new file mode 100644 index 000000000..bddd9ff2c --- /dev/null +++ b/project_1/singleton_design_pattern/SocketClass.java @@ -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(); + } +} \ No newline at end of file diff --git a/project_1/singleton_design_pattern/SocketTester.java b/project_1/singleton_design_pattern/SocketTester.java new file mode 100644 index 000000000..4723e3790 --- /dev/null +++ b/project_1/singleton_design_pattern/SocketTester.java @@ -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(); + } + } +} \ No newline at end of file