当前位置:网站首页>模式-“里氏替换原则”
模式-“里氏替换原则”
2022-07-05 20:48:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
里氏替换原则是,同组的有类的两个子类,在使用子类A这个地方(方法/属性)您可以使用子类 B代替.对于面向接口编程,我只需要确保同样的行为代码;基类的所有子类必须全部 实现,换过来,子类的方法基类不一定都有; 如果:有一个基类Base;其子类是Concrete;那么method(Base b)的调用能够转换成 method(Concrete c); 策略模式:一般,我们把解决某个问题的方法称为一个”算法”,而把解决一类问题的算法封装 成一个接口,那么实现算法的多种方式作为子类;在某个时候,在调用中我们使用算法A替换 算法B,这就是策略模式在里氏代换原则中的应用; ***************策略模式************************************ >准备一组算法,并将每个封装起来使的他们能够互换.
Context
/**
* @author Lean @date:2014-10-17
*/
public class Context {
public static Strategy strategy;
public static void main(String[] args) {
strategy=new ConcreteStrategyA();
strategy.calculate();
}
}
abstract class Strategy{
public abstract void calculate();
}
class ConcreteStrategyA extends Strategy{
@Override
public void calculate() {
System.out.println("ConcreteStrategyA is called !");
}
}
class ConcreteStrategyB extends Strategy{
@Override
public void calculate() {
System.out.println("ConcreteStrategyB is called !");
}
IChoiceStrategy
/**
* 选择策略
*
* @author Lean @date:2014-10-17
*/
public abstract class IChoiceStrategy {
/**
* @return 返回列表名字
*/
public abstract String[] getNames();
/**
* @return 返回相应码
*/
public abstract int getCode(String name);
}
/**
* @author Lean @date:2014-10-17
*/
public class SortChoiceStrategy extends IChoiceStrategy {
private HashMap<String, Integer> mSortMap;
public String name;
public SortChoiceStrategy() {
initSortMap();
}
private void initSortMap() {
mSortMap=new HashMap<String, Integer>();
mSortMap.put("最新上架", 0);
mSortMap.put("销量最高", 1);
mSortMap.put("价格最高", 2);
mSortMap.put("价格最低", 3);
name="最新上架";
}
@Override
public String[] getNames() {
Set<String> set=mSortMap.keySet();
Object[] tempObj=set.toArray();
String[] result=new String[tempObj.length];
for (int i = 0; i < tempObj.length; i++) {
result[i]=(String) tempObj[i];
}
return result;
}
@Override
public int getCode(String name) {
return mSortMap.get(name);
}
}
/**
* @author Lean @date:2014-10-17
*/
public class StatusChoiceStrategy extends IChoiceStrategy {
private HashMap<String, Integer> mStatusMap;
public String name;
public StatusChoiceStrategy() {
initStatusMap();
}
private void initStatusMap() {
mStatusMap=new HashMap<String, Integer>();
mStatusMap.put("定制中", 1);
mStatusMap.put("已完毕", 2);
name="定制中";
}
@Override
public String[] getNames() {
Set<String> set=mStatusMap.keySet();
Object[] tempObj=set.toArray();
String[] result=new String[tempObj.length];
for (int i = 0; i < tempObj.length; i++) {
result[i]=(String) tempObj[i];
}
return result;
}
@Override
public int getCode(String name) {
return mStatusMap.get(name);
}
}
***********************************************************
代理模式:代理和被代理对象相同拥有一样的行为,我们把它封装成一个接口,那么,在被 代理对象被调用的地方都能够使用代理对象替换以隐藏实现细节; ***************代理模式************************************ 如果一个场景,A想去买票,但A没时间,于是A托B到电影院帮他买票; 换成面向对象思维:如果有一个对象A,和一个新的对象C,如今C想使用对象A,而A临时还 不符合C的要求,这时能够间接的使用B以达到使用A的目的,同一时候,B又能够对使用过程进行
拦截,如打印日志;(像这样的利用中间层来达到目的的模式还有适配器模式)
/**
* @author Lean @date:2014-10-17
*/
public abstract class IPerson {
public abstract void buyTicket();
}
/**
* @author Lean @date:2014-10-17
*/
public class RealSeePerson extends IPerson {
@Override
public void buyTicket() {
System.out.println("RealSeePerson get the ticket !");
}
}
/**
* @author Lean @date:2014-10-17
*/
public class BuyTicketPerson extends IPerson{
public RealSeePerson target;
private void preBuyTicket(){
//TODO do th. before buy ticket
target=new RealSeePerson();
}
@Override
public void buyTicket() {
preBuyTicket();
if (target!=null) {
target.buyTicket();
}
postBuyTicket();
}
public void postBuyTicket(){
//TODO do th. after buy thicket
}
}
>代理和被代理对象实现共同接口,代理对象被调用时调用被代理对象的托付;
动态代理实现监听:
/**
* @author Lean @date:2014-10-17
*/
public class VectorProxy implements InvocationHandler {
private Object proxyobj;
public VectorProxy(Object obj) {
proxyobj=obj;
}
public static Object factor(Object obj){
Class cls=obj.getClass();
return Proxy.newProxyInstance(cls.getClassLoader(),cls.getInterfaces(),new VectorProxy(obj));
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("method:"+method.getName());
if (args!=null) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]+"");
}
}
//反射调用
Object obj=method.invoke(proxyobj, args);
System.out.println("*********");
return obj;
}
public static void main(String[] args) {
List v=null;
//返回代理对象,并调用代理对象的add方法
v=(List) factor(new Vector(10));
v.add("New");
}
}
***********************************************************
合成模式:上面两中模式都是里氏代换原则在方法方面的应用.合成模式使用树结果描写叙述 总体和部分的关系,由于单纯元素和复合元素相同实现抽象,那么在抽象使用的地方, 都能够这2种元素替代; ***************合成模式************************************
合成模式分为透明式和安全式
透明:指抽象接口声明了枝叶全部的全部接口方法,在叶子类中。对该方法进行空实现;
/**
* @author Lean @date:2014-10-20
*/
public interface Component {
void sampleOperation();
Composite getComposite();
void add(Component component);
void remove(Component component);
Enumeration<Component> components();
}
/**
* @author Lean @date:2014-10-20
*/
public class Composite implements Component {
private Vector<Component> componentVector=new Vector<Component>();
@Override
public Composite getComposite() {
return this;
}
@Override
public void sampleOperation() {
Enumeration<Component> enumeration=components();
while (enumeration.hasMoreElements()) {
Component component = (Component) enumeration.nextElement();
component.sampleOperation();
}
}
@Override
public void add(Component component) {
componentVector.addElement(component);
}
@Override
public void remove(Component component) {
componentVector.removeElement(component);
}
@Override
public Enumeration<Component> components() {
return componentVector.elements();
}
}
/**
* @author Lean @date:2014-10-20
*/
public class Leaf implements Component {
@Override
public Composite getComposite() {
return null;
}
@Override
public void sampleOperation() {
System.out.println(" call leaf here !");
}
@Override
public void add(Component component) {
}
@Override
public void remove(Component component) {
}
@Override
public Enumeration<Component> components() {
return null;
}
}
安全:指抽象接口仅仅声明叶子全部的方法,树枝类除了继承还包含了自己的管理叶子类方法;典型应用:Android的View,ViewGroup
/**
* @author Lean @date:2014-10-20
*/
public interface Component {
Composite getComposite();
void sampleOperation();
}
/**
* @author Lean @date:2014-10-20
*/
public class Composite implements Component {
private Vector componentVector=new Vector();
@Override
public void sampleOperation() {
Enumeration enumeration=components();
while (enumeration.hasMoreElements()) {
((Component) enumeration.nextElement()).sampleOperation();
}
}
@Override
public Composite getComposite() {
return this;
}
public Enumeration components(){
return componentVector.elements();
}
public void add(Component component){
componentVector.addElement(component);
}
public void remove(Component component){
componentVector.removeElement(component);
}
}
/**
* @author Lean @date:2014-10-20
*/
public class Leaf implements Component {
@Override
public Composite getComposite() {
return null;
}
@Override
public void sampleOperation() {
System.out.println("leaf is called !");
}
}
由于分支机构基本继承抽象类,在抽象方法只支持类,那里可以换成一个叶子。例如,在安全模式。叶类可以换成棒/多叶;在透明模式下可以互换。这是与里氏代换原则;
***********************************************************
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117666.html原文链接:https://javaforall.cn
边栏推荐
- 中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
- Abbkine BCA法 蛋白质定量试剂盒说明书
- Leetcode (347) - top k high frequency elements
- 科普|英语不好对NPDP考试有影响吗 ?
- Applet event binding
- ClickHouse 复制粘贴多行sql语句报错
- Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
- Abnova blood total nucleic acid purification kit pre installed relevant instructions
- Nprogress plug-in progress bar
- Abnova丨CRISPR SpCas9 多克隆抗体方案
猜你喜欢
Use of thread pool
【刷题记录】1. 两数之和
Applet page navigation
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
phpstudy小皮的mysql点击启动后迅速闪退,已解决
1. Strengthen learning basic knowledge points
Abnova blood total nucleic acid purification kit pre installed relevant instructions
Interpreting the daily application functions of cooperative robots
随机推荐
Ros2 topic [01]: installing ros2 on win10
sql系列(基础)-第二章 限制和排序数据
How to open an account online for futures? Is it safe?
Duchefa丨P1001植物琼脂中英文说明书
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
Classic implementation of the basic method of intelligent home of Internet of things
如何让化工企业的ERP库存账目更准确
Make Jar, Not War
最长摆动序列[贪心练习]
Welcome to the game and win rich bonuses: Code Golf Challenge officially launched
Applet global configuration
台风来袭!建筑工地该如何防范台风!
解读协作型机器人的日常应用功能
教你自己训练的pytorch模型转caffe(二)
Abnova blood total nucleic acid purification kit pre installed relevant instructions
Material Design组件 - 使用BottomSheet展现扩展内容(二)
Abnova 环孢素A单克隆抗体,及其研究工具
Redis唯一ID生成器的实现