Multiple Design Patterns - Furniture Shopping #64
Sarfinaa
started this conversation in
Show and tell
Replies: 4 comments
-
//implementing multiple design patterns
//factoryType
//build
//singleton
//abstractFactory
abstract class Varient {
abstract String getName();
}
class ModernFurniture extends Varient {
private static volatile ModernFurniture modernInstance = null; // singleton
private ModernFurniture() {
}
static ModernFurniture getModernFurnitureInstance() {
if (modernInstance == null) {
modernInstance = new ModernFurniture();
}
return modernInstance;
}
public String getName() {
return "Modern";
}
}
class VictorianFurniture extends Varient {
private static volatile VictorianFurniture victorianInstance = null;
private VictorianFurniture() {
}
static VictorianFurniture getVictorianFurnitureInstance() {
if (victorianInstance == null) {
victorianInstance = new VictorianFurniture();
}
return victorianInstance;
}
public String getName() {
return "Victorian";
}
}
abstract class Product {
Varient varient;
Product(Varient varient) {
this.varient = varient;
}
boolean hasLegs() {
return !(this.varient.getName().equals("Modern"));
}
}
class Chair extends Product {
Chair(Varient varient) {
super(varient);
}
public void sitOn() {
System.out.println("I am sat on a " + varient.getName() + " Chair");
}
}
class Sofa extends Product {
Sofa(Varient varient) {
super(varient);
}
public void sitOn() {
System.out.println("I am sat on a " + varient.getName() + " Sofa");
}
}
class CoffeeTable extends Product {
CoffeeTable(Varient varient) {
super(varient);
}
public void keepOn() {
System.out.println("I am sat on a " + varient.getName() + " Coffee Table");
}
}
class FurnitureFactory {
Varient varient;
public FurnitureFactory(Varient varient) {
this.varient = varient;
}
public Chair createChair() {
return new Chair(varient);
}
public Sofa createSofa() {
return new Sofa(varient);
}
public CoffeeTable createCoffeeTable() {
return new CoffeeTable(varient);
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(String furnitureType) {
FurnitureFactory factory = getFactory(furnitureType);
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
FurnitureFactory getFactory(String factoryType) {
Varient varient = factoryType.equals("Modern") ? ModernFurniture.getModernFurnitureInstance()
: VictorianFurniture.getVictorianFurnitureInstance();
return new FurnitureFactory(varient);
}
void useFurniture() {
this.chair.sitOn();
this.sofa.sitOn();
this.coffeeTable.keepOn();
System.out.println("Chair: " + this.chair.hasLegs());
System.out.println("Sofa: " + this.sofa.hasLegs());
System.out.println("Coffee Table: " + this.coffeeTable.hasLegs());
}
}
public class BridgeModelDemo {
public static void main(String[] args) {
Client client = new Client("Modern");
Client client1 = new Client("Victorian");
client.useFurniture();
client1.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
package designPatterns;
interface Variant {
String getTypeName();
}
class Modern implements Variant {
/**
* 1. Singleton Pattern
*/
private static volatile Modern modernInstance = null;
private Modern() {
}
static Modern getModernInstance() {
if (modernInstance == null) {
modernInstance = new Modern();
}
return modernInstance;
}
public String getTypeName() {
return "Modern";
}
}
class Victorian implements Variant {
/**
* 1. Singleton Pattern
*/
private static volatile Victorian victorianInstance = null;
private Victorian() {
}
static Victorian getVictorianInstance() {
if (victorianInstance == null) {
victorianInstance = new Victorian();
}
return victorianInstance;
}
public String getTypeName() {
return "Victorian";
}
}
/**
* Builder Example Abstract Component is Product and Implementation is Variant
*/
abstract class Product {
Variant type;
Product(Variant type) {
this.type = type;
}
abstract String getProductName();
boolean hasLegs() {
return !(this.type.getTypeName().equals("Modern"));
}
void displayLegsStatus() {
System.out.print(this.type.getTypeName() + " " + getProductName() + " ");
if (this.hasLegs()) {
System.out.println("has legs.");
} else {
System.out.println("does not have legs");
}
}
}
class Chair extends Product {
Chair(Variant type) {
super(type);
}
String getProductName() {
return "Chair";
}
void sitOn() {
System.out.println("i sat on " + type.getTypeName() + " " + getProductName());
}
}
class Sofa extends Product {
Sofa(Variant type) {
super(type);
}
String getProductName() {
return "Sofa";
}
void sitOn() {
System.out.println("i sat on " + type.getTypeName() + " " + getProductName());
}
}
class CoffeeTable extends Product {
CoffeeTable(Variant type) {
super(type);
}
String getProductName() {
return "Table";
}
void keepOn() {
System.out.println(type.getTypeName() + " " + getProductName() + " is kept on floor");
}
}
/**
* Factory Method
*/
class FurnitureFactory {
Variant type;
FurnitureFactory(Variant type) {
this.type = type;
}
Chair createChair() {
return new Chair(this.type);
}
Sofa createSofa() {
return new Sofa(this.type);
}
CoffeeTable createCoffeeTable() {
return new CoffeeTable(this.type);
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
/**
* Abstract factory method
* @param factoryType
* @return
*/
FurnitureFactory getFactory(String factoryType) {
Variant variant = factoryType.equals("Modern") ? Modern.getModernInstance() : Victorian.getVictorianInstance();
return new FurnitureFactory(variant);
}
Client(String furnitureType) {
FurnitureFactory factory = getFactory(furnitureType);
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
void useFurniture() {
this.chair.sitOn();
this.sofa.sitOn();
this.coffeeTable.keepOn();
this.chair.displayLegsStatus();
this.sofa.displayLegsStatus();
this.coffeeTable.displayLegsStatus();
}
}
public class DesginPatternAllDesigns {
public static void main(String[] args) {
Client clientModern = new Client("Modern");
Client clientVictorian = new Client("Victorian");
clientModern.useFurniture();
clientVictorian.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
package design_patterns.structural.bridge_pattern;
interface Variants {
String getProductTypeName();
}
// Singelton Pattern
class Modern implements Variants {
private static volatile Modern modern = null;
private Modern() {
}
static Modern getInstanceModern() {
if (modern == null) {
return new Modern();
}
return modern;
}
@Override
public String getProductTypeName() {
return "Modern";
}
}
// Singleton Pattern
class Victorian implements Variants {
private static volatile Victorian victorian = null;
private Victorian() {
}
static Victorian getInstanceVictorian() {
if (victorian == null) {
return new Victorian();
}
return victorian;
}
@Override
public String getProductTypeName() {
return "Victorian";
}
}
// Builder Pattern
abstract class Product {
Variants variants;
String name;
protected Product(Variants variants, String name) {
this.variants = variants;
this.name = name;
}
abstract boolean hasLegs();
}
class Chair extends Product {
public Chair(Variants variants) {
super(variants, "Chair");
}
@Override
public boolean hasLegs() {
return (variants.getProductTypeName().equals("Victorian"));
}
void sitOn() {
System.out.println("I sat on a " + this.variants.getProductTypeName() + " " + this.name);
}
}
class Sofa extends Product {
public Sofa(Variants variants) {
super(variants, "Sofa");
}
@Override
public boolean hasLegs() {
return (variants.getProductTypeName().equals("Victorian"));
}
void sitOn() {
System.out.println("I sat on a " + this.variants.getProductTypeName() + " sofa");
}
}
class CoffeeTable extends Product {
public CoffeeTable(Variants variants) {
super(variants, "CoffeeTable");
}
@Override
public boolean hasLegs() {
return (variants.getProductTypeName().equals("Victorian"));
}
void keepOn() {
System.out.println("I keep things on a " + this.variants.getProductTypeName() + "coffee table");
}
}
class FurnitureFactory {
Variants variants;
public FurnitureFactory(Variants variants) {
this.variants = variants;
}
public Chair createChair() {
return new Chair(variants);
}
public Sofa createSofa() {
return new Sofa(variants);
}
public CoffeeTable createCoffeeTable() {
return new CoffeeTable(variants);
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
// Factory Pattern
FurnitureFactory getFactory(String factoryType) {
Variants variant = factoryType.equals("Modern") ? Modern.getInstanceModern() : Victorian.getInstanceVictorian();
return new FurnitureFactory(variant);
}
// Abstract Factory Pattern
Client(String furnitureType) {
FurnitureFactory factory = getFactory(furnitureType);
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
void useFurniture() {
this.chair.sitOn();
this.sofa.sitOn();
this.coffeeTable.keepOn();
System.out.println("Chair has legs: " + this.chair.hasLegs());
System.out.println("Sofa has legs: " + this.sofa.hasLegs());
System.out.println("CoffeeTable has legs: " + this.coffeeTable.hasLegs());
}
}
public class BridgePatternDemo {
public static void main(String[] args) {
Client clientModern = new Client("Modern");
Client clientVictorian = new Client("Victorian");
clientModern.useFurniture();
System.out.println();
clientVictorian.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
abstract class Varient {
abstract String getName();
}
class ModernFurniture extends Varient {
private static volatile ModernFurniture modernInstance = null; // singleton
private ModernFurniture() {
}
static ModernFurniture getModernFurnitureInstance() {
if (modernInstance == null) {
modernInstance = new ModernFurniture();
}
return modernInstance;
}
public String getName() {
return "Modern";
}
}
class VictorianFurniture extends Varient {
private static volatile VictorianFurniture victorianInstance = null;
private VictorianFurniture() {
}
static VictorianFurniture getVictorianFurnitureInstance() {
if (victorianInstance == null) {
victorianInstance = new VictorianFurniture();
}
return victorianInstance;
}
public String getName() {
return "Victorian";
}
}
abstract class Product {
Varient varient;
Product(Varient varient) {
this.varient = varient;
}
boolean hasLegs() {
return !(this.varient.getName().equals("Modern"));
}
}
class Chair extends Product {
Chair(Varient varient) {
super(varient);
}
public void sitOn() {
System.out.println("I am sat on a " + varient.getName() + " Chair");
}
}
class Sofa extends Product {
Sofa(Varient varient) {
super(varient);
}
public void sitOn() {
System.out.println("I am sat on a " + varient.getName() + " Sofa");
}
}
class CoffeeTable extends Product {
CoffeeTable(Varient varient) {
super(varient);
}
public void keepOn() {
System.out.println("I am sat on a " + varient.getName() + " Coffee Table");
}
}
class FurnitureFactory {
Varient varient;
public FurnitureFactory(Varient varient) {
this.varient = varient;
}
public Chair createChair() {
return new Chair(varient);
}
public Sofa createSofa() {
return new Sofa(varient);
}
public CoffeeTable createCoffeeTable() {
return new CoffeeTable(varient);
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(String furnitureType) {
FurnitureFactory factory = getFactory(furnitureType);
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
FurnitureFactory getFactory(String factoryType) {
Varient varient = factoryType.equals("Modern") ? ModernFurniture.getModernFurnitureInstance()
: VictorianFurniture.getVictorianFurnitureInstance();
return new FurnitureFactory(varient);
}
void useFurniture() {
this.chair.sitOn();
this.sofa.sitOn();
this.coffeeTable.keepOn();
System.out.println("Chair: " + this.chair.hasLegs());
System.out.println("Sofa: " + this.sofa.hasLegs());
System.out.println("Coffee Table: " + this.coffeeTable.hasLegs());
}
}
public class BridgeModelDemo {
public static void main(String[] args) {
Client client = new Client("Modern");
Client client1 = new Client("Victorian");
client.useFurniture();
client1.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Multiple Design Patterns for a Task
Design Patterns Involved
Code
Beta Was this translation helpful? Give feedback.
All reactions