Skip to content

Instantly share code, notes, and snippets.

@AliN11
Created March 31, 2021 10:28
Show Gist options
  • Save AliN11/62550affb50af74d5af99739245f3cd7 to your computer and use it in GitHub Desktop.
Save AliN11/62550affb50af74d5af99739245f3cd7 to your computer and use it in GitHub Desktop.
interface Tablet {
switchOn();
}
interface Smartphone {
switchOn();
ring();
}
class SamsungSmartphone implements Smartphone {
public switchOn() {
console.log("Samsung Smartphone: Switching on");
}
public ring() {
console.log("Samsung Smartphone: Ringing");
}
}
class AppleSmartphone implements Smartphone {
public switchOn() {
console.log("Apple Smartphone: Switching on");
}
public ring() {
console.log("Apple Smartphone: Ringing");
}
}
class AppleTablet implements Tablet {
public switchOn() {
console.log("Apple Tablet: Switching on");
}
}
class SamsungTablet implements Tablet {
public switchOn() {
console.log("Samsung Tablet: Switching on");
}
}
interface DeviceFactory {
createSmartphone(): Smartphone;
createTablet(): Tablet;
}
class AppleFactory implements DeviceFactory {
public createSmartphone(): Smartphone {
return new AppleSmartphone();
}
public createTablet(): Tablet {
return new AppleTablet();
}
}
class SamsungFactory implements DeviceFactory {
public createSmartphone(): Smartphone {
return new SamsungSmartphone();
}
public createTablet(): Tablet {
return new SamsungTablet();
}
}
function client(factory: DeviceFactory) {
const smartphone = factory.createSmartphone();
smartphone.ring();
const tablet = factory.createTablet();
tablet.switchOn();
}
client(new SamsungFactory);
// Samsung Smartphone: Ringing
// Samsung Tablet: Switching on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment