Design Patterns 设计模式
1. What are the types of design patterns in Java?
Java design patterns are divided into three categories
- Creational patterns provide object creation mechanisms that increase flexibility and reuse of existing code.
- Structural patterns explain how to assemble objects and classes into larger structures, while keeping these structures flexible and efficient.
- Behavioral patterns take care of effective communication and the assignment of responsibilities between objects.
2. How can you achieve thread-safe singleton patterns in Java?
To implement a thread-safe Singleton with lazy loading
- static volatile
variable
- make
constructor
be private - static synchornized
getInstance
method - make sure thread safe
public class Singleton {
private static volatile Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
3. Design Problem
We need to send notification to users by notification preference like email/SMS etc,handle error appropriately when preference given does not match.Write simple java program using design patterns, program to interface & Error handling concepts to come up with solve for above problem.
- Notification type: email/SMS/WhatsAPP
- Call notification module to send notification
- Integrate with notification module to notify depending on preference.
- if A prefer EMAIL, then we notify A by EMAIL
- Pass user details and other data points to send email
- Notificaiton content :
- Hey firstName, you have successfully registered to add and here is your
<loginName>
, please use this for future references.
- Hey firstName, you have successfully registered to add and here is your
- Broadcast public notification to all users
- 40% off when you buy Popeyes between 06/13 - 06/19
4. Factory Design Pattern in Java
在Java中,工厂设计模式用于创建对象,封装对象创建的细节,从而使客户端代码与具体的对象实现解耦。
一般来说,工厂设计模式包括以下几个角色:
- 抽象工厂 Abstract Factory: 声明用于创建一组相关对象的接口。
- 具体工厂 Concrete Factory: 实现抽象工厂接口,用于创建一组具体的对象。
- 抽象产品 Abstract Product: 声明具体产品的接口。
- 具体产品 Concrete Product: 实现抽象产品的接口,具体的产品由具体工厂创建。
// 抽象产品:汽车
interface Car {
void drive();
}
// 具体产品:宝马
class BMW implements Car {
@Override
public void drive() {
System.out.println("Driving BMW...");
}
}
// 具体产品:奔驰
class Benz implements Car {
@Override
public void drive() {
System.out.println("Driving Benz...");
}
}
// 抽象工厂:汽车工厂
interface CarFactory {
Car createCar();
}
// 具体工厂:宝马工厂
class BMWFactory implements CarFactory {
@Override
public Car createCar() {
return new BMW();
}
}
// 具体工厂:奔驰工厂
class BenzFactory implements CarFactory {
@Override
public Car createCar() {
return new Benz();
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
// 创建宝马工厂
CarFactory bmwFactory = new BMWFactory();
// 生产宝马汽车
Car bmw = bmwFactory.createCar();
// 驾驶宝马汽车
bmw.drive();
// 创建奔驰工厂
CarFactory benzFactory = new BenzFactory();
// 生产奔驰汽车
Car benz = benzFactory.createCar();
// 驾驶奔驰汽车
benz.drive();
}
}