Task - Design Patterns - Abstract Fatory #62
Replies: 31 comments 1 reply
-
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I kept coffee on a Victorian CoffeeTable");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("I kept coffee on a Modern CoffeeTable");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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("Coffee Table has legs:- " + this.coffeeTable.hasLegs());
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
System.out.println();
FurnitureFactory victorianFactory = new VictorianFactory();
Client client2 = new Client(victorianFactory);
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
package design_patterns.abstract_factory_pattern;
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
@Override
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
@Override
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - Coffee Table
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - Coffee Table
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void keepOn() {
System.out.println("I kept tea on a Victorian Coffee Table");
}
}
/**
* Product Family - Furniture
* Product - Coffee Table
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
@Override
public void keepOn() {
System.out.println("I kept tea on a Modern Coffee Table");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
@Override
public Chair createChair() {
return new VictorianChair();
}
@Override
public Sofa createSofa() {
return new VictorianSofa();
}
@Override
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
@Override
public Chair createChair() {
return new ModernChair();
}
@Override
public Sofa createSofa() {
return new ModernSofa();
}
@Override
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeetable;
Client(FurnitureFactory factory) {
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeetable = factory.createCoffeeTable();
}
void useFurniture() {
chair.sitOn();
sofa.sitOn();
coffeetable.keepOn();
System.out.println(chair.getClass().getSimpleName() + " has legs: " + chair.hasLegs());
System.out.println(sofa.getClass().getSimpleName() + " has legs: " + sofa.hasLegs());
System.out.println(coffeetable.getClass().getSimpleName() + " has legs: " + coffeetable.hasLegs());
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory1 = new VictorianFactory();
FurnitureFactory factory2 = new ModernFactory();
Client client1 = new Client(factory1);
Client client2 = new Client(factory2);
client1.useFurniture();
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
package designPatterns;
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I sat on a Victorian table");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I sat on a Modern table");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
void useFurniture() {
this.chair.sitOn();
this.sofa.sitOn();
this.coffeeTable.keepOn();
if(this.chair.hasLegs())
System.out.println("Chair has legs.");
if(this.sofa.hasLegs())
System.out.println("Sofa has legs.");
if(this.coffeeTable.hasLegs())
System.out.println("CoffeeTable has legs");
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
FurnitureFactory factory2 = new VictorianFactory();
Client client2 = new Client(factory2);
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
// TODO: Implement the variants of Sofa and CoffeeTable products
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
class VictorianCoffeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I am sat on a Victorian Coffee Table");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("I am sat on a Modern Cofeee Table");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return null;
}
public CoffeeTable createCoffeeTable() {
return null;
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
// TODO: Implement appropriate object creation
}
public CoffeeTable createCoffeeTable() {
// TODO: Implement appropriate object creation
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
void useFurniture() {
this.chair.sitOn();
// TODO: Uncomment and use Sofa and CoffeeTable
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());
// TODO: For each furniture, print whether it has legs or not
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
FurnitureFactory factory1 = new VictorianFactory();
Client client = new Client(factory);
Client client1 = new Client(factory1);
client.useFurniture();
client1.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
package design_patterns.creational;
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sitting on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sitting on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void sitOn() {
System.out.println("I am sitting on Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
@Override
public void sitOn() {
System.out.println("I am sitting on Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void keepOn() {
System.out.println("Victorian Coffee Table is kept on Floor");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void keepOn() {
System.out.println("Modern Coffee Table is kept on Floor");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
// TODO: Implement appropriate object creation
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
// TODO: Implement appropriate object creation
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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(legsStatus("Chair", this.chair.hasLegs()));
System.out.println(legsStatus("Sofa", this.sofa.hasLegs()));
System.out.println(legsStatus("Coffee Table", this.coffeeTable.hasLegs()));
}
String legsStatus(String type, boolean hasLegs) {
if (hasLegs) {
return type + " has legs.";
} else {
return type + " does not have legs";
}
}
}
public class AbstractFactoryEg {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
package DesignPattern;
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - CoffeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("keep on a Victorian CoffeeTable");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("keep on a Modern CoffeeTable");
}
}
// TODO: Implement the variants of Sofa and CoffeeTable products
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
// TODO: Implement appropriate object creation
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
// TODO: Implement appropriate object creation
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
void useFurniture() {
this.chair.sitOn();
this.sofa.sitOn();
this.coffeeTable.keepOn();
// TODO: For each furniture, print whether it has legs or not
System.out.println(" chair has leg? "+this.chair.hasLegs());
System.out.println(" sofa has legs? "+this.sofa.hasLegs());
System.out.println(" coffeeTable has legs? "+this.coffeeTable.hasLegs());
}
}
public class AbstractFactory {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
FurnitureFactory factory2 = new VictorianFactory();
Client client2 = new Client(factory2);
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
package design_patterns.abstract_factory;
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa{
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I keep things on modern coffeeTable");
}
}
/**
* Product Family - Furniture
* Product - Coffeetable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("I keep things on modern coffeeTable");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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 have: " + this.chair.hasLegs());
System.out.println("Sofa have: " + this.sofa.hasLegs());
System.out.println("CoffeeTable have: " + this.coffeeTable.hasLegs());
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
FurnitureFactory factory1 = new VictorianFactory();
Client client1 = new Client(factory1);
client1.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
package DSA.core;
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
// TODO: Implement the variants of Sofa and CoffeeTable products
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("coffee cups on a Victorian table");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("coffee on a Modern table");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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("has legs for chair :" + this.chair.hasLegs());
System.out.println("has legs for sofa :" + this.sofa.hasLegs());
System.out.println("has legs for coffeeTable :" + this.coffeeTable.hasLegs());
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory modernfactory = new ModernFactory();
FurnitureFactory victoranianfactory = new VictorianFactory();
Client client1 = new Client(modernfactory);
Client client2 = new Client(victoranianfactory);
client1.useFurniture();
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I kept coffee on a Victorian CoffeeTable");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("I kept coffee on a Modern CoffeeTable");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
void useFurniture() {
this.chair.sitOn();
this.sofa.sitOn();
this.coffeeTable.keepOn();
if (chair.hasLegs()) {
System.out.println("Chair has legs");
} else {
System.out.println("Chair has no legs");
}
if (sofa.hasLegs()) {
System.out.println("Sofa has legs");
} else {
System.out.println("Sofa has no legs");
}
if (coffeeTable.hasLegs()) {
System.out.println("Coffee Table has legs");
} else {
System.out.println("Coffee Table has no legs");
}
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void keepOn() {
System.out.println("Keep on Victorian Coffee Table");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
@Override
public void keepOn() {
System.out.println("Keep on Modern Coffee Table");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
void useFurniture() {
this.chair.sitOn();
this.sofa.sitOn();
this.coffeeTable.keepOn();
if (this.chair.hasLegs())
System.out.println("Chair has legs!");
if (this.sofa.hasLegs())
System.out.println("Sofa has legs!");
if (this.coffeeTable.hasLegs())
System.out.println("CoffeeTable has legs!");
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory1 = new ModernFactory();
Client client1 = new Client(factory1);
client1.useFurniture();
FurnitureFactory factory2 = new VictorianFactory();
Client client2 = new Client(factory2);
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void sitOn() {
System.out.println("I sat on Victorian Sofa");
}
}
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void sitOn() {
System.out.println("I sat on modern sofa");
}
}
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void keepOn() {
System.out.println("I keep on Victorian Coffee table");
}
}
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void keepOn() {
System.out.println("I keep on Modern coffee table");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
// TODO: Implement appropriate object creation
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
// TODO: Implement appropriate object creation
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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("Coffee table has legs " + this.coffeeTable.hasLegs());
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sitting on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sitting on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void sitOn() {
System.out.println("I am sitting on a victorian sofa");
}
}
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
@Override
public void sitOn() {
System.out.println("I am sitting on a modern sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void keepOn() {
System.out.println("coffee kept on a victorian coffee table");
}
}
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
@Override
public void keepOn() {
System.out.println("coffee kept on a modern coffee table");
}
}
// TODO: Implement the variants of Sofa and CoffeeTable products
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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("Coffee Table has legs: " + this.coffeeTable.hasLegs());
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
package src.design_patterns.creational_patterns;
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I am kept on a Victorian CoffeeTable");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("I am kept on a Modern CoffeeTable");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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("Coffee Table has legs: " + this.coffeeTable.hasLegs());
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
System.out.println();
FurnitureFactory factory1 = new VictorianFactory();
Client client1 = new Client(factory1);
client1.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I am kept on a Victorian CoffeeTable");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
}
public void keepOn() {
System.out.println("I am kept on a Modern CoffeeTable");
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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("Coffee Table has legs"this.coffeeTable.hasLegs());
}
}
public class AbstractFactoryPattern {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
interface Chair {
boolean hasLegs();
void sitOn();
}
interface Sofa {
boolean hasLegs();
void sitOn();
}
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I'm sat on a victorian chair");
}
}
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I'm sat on a Modern Chair");
}
}
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I'm sat on a Victorian Sofa");
}
}
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I'm sat on a Modern Sofa");
}
}
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I kept coffee on a Victorian CoffeeTable");
}
}
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("I kept coffee on a Modern CoffeeTable");
}
}
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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("Coffee Table has legs" + " " + this.coffeeTable.hasLegs());
}
}
public class AbstractFactorymethodDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
/**
/**
/**
/**
/**
class VictorianSofa implements Sofa { } class ModernSofa implements Sofa { } class VictorianCoffeeTable implements CoffeeTable { } class ModernCoffeeTable implements CoffeeTable { } /**
/**
/**
class Client { } public class AbstractFactoryDemo { } |
Beta Was this translation helpful? Give feedback.
-
package DesignPatterns;
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I kept coffee on a Victorian CoffeeTable");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("I kept coffee on a Modern CoffeeTable");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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("Coffee Table has legs " + this.coffeeTable.hasLegs());
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client1 = new Client(factory);
client1.useFurniture();
System.out.println();
FurnitureFactory victorianFactory = new VictorianFactory();
Client client2 = new Client(victorianFactory);
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
interface Chair { class VictorianSofa implements Sofa { class VictorianCoffeeTable implements CoffeeTable { |
Beta Was this translation helpful? Give feedback.
-
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void keepOn() {
System.out.println("Keep on Victorian Coffee Table");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
@Override
public void keepOn() {
System.out.println("Keep on Modern Coffee Table");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
void useFurniture() {
this.chair.sitOn();
this.sofa.sitOn();
this.coffeeTable.keepOn();
if (this.chair.hasLegs())
System.out.println("Chair has legs!");
if (this.sofa.hasLegs())
System.out.println("Sofa has legs!");
if (this.coffeeTable.hasLegs())
System.out.println("CoffeeTable has legs!");
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory1 = new ModernFactory();
Client client1 = new Client(factory1);
client1.useFurniture();
FurnitureFactory factory2 = new VictorianFactory();
Client client2 = new Client(factory2);
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
// -------------------
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
// -------------------
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
// ---------------
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void keepOn() {
System.out.println("I kept the coffee on VictorianCoffeeTable");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
@Override
public void keepOn() {
System.out.println("I kept the coffee on ModernCoffeeTable");
}
}
// ---------------
// TODO: Implement the variants of Sofa and CoffeeTable products
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
// TODO: Implement appropriate object creation
public Sofa createSofa() {
return new VictorianSofa();
}
// TODO: Implement appropriate object creation
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
// TODO: Implement appropriate object creation
public Sofa createSofa() {
return new ModernSofa();
}
// TODO: Implement appropriate object creation
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
void useFurniture() {
this.chair.sitOn();
// TODO: Uncomment and use Sofa and CoffeeTable
this.sofa.sitOn();
this.coffeeTable.keepOn();
// TODO: For each furniture, print whether it has legs or not
System.out.println(chair.hasLegs());
System.out.println(sofa.hasLegs());
System.out.println(coffeeTable.hasLegs());
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory1 = new ModernFactory();
Client client1 = new Client(factory1);
client1.useFurniture();
// ---------------------------------------------------------------------
FurnitureFactory factory2 = new VictorianFactory();
Client client2 = new Client(factory2);
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
package src.design_patterns.creational_patterns;
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I kept coffee on a Victorian CoffeeTable");
}
}
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("I kept coffee on a Modern CoffeeTable");
}
}
// TODO: Implement the variants of Sofa and CoffeeTable products
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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(this.chair.hasLegs());
System.out.println(this.sofa.hasLegs());
System.out.println(this.coffeeTable.hasLegs());
// TODO: For each furniture, print whether it has legs or not
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
FurnitureFactory V_F = new VictorianFactory();
Client c2 = new Client(V_F);
c2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
class VictorianSofa implements Sofa{
@Override
public boolean hasLegs() {
// TODO Auto-generated method stub
return true;
}
@Override
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
class ModernSofa implements Sofa{
@Override
public boolean hasLegs() {
// TODO Auto-generated method stub
return false;
}
@Override
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
class VictorianCoffeeTable implements CoffeeTable{
@Override
public boolean hasLegs() {
// TODO Auto-generated method stub
return true;
}
@Override
public void keepOn() {
// TODO Auto-generated method stub
System.out.println("I kept coffee on a Victorian Coffee Table");
}
}
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("I kept coffee on a Modern CoffeeTable");
}
}
// TODO: Implement the variants of Sofa and CoffeeTable products
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
// TODO: Implement appropriate object creation
public Sofa createSofa() {
return new VictorianSofa();
}
// TODO: Implement appropriate object creation
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
// TODO: Implement appropriate object creation
public Sofa createSofa() {
return new ModernSofa();
}
// TODO: Implement appropriate object creation
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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("Coffee Table has legs:- " + this.coffeeTable.hasLegs());
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client1 = new Client(factory);
client1.useFurniture();
FurnitureFactory victorianFactory = new VictorianFactory();
Client client2 = new Client(victorianFactory);
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
package Design_Pattern;
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I kept coffee on a Victorian CoffeeTable");
}
}
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
public void keepOn() {
System.out.println("I kept coffee on a Modern CoffeeTable");
}
}
// TODO: Implement the variants of Sofa and CoffeeTable products
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
// TODO: Implement appropriate object creation
public Sofa createSofa() {
return new VictorianSofa();
}
// TODO: Implement appropriate object creation
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
// TODO: Implement appropriate object creation
public Sofa createSofa() {
return new ModernSofa();
}
// TODO: Implement appropriate object creation
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
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("Coffee Table has legs:- " + this.coffeeTable.hasLegs());
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory modernFactory = new ModernFactory();
Client client1 = new Client(modernFactory);
client1.useFurniture();
FurnitureFactory victorianFactory = new VictorianFactory();
Client client2 = new Client(victorianFactory);
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
// TODO: Implement the variants of Sofa and CoffeeTable products
class VictorianSofa implements Sofa{
@Override
public boolean hasLegs(){
return true;
}
@Override
public void sitOn(){
System.out.println("I am sat on a Victorian sofa");
}
}
class ModernSofa implements Sofa{
@Override
public boolean hasLegs(){
return true;
}
@Override
public void sitOn(){
System.out.println("I am sat on a Modern sofa");
}
}
class VictorianCoffeeTable implements CoffeeTable{
@Override
public boolean hasLegs(){
return true;
}
@Override
public void keepOn(){
System.out.println("I am kept on Victorian coffee table");
}
}
class ModernCoffeeTable implements CoffeeTable{
@Override
public boolean hasLegs(){
return true;
}
@Override
public void keepOn(){
System.out.println("I am kept on Modern coffee table");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
// TODO: Implement appropriate object creation
public Sofa createSofa() {
return new VictorianSofa();
}
// TODO: Implement appropriate object creation
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
// TODO: Implement appropriate object creation
public Sofa createSofa() {
return new ModernSofa();
}
// TODO: Implement appropriate object creation
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
void useFurniture() {
this.chair.sitOn();
// TODO: Uncomment and use Sofa and CoffeeTable
this.sofa.sitOn();
this.coffeeTable.keepOn();
// TODO: For each furniture, print whether it has legs or not
System.out.println("Chair has legs? "+this.chair.hasLegs());
System.out.println("Sofa has legs? "+this.sofa.hasLegs());
System.out.println("Coffee Table has legs? "+this.coffeeTable.hasLegs());
}
}
public class AbstractFactory {
public static void main(String[] args) {
FurnitureFactory factory1 = new ModernFactory();
Client client = new Client(factory1);
client.useFurniture();
FurnitureFactory factory2 = new VictorianFactory();
client = new Client(factory2);
client.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
package week_6;
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - CoffeeTable
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I sat on a Victorian table");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
public void sitOn() {
System.out.println("I sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
public void sitOn() {
System.out.println("I sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - CoffeeTable
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
public void keepOn() {
System.out.println("I sat on a Modern table");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
public Chair createChair() {
return new VictorianChair();
}
public Sofa createSofa() {
return new VictorianSofa();
}
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Sofa createSofa() {
return new ModernSofa();
}
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
void useFurniture() {
this.chair.sitOn();
this.sofa.sitOn();
this.coffeeTable.keepOn();
if(this.chair.hasLegs())
System.out.println("Chair has legs.");
if(this.sofa.hasLegs())
System.out.println("Sofa has legs.");
if(this.coffeeTable.hasLegs())
System.out.println("CoffeeTable has legs");
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory = new ModernFactory();
Client client = new Client(factory);
client.useFurniture();
FurnitureFactory factory2 = new VictorianFactory();
Client client2 = new Client(factory2);
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
package March24; interface Chair { } class VictorianChair implements Chair { } class ModernChair implements Chair { } interface Sofa { } interface CoffeeTable { } class VictorianSofa implements Sofa { } class ModernSofa implements Sofa { } class VictorianCoffeeTable implements CoffeeTable { } class ModernCoffeeTable implements CoffeeTable { } interface FurnitureFactory { } class VictorianFactory implements FurnitureFactory { } class ModernFactory implements FurnitureFactory { } class Client { } public class AbstractFactory { } |
Beta Was this translation helpful? Give feedback.
-
package design_abstract_factory_pattern;
/**
* Product Family - Furniture
* Product - Chair
*/
interface Chair {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Victorian
*/
class VictorianChair implements Chair {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void sitOn() {
System.out.println("I am sat on a Victorian Chair");
}
}
/**
* Product Family - Furniture
* Product - Chair
* Variant Type - Modern
*/
class ModernChair implements Chair {
@Override
public boolean hasLegs() {
return false;
}
@Override
public void sitOn() {
System.out.println("I am sat on a Modern Chair");
}
}
/**
* Product Family - Furniture
* Product - Sofa
*/
interface Sofa {
boolean hasLegs();
void sitOn();
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Victorian
*/
class VictorianSofa implements Sofa {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void sitOn() {
System.out.println("I am sat on a Victorian Sofa");
}
}
/**
* Product Family - Furniture
* Product - Sofa
* Variant Type - Modern
*/
class ModernSofa implements Sofa {
@Override
public boolean hasLegs() {
return false;
}
@Override
public void sitOn() {
System.out.println("I am sat on a Modern Sofa");
}
}
/**
* Product Family - Furniture
* Product - Coffee Table
*/
interface CoffeeTable {
boolean hasLegs();
void keepOn();
}
/**
* Product Family - Furniture
* Product - Coffee Table
* Variant Type - Victorian
*/
class VictorianCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return true;
}
@Override
public void keepOn() {
System.out.println("I kept tea on a Victorian Coffee Table");
}
}
/**
* Product Family - Furniture
* Product - Coffee Table
* Variant Type - Modern
*/
class ModernCoffeeTable implements CoffeeTable {
@Override
public boolean hasLegs() {
return false;
}
@Override
public void keepOn() {
System.out.println("I kept tea on a Modern Coffee Table");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
interface FurnitureFactory {
Chair createChair();
Sofa createSofa();
CoffeeTable createCoffeeTable();
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Victorian
*/
class VictorianFactory implements FurnitureFactory {
@Override
public Chair createChair() {
return new VictorianChair();
}
@Override
public Sofa createSofa() {
return new VictorianSofa();
}
@Override
public CoffeeTable createCoffeeTable() {
return new VictorianCoffeeTable();
}
}
/**
* Concrete Factory
* Product Family - Furniture
* Variant Type - Modern
*/
class ModernFactory implements FurnitureFactory {
@Override
public Chair createChair() {
return new ModernChair();
}
@Override
public Sofa createSofa() {
return new ModernSofa();
}
@Override
public CoffeeTable createCoffeeTable() {
return new ModernCoffeeTable();
}
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeetable;
Client(FurnitureFactory factory) {
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeetable = factory.createCoffeeTable();
}
void useFurniture() {
chair.sitOn();
sofa.sitOn();
coffeetable.keepOn();
System.out.println(chair.getClass().getSimpleName() + " has legs: " + chair.hasLegs());
System.out.println(sofa.getClass().getSimpleName() + " has legs: " + sofa.hasLegs());
System.out.println(coffeetable.getClass().getSimpleName() + " has legs: " + coffeetable.hasLegs());
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
FurnitureFactory factory1 = new VictorianFactory();
FurnitureFactory factory2 = new ModernFactory();
Client client1 = new Client(factory1);
Client client2 = new Client(factory2);
client1.useFurniture();
client2.useFurniture();
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Implementation of Abstract Factory Design Pattern
Implement the
TODOsin the Abstract Factory code.Overview of the pending items:
SofaandCoffeeTable.createSofa()andcreateCoffeeTable()methods in theVariantFactoryclasses.useFurniture()method in theClientclass to use all the furnitures.Beta Was this translation helpful? Give feedback.
All reactions