Factory Design Pattern in Object Oriented Design Programming
- 时间:2020-09-10 13:03:17
- 分类:网络文摘
- 阅读:108 次
In OOP (Object Oriented Programming), we use inheritance to achieve the polymorphism. The factory design pattern allows you to create/manage different types of classes in a single place.
Let’s say, we have a abstract class Animal that defines a walk method that indicates that any subclasses (any animals) can walk. The abstract keyword forbids the direct instantiation of the class Animal.
Also, we add to the prototype of the class Animal an abstract method: single that needs to be implemented in subclasses. Note that the following also uses the lombok plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import lombok.Data; @Data public abstract class Animal { protected String name; public Animal(String name) { this.name = name; } public void walk() { System.out.println(this.name + " Walks."); } public abstract void sing(); } |
import lombok.Data;
@Data
public abstract class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public void walk() {
System.out.println(this.name + " Walks.");
}
public abstract void sing();
}And the Bird class inherits from the Animal. In the inherited class, we can invoke the constructor from the parent class using super constructor.
Not all animals can fly, thus we define the behavior that is only specific to bird e.g. fly().
Also, we need to implement the content of the sing which is the abstract method in the Animal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Data class Bird extends Animal { public Bird(String name) { super(name); } public void fly() { System.out.println(this.name + " Flies."); } @Override public void sing() { System.out.println(this.name + " Sings happily."); } } |
@Data
class Bird extends Animal {
public Bird(String name) {
super(name);
}
public void fly() {
System.out.println(this.name + " Flies.");
}
@Override
public void sing() {
System.out.println(this.name + " Sings happily.");
}
}Similarly, a Fish can swim and has a different implementation of the sing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Data class Fish extends Animal { public Fish(String name) { super(name); } public void swim() { System.out.println(this.name + " Swims."); } @Override public void sing() { System.out.println(this.name + " Sings sadly."); } } |
@Data
class Fish extends Animal {
public Fish(String name) {
super(name);
}
public void swim() {
System.out.println(this.name + " Swims.");
}
@Override
public void sing() {
System.out.println(this.name + " Sings sadly.");
}
}Factory Pattern in Java
In our factory class, we have a static method that takes parameters of Animal Type and Animal Name, then return an instance of the target.
1 2 3 4 5 6 7 8 9 10 | class AnimalFactory { public static Animal createAnimal(String type, String name) { switch (type) { case "BIRD": return new Bird(name); case "FISH": return new Fish(name); default: throw new IllegalArgumentException("Invalid Animal"); } } } |
class AnimalFactory {
public static Animal createAnimal(String type, String name) {
switch (type) {
case "BIRD": return new Bird(name);
case "FISH": return new Fish(name);
default:
throw new IllegalArgumentException("Invalid Animal");
}
}
}The return type should be parent abstract Animal class, and it can return concrete instance of a Bird or Fish respectively.
Example usage:
1 2 3 4 5 6 | Animal bird1 = AnimalFactory.createAnimal("BIRD", "bird1"); // bird1 Sings happily. bird1.sing(); Animal fish1 = AnimalFactory.createAnimal("FISH", "fish1"); // fish1 Sings sadly. fish1.sing(); |
Animal bird1 = AnimalFactory.createAnimal("BIRD", "bird1");
// bird1 Sings happily.
bird1.sing();
Animal fish1 = AnimalFactory.createAnimal("FISH", "fish1");
// fish1 Sings sadly.
fish1.sing();Both bird and fish are Animal type but their sing is different as implemented in each subclass. We can also override the parent’s implementation of the walk. All these are the polymorphism of the OOP.
Another usage is to do the type casting – but this is usually a hint of code smell.
1 2 3 4 5 6 | Bird bird2 = (Bird)AnimalFactory.createAnimal("BIRD", "bird2"); // bird2 Flies. bird2.fly(); Fish fish2 = (Fish)AnimalFactory.createAnimal("FISH", "fish2"); // fish2 swims fish2.swim(); |
Bird bird2 = (Bird)AnimalFactory.createAnimal("BIRD", "bird2");
// bird2 Flies.
bird2.fly();
Fish fish2 = (Fish)AnimalFactory.createAnimal("FISH", "fish2");
// fish2 swims
fish2.swim();The Animal type is casted into either Bird or Fish. Usually, it makes more sense to type casting a child class to its parent (if they belong to the same inheritance tree).
Obviously, you can’t convert Bird to Fish or vice versa.
1 2 3 | Bird bird2 = (Bird) AnimalFactory.createAnimal("BIRD", "bird2"); Animal fish2 = bird2; ((Fish)fish2).sing(); |
Bird bird2 = (Bird) AnimalFactory.createAnimal("BIRD", "bird2");
Animal fish2 = bird2;
((Fish)fish2).sing();This will result a runtime exception:
Exception in thread “main” java.lang.ClassCastException: com.company.Bird cannot be cast to com.company.Fish
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:数学题:一只蚂蚁从墙根竖直向上爬到墙头用了4分钟 一位农妇上午挎了一个空篮子笑眯眯地回家 奥数题:秋游时,小红小玲小芳三个好朋友在一个小组一起活动 平年和闰年自测题 数学题:李爷爷家住在半山腰 数学题:无线电元件厂验收一批零件 数学题:调查发现,该学校七年级参加魔方比赛的学生中 数学题:甲乙两辆客车同时从东站开往西站 数学题:养猪大户王师傅说他的猪卖75头 数学题:下图是一个正方形,边长是6厘米
- 评论列表
-
- 添加评论