Task - Design Patterns - Factory Method #60
Replies: 31 comments
-
package week_6;
interface MyWrapper {
void printType();
}
class MyInteger implements MyWrapper {
public void printType() {
System.out.println("Integer");
}
}
class MyDouble implements MyWrapper {
public void printType() {
System.out.println("Double");
}
}
class MyCharacter implements MyWrapper {
public void printType() {
System.out.println("Character");
}
}
class MyBoolean implements MyWrapper {
public void printType() {
System.out.println("Boolean");
}
}
class MyWrapperFactory {
private boolean checkInteger(String type) {
return type.matches("^[+-]?[0-9]+");
}
private boolean checkCharacter(String type) {
return type.matches("[a-zA-Z]");
}
private boolean checkDouble(String type) {
return type.matches("^[+-]?[0-9]+.[0-9]+");
}
private boolean checkBoolean(String type) {
return type.matches("true|false");
}
public MyWrapper createMyWrapper(String type) {
MyWrapper wrapper = null;
if (type == null || type.length() == 0)
throw new IllegalArgumentException("String is empty!!!");
if (checkCharacter(type))
wrapper = new MyCharacter();
else if (checkDouble(type))
wrapper = new MyDouble();
else if (checkBoolean(type))
wrapper = new MyBoolean();
else if (checkInteger(type))
wrapper = new MyInteger();
else
throw new IllegalArgumentException("Invalid type");
return wrapper;
}
}
public class DesignPattern {
public static void main(String[] args) {
MyWrapperFactory myFactory = new MyWrapperFactory();
try {
myFactory.createMyWrapper("1").printType();
myFactory.createMyWrapper("1.1").printType();
myFactory.createMyWrapper("a").printType();
myFactory.createMyWrapper("true").printType();
} catch (IllegalArgumentException e) {
System.out.println(e);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
interface MyWrapper {
void printType();
}
class MyInteger implements MyWrapper {
@Override
public void printType() {
System.out.println("Integer");
}
}
class MyDouble implements MyWrapper {
@Override
public void printType() {
System.out.println("Double");
}
}
class MyCharacter implements MyWrapper {
@Override
public void printType() {
System.out.println("Character");
}
}
class MyBoolean implements MyWrapper {
@Override
public void printType() {
System.out.println("Boolean");
}
}
class MyWrapperFactory {
public MyWrapper createMyWrapper(String val){
if(val == null || val.length() == 0)
return null;
else if(val.matches("\\d+"))
return new MyInteger();
else if(val.matches("\\d+[.]\\d+"))
return new MyDouble();
else if(val.matches("[a-zA-Z]"))
return new MyCharacter();
else if("true".equals(val) || "false".equals(val))
return new MyBoolean();
else
throw new IllegalArgumentException("Invalid Type");
}
}
public class FactoryDemo {
public static void main(String[] args) {
MyWrapperFactory myFactory = new MyWrapperFactory();
MyWrapper myWrapper1 = myFactory.createMyWrapper("123");
MyWrapper myWrapper2 = myFactory.createMyWrapper("12.34");
MyWrapper myWrapper3 = myFactory.createMyWrapper("S");
MyWrapper myWrapper4 = myFactory.createMyWrapper("false");
myWrapper1.printType();
myWrapper2.printType();
myWrapper3.printType();
myWrapper4.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
interface MyWrapper { class MyInteger implements MyWrapper { } class MyDouble implements MyWrapper { } class MyCharacter implements MyWrapper { } class MyBoolean implements MyWrapper { } class MyWrapperFactory { class PatternDriver { } |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
interface MyWrapper {
void printType();
}
class MyInteger implements MyWrapper {
String str;
public MyInteger(String str) {
this.str = str;
}
public void printType() {
System.out.println("The input is of type Integer and value is: " + str);
}
}
class MyDouble implements MyWrapper {
String str;
public MyDouble(String str) {
this.str = str;
}
public void printType() {
System.out.println("The input is of type Double and value is: " + str);
}
}
class MyCharacter implements MyWrapper {
String str;
public MyCharacter(String str) {
this.str = str;
}
public void printType() {
System.out.println("The input is of type Character and value is: " + str);
}
}
class MyBoolean implements MyWrapper {
String str;
public MyBoolean(String str) {
this.str = str;
}
public void printType() {
System.out.println("The input is of type Boolean and value is: " + str);
}
}
class MyWrapperFactory {
public MyWrapper createMyWrapper(String input) {
if (input == null || input.length() == 0)
return null;
else if (input.matches("\\d+"))
return new MyInteger(input);
else if (input.matches("[+-]?(\\d*[.])?\\d+"))
return new MyDouble(input);
else if (input.equals("true") || input.equals("false"))
return new MyBoolean(input);
else if (input.matches("[a-zA-Z]"))
return new MyCharacter(input);
else
throw new IllegalArgumentException("Not a valid input type");
}
}
public class CustomWrapperDriver {
public static void main(String[] args) {
MyWrapperFactory obj = new MyWrapperFactory();
System.out.println("Enter a value: ");
Scanner sc = new Scanner(System.in);
String value = sc.nextLine();
MyWrapper w = obj.createMyWrapper(value);
if (w != null)
w.printType();
else
System.out.println("Empty input");
}
} |
Beta Was this translation helpful? Give feedback.
-
interface MyWrapper {
void printType();
}
class MyInteger implements MyWrapper {
@Override
public void printType() {
System.out.println("I am an Integer");
}
}
class MyDouble implements MyWrapper {
@Override
public void printType() {
System.out.println("I am a Double");
}
}
class MyCharacter implements MyWrapper {
@Override
public void printType() {
System.out.println("I am a Character");
}
}
class MyBoolean implements MyWrapper {
@Override
public void printType() {
System.out.println("I am a Boolean");
}
}
class MyWrapperFactory {
public MyWrapper getMyWrapperInstance(String channel) {
if (channel == null || channel.isEmpty()) {
return null;
}
System.out.println(channel);
if (channel.matches("[a-zA-z]")) {
return new MyCharacter();
}
if (channel.matches("\\d+")) {
return new MyInteger();
}
if ("true".equals(channel) || "false".equals(channel)) {
return new MyBoolean();
}
if (channel.matches("\\d+.\\d+")) {
return new MyDouble();
}
throw new IllegalArgumentException("Unknown type of channel");
}
}
public class FactoryMethod {
public static void main(String[] args) {
MyWrapperFactory myWrapperFactory = new MyWrapperFactory();
MyWrapper obj1 = myWrapperFactory.getMyWrapperInstance("10");
obj1.printType();
MyWrapper obj2 = myWrapperFactory.getMyWrapperInstance("e");
obj2.printType();
MyWrapper obj3 = myWrapperFactory.getMyWrapperInstance("10.5");
obj3.printType();
MyWrapper obj4 = myWrapperFactory.getMyWrapperInstance("false");
obj4.printType();
MyWrapper obj5 = myWrapperFactory.getMyWrapperInstance("asfsaf");
obj5.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
package DesignPatterns;
interface MyWrapper{
void printType();
}
class MyInteger implements MyWrapper{
public void printType(){
System.out.println("Integer type");
}
}
class MyDouble implements MyWrapper{
public void printType(){
System.out.println("Double type");
}
}
class MyCharacter implements MyWrapper{
public void printType(){
System.out.println("Character type");
}
}
class MyBoolean implements MyWrapper{
public void printType(){
System.out.println("Boolean type");
}
}
class MyWrapperFactory{
public static boolean isInteger(String number){
for(int i=0;i<number.length();i++){
if(number.charAt(i)<48 && number.charAt(i)>57){
return false;
}
}
return true;
}
public static boolean isDouble(String number){
int cnt=0;
for(int i=0;i<number.length();i++){
if(cnt>1 || number.charAt(i)<48 && number.charAt(i)>57){
return false;
}else if( number.charAt(i)=='.'){
cnt++;
}
}
return true;
}
public static boolean isBoolean(String number){
if(number.toLowerCase().equals("true") || number.toLowerCase().equals("false")){
return true;
}
return false;
}
public MyWrapper createMyWrapper(String number){
if(isBoolean(number)){
return new MyBoolean();
}else if(isCharacter(number)){
return new MyCharacter();
}else if(isDouble(number)){
return new MyDouble();
}else if(isInteger(number)){
return new MyInteger();
}else{
throw new IllegalArgumentException("Invalid Input"+number);
}
}
private boolean isCharacter(String number) {
if(number.length()==1 && !(number.charAt(0)>=48 && number.charAt(0)<=57)){
return true;
}
return false;
}
}
public class FactoryPattern{
public static void main(String[] args) {
MyWrapperFactory factory=new MyWrapperFactory();
try{
MyWrapper b= factory.createMyWrapper("34.67");
b.printType();
}catch(Exception e){
System.out.println(e);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
interface MyWrapper {
void printType();
}
class MyInteger implements MyWrapper {
@Override
public void printType() {
System.out.println("Integer type");
}
}
class MyDouble implements MyWrapper {
@Override
public void printType() {
System.out.println("Double type");
}
}
class MyCharacter implements MyWrapper {
@Override
public void printType() {
System.out.println("Character type");
}
}
class MyBoolean implements MyWrapper {
@Override
public void printType() {
System.out.println("Boolean type");
}
}
class MyWrapperFactory {
public static MyWrapper getMyWrapperInstance(String s) {
if (s == null || s.isEmpty())
return null;
if (s.matches("-?\\d+")) {
return new MyInteger();
}
if (s.matches("-?\\d+(\\.\\d+)?"))
return new MyDouble();
if (s.matches("[a-zA-Z]"))
return new MyCharacter();
if (s.equals("true") || s.equals("false"))
return new MyBoolean();
else
throw new IllegalArgumentException("Invalid input");
}
}
public class FactoryMethodDemo {
public static void main(String[] args) {
MyWrapperFactory.getMyWrapperInstance("-1234").printType();
MyWrapperFactory.getMyWrapperInstance("890000.34").printType();
MyWrapperFactory.getMyWrapperInstance("true").printType();
MyWrapperFactory.getMyWrapperInstance("c").printType();
MyWrapperFactory.getMyWrapperInstance("@").printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
interface MyWrapper{
void printType();
}
class MyInteger implements MyWrapper{
@Override
public void printType(){
System.out.println("Integer");
}
}
class MyDouble implements MyWrapper{
@Override
public void printType(){
System.out.println("Double");
}
}
class MyCharacter implements MyWrapper{
@Override
public void printType(){
System.out.println("Character");
}
}
class MyBoolean implements MyWrapper{
@Override
public void printType(){
System.out.println("Boolean");
}
}
class MyWrapperFactory{
MyWrapper createMyWrapper(String input){
if(input == null || input.isEmpty())
return null;
else if(input.matches("\\d+"))
return new MyInteger();
else if(input.matches("([0-9]*)\\.([0-9]*)"))
return new MyDouble();
else if(input.length() == 1)
return new MyCharacter();
else if(input.equals("true") || input.equals("false"))
return new MyBoolean();
else
throw new IllegalArgumentException("Invalid input : " + input);
}
}
public class FactoryMethodsDemo {
public static void main(String[] args) {
MyWrapperFactory myWrapperFactory = new MyWrapperFactory();
MyWrapper myWrapper = myWrapperFactory.createMyWrapper("9.6");
myWrapper.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
package DesignPattern;
interface MyWrapper{
void printType();
}
class MyInteger implements MyWrapper{
@Override
public void printType()
{
System.out.println("It is of integer type");
}
}
class MyDouble implements MyWrapper{
@Override
public void printType()
{
System.out.println("It is of double type");
}
}
class MyCharacter implements MyWrapper{
@Override
public void printType()
{
System.out.println("It is of character type");
}
}
class MyBoolean implements MyWrapper{
@Override
public void printType()
{
System.out.println("It is of boolean type");
}
}
class MyWrapperFactory{
public MyWrapper createMyWrapper(String param)
{
boolean isCharacter=false;
boolean isBoolean=false;
//check for boolean
if(param.equals("false")|| param.equals("true"))
{
isBoolean=true;
}
//check for charcter
if(param.length()==1 && !Character.isDigit(param.charAt(0)))
{
isCharacter=true;
}
//cases
if(isBoolean)
{
return new MyBoolean();
}
else if(isCharacter)
{
return new MyCharacter();
}
else if (param.matches("\\d+"))
{
return new MyInteger();
}
else if(param.matches("[0-9]{1,13}(\\.[0-9]*)?"))
{
return new MyDouble();
}
else
{
throw new IllegalArgumentException("Invalid data type");
}
}
}
public class FactoryPattern{
public static void main(String[] args){
MyWrapperFactory obj=new MyWrapperFactory();
MyWrapper myWrapperObj=obj.createMyWrapper("123.4");
myWrapperObj.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
interface MyWrapper {
void printType();
}
class MyInteger implements MyWrapper {
@Override
public void printType() {
System.out.println("It is an Integer");
}
}
class MyDouble implements MyWrapper {
@Override
public void printType() {
System.out.println("It is a Double");
}
}
class MyCharacter implements MyWrapper {
@Override
public void printType() {
System.out.println("It is a Character");
}
}
class MyBoolean implements MyWrapper {
@Override
public void printType() {
System.out.println("It is a Boolean");
}
}
class MyWrapperFactory {
public MyWrapper getMyWrapperInstance(String s) {
if (s == null || s.isEmpty()) {
return null;
}
if (s.equalsIgnoreCase("TRUE") || s.equalsIgnoreCase("FALSE")) {
return new MyBoolean();
} else if (s.length() == 1
&& ((s.charAt(0) <= 90 && s.charAt(0) >= 65) || (s.charAt(0) <= 122 && s.charAt(0) >= 97))) {
return new MyCharacter();
} else if (s.matches("^[+-]?[0-9]+.[0-9]+")) {
return new MyDouble();
} else if (s.matches("^[+-]?[0-9]+")) {
return new MyInteger();
} else {
throw new IllegalArgumentException("invalid string input : " + s);
}
}
}
public class DataType {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
MyWrapperFactory obj = new MyWrapperFactory();
MyWrapper t = obj.getMyWrapperInstance(s);
t.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
interface Mywrapper {
void printType();
}
class Myinteger implements Mywrapper {
@Override
public void printType() {
System.out.println("I am of type Integer");
}
}
class Mydouble implements Mywrapper {
@Override
public void printType() {
System.out.println("I am of type floating point");
}
}
class Mycharacter implements Mywrapper {
@Override
public void printType() {
System.out.println("I am of type character");
}
}
class Myboolean implements Mywrapper {
@Override
public void printType() {
System.out.println("I am of type boolean");
}
}
class MyWrapperFactory {
public Mywrapper createmyWrapper(String param) throws IllegalArgumentException {
if (param == null || param.isEmpty()) {
return null;
} else if (param.matches("[0-9]")) {
return new Myinteger();
} else if (param.matches("[+-]?([0-9]*[.])?[0-9]+")) {
return new Mydouble();
} else if (param.equals("true") || param.equals("false")) {
return new Myboolean();
} else if (param.length() == 1 && (param.charAt(0) > 0 && param.charAt(0) < 127)) {
return new Mycharacter();
} else
throw new IllegalArgumentException("Invalid Type");
}
}
public class Factorymethod {
public static void main(String[] args) {
MyWrapperFactory obj = new MyWrapperFactory();
obj.createmyWrapper("true").printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
interface MyWrapper { class MyInteger implements MyWrapper { } class MyDouble implements MyWrapper { } class MyCharacter implements MyWrapper { } class MyBoolean implements MyWrapper { } class MyWrapperFactory { } public class FactoryMethodDemo { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
package DSA.core;
interface MyWrapper {
void printType();
}
class MyInteger implements MyWrapper {
@Override
public void printType() {
System.out.println("Integer");
}
}
class MyDouble implements MyWrapper {
@Override
public void printType() {
System.out.println("Double");
}
}
class MyCharacter implements MyWrapper {
@Override
public void printType() {
System.out.println("Character");
}
}
class MyBoolean implements MyWrapper {
@Override
public void printType() {
System.out.println("Boolean");
}
}
class MyString implements MyWrapper {
@Override
public void printType() {
System.out.println("String");
}
}
class MyWrapperFactory {
public MyWrapper createMyWrapper(String data) {
if (data == null || data.isEmpty()) {
return null;
}
if (data == "true" || data == "false") {
return new MyBoolean();
}
if (data.length() == 1 && (Character.isDigit(data.charAt(0)) == false)) {
return new MyCharacter();
}
if (data.matches("-?\\d+")) {
return new MyInteger();
}
if (data.matches("-?\\d+(\\.\\d+)?")) {
return new MyDouble();
} else {
throw new IllegalArgumentException("Invalid inpur");
}
}
}
public class FactoryDemo {
public static void main(String[] args) {
MyWrapperFactory myWrapperFactory = new MyWrapperFactory();
MyWrapper printType1 = myWrapperFactory.createMyWrapper("41.5");
MyWrapper printType2 = myWrapperFactory.createMyWrapper("true");
MyWrapper printType3 = myWrapperFactory.createMyWrapper("41");
MyWrapper printType4 = myWrapperFactory.createMyWrapper("c");
MyWrapper printType5 = myWrapperFactory.createMyWrapper("hello");
printType1.printType();
printType2.printType();
printType3.printType();
printType4.printType();
printType5.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
interface MyWrapper {
void printType();
}
class MyInteger implements MyWrapper {
@Override
public void printType() {
System.out.println("Integer type");
}
}
class MyDouble implements MyWrapper {
@Override
public void printType() {
System.out.println("Double type");
}
}
class MyCharacter implements MyWrapper {
@Override
public void printType() {
System.out.println("Character type");
}
}
class MyBoolean implements MyWrapper {
@Override
public void printType() {
System.out.println("Boolean type");
}
}
class MyWrapperFactory {
MyWrapper createMyWrapper(String input) {
if (input == null || input.isEmpty()) {
return null;
}
if (input.matches("\\d+"))
return new MyInteger();
else if (input.matches("([0-9]*)\\.([0-9]*)"))
return new MyDouble();
else if (input.length() == 1)
return new MyCharacter();
else if (input.equals("true") || input.equals("false"))
return new MyBoolean();
else
throw new IllegalArgumentException("Invalid input : " + input);
}
}
public class FactorymethodDemo {
public static void main(String[] args) {
MyWrapperFactory factory = new MyWrapperFactory();
MyWrapper obj = factory.createMyWrapper("3.3");
obj.printType();
MyWrapper obj1 = factory.createMyWrapper("3");
obj1.printType();
MyWrapper obj2 = factory.createMyWrapper("Prem");
obj2.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
interface MyWrapper{
void printType();
}
class MyInteger implements MyWrapper{
@Override
public void printType() {
System.out.println("Integer");
}
}
class MyDouble implements MyWrapper{
@Override
public void printType() {
System.out.println("Double");
}
}
class MyCharacter implements MyWrapper{
@Override
public void printType() {
System.out.println("Character");
}
}
class MyBoolean implements MyWrapper{
@Override
public void printType() {
System.out.println("Boolean");
}
}
class MyWrapperFactory {
public MyWrapper getMyWrapperInstance(String value){
if(value == null || value.length() == 0)
return null;
else if(value.matches("\\d+"))
return new MyInteger();
else if(value.matches("\\d+[.]\\d+"))
return new MyDouble();
else if(value.matches("[a-zA-Z]"))
return new MyCharacter();
else if(value.equals("true") || value.equals("false"))
return new MyBoolean();
else
throw new IllegalArgumentException("Invalid Type");
}
}
public class FactoryDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
MyWrapperFactory myWrapperFactory = new MyWrapperFactory();
MyWrapper myWrapper = myWrapperFactory.getMyWrapperInstance(s);
myWrapper.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
package design_patterns.creational;
import java.util.regex.Pattern;
interface MyWrapper {
void printType();
}
public class FactoryPatternEg {
class MyInteger implements MyWrapper {
@Override
public void printType() {
System.out.println("Integer");
}
}
class MyDouble implements MyWrapper {
@Override
public void printType() {
System.out.println("Double");
}
}
class MyCharacter implements MyWrapper {
@Override
public void printType() {
System.out.println("Character");
}
}
class MyBoolean implements MyWrapper {
@Override
public void printType() {
System.out.println("Boolean");
}
}
class MyString implements MyWrapper {
@Override
public void printType() {
System.out.println("String");
}
}
public MyWrapper createMyWrapper(String str) {
// String[] strAr = str.split("\\.");
if (isInt(str)) {
return new MyInteger();
}
if (isDouble(str)) {
return new MyDouble();
}
if (str.equals("true") || str.equals("false")) {
return new MyBoolean();
}
if (str.length() == 1) {
return new MyCharacter();
}
return new MyString(); // default
}
private boolean isInt(String str) {
return Pattern.compile("-?\\d+").matcher(str).matches();
}
private boolean isDouble(String str) {
return Pattern.compile("-?\\d+(\\.\\d+)?").matcher(str).matches();
}
public static void main(String[] args) {
FactoryPatternEg myFactory = new FactoryPatternEg();
MyWrapper myWrapper = myFactory.createMyWrapper("");
myWrapper.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
interface MyWrapper { class MyInteger implements MyWrapper { class MyCharacter implements MyWrapper { public class CustomeWrapperDriver { |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
interface MyWrapper{
void printType();
}
class MyInteger implements MyWrapper{
@Override
public void printType() {
System.out.println("Integer");
}
}
class MyDouble implements MyWrapper{
@Override
public void printType() {
System.out.println("Double");
}
}
class MyCharacter implements MyWrapper{
@Override
public void printType() {
System.out.println("Character");
}
}
class MyBoolean implements MyWrapper{
@Override
public void printType() {
System.out.println("Boolean");
}
}
class MyWrapperFactory {
public MyWrapper getMyWrapperInstance(String value){
if(value == null || value.length() == 0)
return null;
else if(value.matches("\\d+"))
return new MyInteger();
else if(value.matches("\\d+[.]\\d+"))
return new MyDouble();
else if(value.matches("[a-zA-Z]"))
return new MyCharacter();
else if(value.equals("true") || value.equals("false"))
return new MyBoolean();
else
throw new IllegalArgumentException("Invalid Type");
}
}
public class FactoryDesign {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
MyWrapperFactory myWrapperFactory = new MyWrapperFactory();
MyWrapper myWrapper = myWrapperFactory.getMyWrapperInstance(s);
myWrapper.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
interface MyWrapper {
void printType();
}
class MyInteger implements MyWrapper {
@Override
public void printType() {
System.out.println("Integer Type");
}
}
class MyDouble implements MyWrapper {
@Override
public void printType() {
System.out.println("Double Type");
}
}
class MyCharacter implements MyWrapper {
@Override
public void printType() {
System.out.println("Character Type");
}
}
class MyBoolean implements MyWrapper {
@Override
public void printType() {
System.out.println("Boolean Type");
}
}
class WrapperFactory {
public MyWrapper checkType(String str) {
if (str == null)
throw new IllegalArgumentException("null");
else if (str.length() == 0)
throw new IllegalArgumentException("Empty String!!");
else if (str.matches("-?\\d+"))
return new MyInteger();
else if (str.length() == 1 && str.matches("[a-zA-Z]"))
return new MyCharacter();
else if (str.equals("true") || str.equals("false"))
return new MyBoolean();
else if (str.matches("-?\\d+\\.\\d+"))
return new MyDouble();
else
throw new IllegalArgumentException("Unknown Type!");
}
}
public class MyWrapperFactory {
public static void main(String[] args) {
WrapperFactory fac = new WrapperFactory();
MyWrapper myFactory = fac.checkType("g");
myFactory.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
interface MyWrapper { class MyInteger implements MyWrapper { class MyDouble implements MyWrapper { class MyCharacter implements MyWrapper { class MyBoolean implements MyWrapper { class WrapperFactory { public class FactoryDemo { } |
Beta Was this translation helpful? Give feedback.
-
package Tasks.Design_Pattern;
interface MyWrapper {
void printType();
}
class MyInteger implements MyWrapper {
@Override
public void printType() {
// TODO Auto-generated method stub
System.out.println("Integer");
}
}
class MyDouble implements MyWrapper {
@Override
public void printType() {
// TODO Auto-generated method stub
System.out.println("Double");
}
}
class MyBoolean implements MyWrapper {
@Override
public void printType() {
// TODO Auto-generated method stub
System.out.println("Boolean");
}
}
class MyCharacter implements MyWrapper {
@Override
public void printType() {
// TODO Auto-generated method stub
System.out.println("Character");
}
}
class MyWrapperFactory {
public MyWrapper getMyWrapperInstance(String value) {
// return the wrapper instance corresponding to the type of value
if (value == null || value.isEmpty())
return null;
else if(value.equals("true") || value.equals("false"))
return new MyBoolean();
else if(value.matches("\\d+"))
return new MyInteger();
else if(value.matches("[+-]?(\\d*[.])?\\d+"))
return new MyDouble();
else if(value.matches("[a-zA-z]"))
return new MyCharacter();
else
throw new IllegalArgumentException("Not a valid input type");
}
}
public class CustomeWrapperDriver {
public static void main(String[] args) {
MyWrapperFactory myWrapperFactory = new MyWrapperFactory();
myWrapperFactory.getMyWrapperInstance("1").printType();
myWrapperFactory.getMyWrapperInstance("1.5").printType();
myWrapperFactory.getMyWrapperInstance("true").printType();
myWrapperFactory.getMyWrapperInstance("a").printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
interface MyWrapper{
void printType();
}
class MyInteger implements MyWrapper{
@Override
public void printType() {System.out.println("Integer");}
}
class MyDouble implements MyWrapper{
@Override
public void printType() {System.out.println("Double");}
}
class MyBoolean implements MyWrapper{
@Override
public void printType() {System.out.println("Boolean");}
}
class MyCharacter implements MyWrapper{
@Override
public void printType() {System.out.println("Character");}
}
class MyWrapperFactory{
public MyWrapper getMyWrapperInstance(String value) {
if(value == null) return null;
else if(value.matches("\\d+")) return new MyInteger();
else if(value.matches("\\d+.\\d+")) return new MyDouble();
else if(value.matches("true|false")) return new MyBoolean();
else if(value.matches("[a-zA-Z]")) return new MyCharacter();
else throw new IllegalArgumentException("Unknown Type");
}
}
public class CustomeWrapperDriver {
public static void main(String[] args) {
MyWrapperFactory myFact = new MyWrapperFactory();
MyWrapper myWrapper1 = myFact.getMyWrapperInstance("143");
myWrapper1.printType();
MyWrapper myWrapper2 = myFact.getMyWrapperInstance("3.14");
myWrapper2.printType();
MyWrapper myWrapper3 = myFact.getMyWrapperInstance("true");
myWrapper3.printType();
MyWrapper myWrapper4 = myFact.getMyWrapperInstance("R");
myWrapper4.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
interface MyWrapper {
void printType();
}
class MyInteger implements MyWrapper {
@Override
public void printType() {
System.out.println("Integer");
}
}
class MyDouble implements MyWrapper {
@Override
public void printType() {
System.out.println("Double");
}
}
class MyCharacter implements MyWrapper {
@Override
public void printType() {
System.out.println("Character");
}
}
class MyBoolean implements MyWrapper {
@Override
public void printType() {
System.out.println("Boolean");
}
}
class MyWrapperFactory {
public MyWrapper createMyWrapper(String val){
if(val == null || val.length() == 0)
return null;
else if(val.matches("\\d+"))
return new MyInteger();
else if(val.matches("\\d+[.]\\d+"))
return new MyDouble();
else if(val.matches("[a-zA-Z]"))
return new MyCharacter();
else if("true".equals(val) || "false".equals(val))
return new MyBoolean();
else
throw new IllegalArgumentException("Invalid Type");
}
}
public class FactoryDemo {
public static void main(String[] args) {
MyWrapperFactory myFactory = new MyWrapperFactory();
MyWrapper myWrapper1 = myFactory.createMyWrapper("123");
MyWrapper myWrapper2 = myFactory.createMyWrapper("12.34");
MyWrapper myWrapper3 = myFactory.createMyWrapper("S");
MyWrapper myWrapper4 = myFactory.createMyWrapper("false");
myWrapper1.printType();
myWrapper2.printType();
myWrapper3.printType();
myWrapper4.printType();
}
} |
Beta Was this translation helpful? Give feedback.
-
package designPatternTypeQuiz;
public interface MyWrapper {
void printType();
}
package designPatternTypeQuiz;
public class MyBoolean implements MyWrapper{
@Override
public void printType() {
System.out.println("The value is type of MyBoolean");
}
}
package designPatternTypeQuiz;
public class MyCharacter implements MyWrapper{
@Override
public void printType() {
System.out.println("The value is type of MyCharacter");
}
}
package designPatternTypeQuiz;
public class MyDouble implements MyWrapper{
@Override
public void printType() {
System.out.println("The value is type of MyDouble");
}
}
package designPatternTypeQuiz;
public class MyInteger implements MyWrapper {
@Override
public void printType() {
System.out.println("The value is type of MyInteger");
}
}
package designPatternTypeQuiz;
import java.util.Locale;
public class MyWrapperFactory {
public MyWrapper getMyWrapperInstance(String value) throws Exception {
if(value.toLowerCase(Locale.ROOT).matches("^true$|^false$"))
return new MyBoolean();
else if(value.matches("[a-zA-Z]+"))
return new MyCharacter();
else if(value.matches("\\d+"))
return new MyInteger();
else if(value.matches("\\d+\\.\\d+"))
return new MyDouble();
else
throw new Exception("Unsupported type provided");
}
}
package designPatternTypeQuiz;
public class TestWrapper {
public static void main(String... args){
MyWrapperFactory factory = new MyWrapperFactory();
try{
MyWrapper instance1 = factory.getMyWrapperInstance("false");
instance1.printType();
MyWrapper instance2 = factory.getMyWrapperInstance("true");
instance2.printType();
MyWrapper instance3 = factory.getMyWrapperInstance("190");
instance3.printType();
MyWrapper instance4 = factory.getMyWrapperInstance("9.67");
instance4.printType();
MyWrapper instance5 = factory.getMyWrapperInstance("Juma");
instance5.printType();
}catch (Exception ex){
System.out.println(String.format("Error: %s", ex.getMessage()));
}
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Implement Factory Method Design Pattern
Declare an interface names
MyWrapperas follows:Implement the interface in the following concrete classes:
MyIntegerMyDoubleMyBooleanMyCharacterThe concrete methods in these classes just print the name of the class with a message, e.g., "The value is of type: MyInteger".
Create a factory class called
MyWrapperFactorywith the method:The
getMyWrapperInstancemethod returns an instance of one of the four concrete classes ornull, depending on the type of the parametervalue. Write custom logic and process the string to find out the type ofvalue. If thevalueis of undefined/unknown type, throwIllegalArgumentExceptionwith the message "Value format not recognized".String processing logic to Implement in the factory method:
valuestring contains only digits, without any other types of characters, return an instance ofMyInteger.valuestring contains eithertrueorfalsein all lowercase, return an instance ofMyBoolean.valuestring contains only digits, and a dot(.) in any position other than at first or last, then return an instance ofMyDouble.valuestring contains only a single alphabet, then return an instance ofMyCharacter.Demonstrate the working in the driver class named
CustomeWrapperDriver.Sample Inputs
Sample Outputs
Hints
Some common ASCII ranges:
Beta Was this translation helpful? Give feedback.
All reactions