Demo entry 6645849

FactoryMode

   

Submitted by anonymous on Oct 12, 2017 at 01:30
Language: Java. Code size: 817 Bytes.

package DesignMode;

public class FactoryMode {
	public static void main(String[] args){
		AnimalFactory animalFactory = new AnimalFactory();
		
		Animal cat = animalFactory.GetAnimals("cat");
		cat.name();
		
		Animal dog = animalFactory.GetAnimals("dog");
		dog.name();
	}
}

class Factory_Method{
	
}

interface Animal{
	void name();
}

class Cat implements Animal{

	@Override
	public void name() {
		System.out.println("this is a cat ! ");
	}
	
}

class Dog implements Animal{

	@Override
	public void name() {
		System.out.println("this is a dog ! ");
	}
}

class AnimalFactory{
	public Animal GetAnimals(String name){
		if(name.equalsIgnoreCase("cat"))
			return new Cat();
		else if(name.equalsIgnoreCase("dog"))
			return new Dog();
		else
			return null;
	}
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).