https://gitee.com/Jasonpupil/designPatterns
if else
进行判断实现不好维护访客用户(视频480p高清,并有广告)
、普通用户(视频720p超清,并有广告)
和VIP用户(视频1080p蓝光,没有广告)
if else
处理方式示例代码:/**
* @Description: 视频用户服务
* @Author: pupil
* @Date: 2024/06/21 下午 8:12
*/
public class VideoUserService {
public void serveGrade(String userType){
if ("VIP用户".equals(userType)){
System.out.println("VIP用户,视频1080p蓝光");
}else if ("普通用户".equals(userType)){
System.out.println("普通用户,视频720p超清");
}else if ("访客用户".equals(userType)){
System.out.println("访客用户,视频480p高清");
}
}
}
/**
* @description:测试验证
* @author: pupil
* @data: 2024/06/21 下午 7:57
*/
public class ApiTest {
public static void main(String[] args) {
VideoUserService service = new VideoUserService();
service.serveGrade("VIP用户");
service.serveGrade("普通用户");
service.serveGrade("访客用户");
}
}
单一职责原则
处理方式示例代码:/**
* @Description: 视频用户服务接口
* @Author: pupil
* @Date: 2024/06/21 下午 8:17
*/
public interface IVideoUserService {
void definition();
void advertisement();
}
/**
* @Description: 访客用户
* @Author: pupil
* @Date: 2024/06/21 下午 8:18
*/
public class GuestVideoUserServiceImpl implements IVideoUserService {
@Override
public void definition() {
System.out.println("访客用户,视频480p超清");
}
@Override
public void advertisement() {
System.out.println("访客用户,视频有广告");
}
}
/**
* @Description: 普通用户
* @Author: pupil
* @Date: 2024/06/21 下午 8:19
*/
public class OrdinaryVideoUserServiceImpl implements IVideoUserService {
@Override
public void definition() {
System.out.println("普通用户,视频720p超清");
}
@Override
public void advertisement() {
System.out.println("普通用户,视频有广告");
}
}
/**
* @Description: VIP用户
* @Author: pupil
* @Date: 2024/06/21 下午 8:20
*/
public class VipVideoUserServiceImpl implements IVideoUserService {
@Override
public void definition() {
System.out.println("VIP用户,视频1080p蓝光");
}
@Override
public void advertisement() {
System.out.println("VIP会员,视频无广告");
}
}
/**
* @Description: 测试验证
* @Author: pupil
* @Date: 2024/06/21 下午 8:24
*/
public class ApiTest {
public static void main(String[] args) {
IVideoUserService guest = new GuestVideoUserServiceImpl();
guest.definition();
guest.advertisement();
System.out.println("=================================");
IVideoUserService ordinary= new OrdinaryVideoUserServiceImpl();
ordinary.definition();
ordinary.advertisement();
System.out.println("=================================");
IVideoUserService vip = new VipVideoUserServiceImpl();
vip.definition();
vip.advertisement();
}
}
接口和类是实现关系