当前位置: 首页 > news >正文

网页设计搭建网站哪些网站可以免费做产品推广

网页设计搭建网站,哪些网站可以免费做产品推广,百度登陆页面,荆门刚刚发布的引言 设计模式是软件工程中用于解决常见问题的可复用解决方案。在C#编程中#xff0c;常见的设计模式具有广泛的应用。本篇博客将重点介绍C#中常见的结构型设计模式#xff0c;包括适配器模式、装饰器模式、代理模式、组合模式和享元模式。 目录 引言1. 适配器模式(Adapter …引言 设计模式是软件工程中用于解决常见问题的可复用解决方案。在C#编程中常见的设计模式具有广泛的应用。本篇博客将重点介绍C#中常见的结构型设计模式包括适配器模式、装饰器模式、代理模式、组合模式和享元模式。 目录 引言1. 适配器模式(Adapter Pattern)示例代码解释 2. 桥接模式Bridge Pattern示例代码解释 3. 外观模式Facade示例代码解释 4. 装饰器模式(Decorator Pattern)示例代码解释 5. 代理模式(Proxy Pattern)示例代码解释 6. 组合模式(Composite Pattern)示例代码解释 7. 享元模式(Flyweight Pattern)示例代码解释 结论 1. 适配器模式(Adapter Pattern) 适配器模式用于将一个类的接口转换成客户端所期望的另一个接口。这种模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。 示例代码 // 目标接口 interface ITarget {void Request(); }// 需要被适配的类 class Adaptee {public void SpecificRequest(){Console.WriteLine(Adaptees SpecificRequest);} }// 适配器类 class Adapter : ITarget {private Adaptee _adaptee;public Adapter(Adaptee adaptee){_adaptee adaptee;}public void Request(){_adaptee.SpecificRequest();} }// 客户端代码 class Client {static void Main(string[] args){Adaptee adaptee new Adaptee();ITarget target new Adapter(adaptee);target.Request();} }解释 适配器模式中的目标接口(ITarget)定义了客户端所期望的方法。Adaptee类是需要被适配的类其中包含了一个SpecificRequest方法。Adapter类则是适配器持有一个Adaptee对象的引用并实现了ITarget接口。在Adapter的Request方法中调用了Adaptee的SpecificRequest方法。在客户端代码中创建了一个Adaptee对象和一个Adapter对象然后通过Adapter对象去调用Request方法。 2. 桥接模式Bridge Pattern 桥接模式是一种结构型设计模式用于将抽象与实现隔离开来使它们能够独立变化。它通过组合的方式代替继承从而降低了系统的耦合性。该模式适合在需要多个维度进行扩展时使用例如在图形界面库中将窗口与不同操作系统的窗口装饰风格进行分离。桥接模式的核心思想是将抽象部分与实现部分分离使它们可以独立地变化和演化。 示例代码 // 实现部分接口 interface Implementor {void operationImpl(); }// 具体实现类A class ConcreteImplementorA implements Implementor {Overridepublic void operationImpl() {System.out.println(ConcreteImplementorA operation implementation);} }// 具体实现类B class ConcreteImplementorB implements Implementor {Overridepublic void operationImpl() {System.out.println(ConcreteImplementorB operation implementation);} }// 抽象部分 abstract class Abstraction {protected Implementor implementor;public void setImplementor(Implementor implementor) {this.implementor implementor;}public abstract void operation(); }// 扩展抽象部分的具体类 class RefinedAbstraction extends Abstraction {Overridepublic void operation() {implementor.operationImpl();} }// 使用示例 public class BridgePatternExample {public static void main(String[] args) {Implementor implementorA new ConcreteImplementorA();Implementor implementorB new ConcreteImplementorB();Abstraction abstraction new RefinedAbstraction();abstraction.setImplementor(implementorA);abstraction.operation();abstraction.setImplementor(implementorB);abstraction.operation();} }解释 在上述示例中​Implementor​ 接口定义了实现部分的操作方法。​ConcreteImplementorA​ 和ConcreteImplementorB​ 是具体的实现类。​Abstraction​ 是抽象部分的定义其中包含一个实现部分的通用接口并有一个抽象方法 ​operation()​ 来定义具体的操作。​RefinedAbstraction​ 是具体的扩展抽象类实现了抽象方法并通过组合关联了具体的实现类。 3. 外观模式Facade 外观模式是一种结构型设计模式提供了一个统一的接口用来访问子系统中的一群接口。外观模式定义了一个高层接口让子系统更容易使用。。 示例代码 // 子系统A class SubsystemA {public void operationA() {System.out.println(SubsystemA operation);} }// 子系统B class SubsystemB {public void operationB() {System.out.println(SubsystemB operation);} }// 子系统C class SubsystemC {public void operationC() {System.out.println(SubsystemC operation);} }// 外观类 class Facade {private SubsystemA subsystemA;private SubsystemB subsystemB;private SubsystemC subsystemC;public Facade() {subsystemA new SubsystemA();subsystemB new SubsystemB();subsystemC new SubsystemC();}public void operation() {subsystemA.operationA();subsystemB.operationB();subsystemC.operationC();} }// 使用示例 public class FacadePatternExample {public static void main(String[] args) {Facade facade new Facade();facade.operation();} }解释 在上述示例中​SubsystemA​、​SubsystemB​ 和 ​SubsystemC​ 是不同的子系统它们分别提供了不同的操作。​Facade​ 是外观类隐藏了子系统的复杂性为客户端提供了一个简单的接口。在外观类的操作方法中调用了多个子系统的操作。 4. 装饰器模式(Decorator Pattern) 装饰器模式允许向一个现有对象添加新功能同时又不改变其结构。它是通过创建一个包装对象来包裹真实对象从而对真实对象进行功能的扩展。 示例代码 // 抽象组件接口 interface IComponent {void Operation(); }// 具体组件类 class ConcreteComponent : IComponent {public void Operation(){Console.WriteLine(ConcreteComponents Operation);} }// 抽象装饰者类 abstract class Decorator : IComponent {protected IComponent _component;public Decorator(IComponent component){_component component;}public virtual void Operation(){_component.Operation();} }// 具体装饰者类 class ConcreteDecoratorA : Decorator {public ConcreteDecoratorA(IComponent component): base(component){}public override void Operation(){base.Operation();AddedBehavior();}private void AddedBehavior(){Console.WriteLine(ConcreteDecoratorAs AddedBehavior);} }class ConcreteDecoratorB : Decorator {public ConcreteDecoratorB(IComponent component): base(component){}public override void Operation(){base.Operation();AddedBehavior();}private void AddedBehavior(){Console.WriteLine(ConcreteDecoratorBs AddedBehavior);} }// 客户端代码 class Client {static void Main(string[] args){IComponent component new ConcreteComponent();component new ConcreteDecoratorA(component);component new ConcreteDecoratorB(component);component.Operation();} }解释 装饰器模式中的抽象组件接口(IComponent)定义了被装饰者和装饰者之间的公共方法。ConcreteComponent类是具体的组件类实现了IComponent接口的Operation方法。Decorator类是抽象装饰者类持有一个IComponent对象的引用并实现了IComponent接口。ConcreteDecoratorA和ConcreteDecoratorB类分别是具体装饰者类它们继承自Decorator类并在调用父类Operation方法的同时添加了额外的行为。 在客户端代码中首先创建了一个ConcreteComponent对象然后通过多次进行装饰分别使用ConcreteDecoratorA和ConcreteDecoratorB对其进行包装。最后调用component.Operation()方法时实际上会调用被装饰者的Operation方法并在其基础上添加了额外的行为。 5. 代理模式(Proxy Pattern) 代理模式为其他对象提供一种代理以控制对这个对象的访问。代理模式主要通过代理类来封装目标对象控制客户端对目标对象的访问并在必要的时候进行一些预处理或后处理操作。 示例代码 // 被代理接口 interface ISubject {void Request(); }// 真实对象类 class RealSubject : ISubject {public void Request(){Console.WriteLine(RealSubjects Request);} }// 代理类 class Proxy : ISubject {private RealSubject _realSubject;public void Request(){if (_realSubject null){_realSubject new RealSubject();}PreProcess();_realSubject.Request();PostProcess();}private void PreProcess(){Console.WriteLine(Proxys PreProcess);}private void PostProcess(){Console.WriteLine(Proxys PostProcess);} }// 客户端代码 class Client {static void Main(string[] args){ISubject proxy new Proxy();proxy.Request();} }解释 代理模式中的被代理接口(ISubject)定义了代理对象和真实对象的公共方法。RealSubject类是真实对象类实现了被代理接口的Request方法。Proxy类是代理类持有一个RealSubject对象的引用并实现了被代理接口的Request方法。在Proxy类的Request方法中进行了预处理、调用真实对象的Request方法和后处理。 在客户端代码中创建了一个代理对象Proxy然后通过该对象调用Request方法。在调用过程中会对真实对象进行实例化并在方法执行前后进行相应的预处理和后处理。 6. 组合模式(Composite Pattern) 组合模式将对象组合成树形结构以表示部分-整体的层次结构。组合模式使得客户端对单个对象和组合对象的使用具有一致性。 示例代码 // 抽象组件类 abstract class Component {protected string _name;public Component(string name){_name name;}public abstract void Display(); }// 叶子节点类 class Leaf : Component {public Leaf(string name): base(name){}public override void Display(){Console.WriteLine(_name);} }// 容器节点类 class Composite : Component {private ListComponent _children new ListComponent();public Composite(string name): base(name){}public void Add(Component component){_children.Add(component);}public void Remove(Component component){_children.Remove(component);}public override void Display(){Console.WriteLine(_name);foreach (Component component in _children){component.Display();}} }// 客户端代码 class Client {static void Main(string[] args){Composite root new Composite(root);Leaf leaf1 new Leaf(leaf1);Leaf leaf2 new Leaf(leaf2);Composite composite1 new Composite(composite1);Composite composite2 new Composite(composite2);root.Add(leaf1);root.Add(composite1);composite1.Add(leaf2);composite1.Add(composite2);root.Display();} }解释 组合模式中的抽象组件类(Component)定义了树状结构中所有对象的通用行为的接口。Leaf类是叶子节点类它继承自Component类并实现了Display方法。Composite类是容器节点类它继承自Component类并持有一组Component对象。在Composite类的Display方法中首先输出自身信息然后递归调用所有子节点的Display方法。 在客户端代码中首先创建了一个根节点root和一些叶子节点和容器节点。通过调用容器节点的Add方法可以将其他节点添加到其内部。最后调用root.Display()方法会递归地展示整个树状结构。 7. 享元模式(Flyweight Pattern) 享元模式是一种池技术主要用于减少创建对象的数量以减少内存占用和提高性能。享元模式通过共享已创建的对象避免重复创建相同的对象。 示例代码 // 享元接口 interface IFlyweight {void Operation(); }// 具体享元类 class ConcreteFlyweight : IFlyweight {private readonly string _intrinsicState;public ConcreteFlyweight(string intrinsicState){_intrinsicState intrinsicState;}public void Operation(){Console.WriteLine($ConcreteFlyweights Operation with {_intrinsicState});} }// 享元工厂类 class FlyweightFactory {private Dictionarystring, IFlyweight _flyweights new Dictionarystring, IFlyweight();public IFlyweight GetFlyweight(string key){if (_flyweights.ContainsKey(key)){return _flyweights[key];}else{IFlyweight flyweight new ConcreteFlyweight(key);_flyweights.Add(key, flyweight);return flyweight;}} }// 客户端代码 class Client {static void Main(string[] args){FlyweightFactory factory new FlyweightFactory();IFlyweight flyweight1 factory.GetFlyweight(key1);flyweight1.Operation();IFlyweight flyweight2 factory.GetFlyweight(key2);flyweight2.Operation();IFlyweight flyweight3 factory.GetFlyweight(key1);flyweight3.Operation();} }解释 享元模式中的享元接口(IFlyweight)定义了享元类的公共方法。ConcreteFlyweight类是具体享元类它实现了IFlyweight接口并持有一个内部状态(_intrinsicState)。FlyweightFactory类是享元工厂类用于创建和管理享元对象。 在客户端代码中首先创建了一个FlyweightFactory对象。通过调用工厂的GetFlyweight方法可以获取享元对象如果对象已经存在则直接返回已有对象如果对象不存在则创建新的享元对象并将其缓存起来。最后调用享元对象的Operation方法时会输出其内部状态。 结论 结构型设计模式在C#编程中具有广泛的应用。适配器模式用于解决不兼容接口的问题装饰器模式用于动态地扩展对象的功能代理模式用于控制对对象的访问组合模式用于处理树状结构数据享元模式用于减少对象创建的数量。合理使用这些结构型设计模式可以提高代码的可读性、可维护性和可扩展性。 参考资料 Design Patterns in C#
http://www.pierceye.com/news/440799/

相关文章:

  • 免费建立单位的网站适合个人做的跨境电商
  • 沈阳军成网站建设17网站一起做网店
  • 哪些cms做同城网站比较好上海建设工程协会网站
  • 潍坊企业自助建站系统seo博客网站
  • 做啤酒最全的网站鱼台县建设局网站
  • 网站开发转行进入衍生领域wordpress qaengine
  • 公司内部网站模板快速建网站的软件
  • 被骗去国外做网站网站推广网站的运营推广方案
  • 北京汽车业务网站开发公司桂林旅游攻略必去景点
  • 个人网站开发是学什么语言wordpress打造cms
  • 网站建设与维护的重要性岳阳建设厅网站
  • 惠州网站开发公司wordpress简单
  • 外贸网站 免费模板 使用 zencart如何购买域名和备案
  • 网站建设联系我们设计网站无锡
  • 深圳做网站好的公司wordpress建菜单
  • 网站编辑需要的技能做网站需要什么域名
  • 营销型网站建设目的和意义网站托管方案
  • 网站感谢页面企业标志图片大全
  • 响应式网站建设必推全网天下邵阳竞价网站建设设计
  • 大连网站如何制作辽阳公司做网站
  • 百度站长怎么验证网站jekyll做公司网站
  • 电子商务公司建设网站方案设计网站建设开发背景
  • 同一产品做多个网站山西省住房和城乡建设厅官网
  • 网站建设的流程是什么意思微信小程序的代码
  • 广州网站整站优化html项目案例实战
  • 宁波网站推广方式seo优化按天扣费
  • 紫金优化网站制作python编程100例
  • 原阳网站建设哪家好域名网址
  • 西安学校网站建设wordpress手机端模板下载
  • 泉州网站建设工作室网站上的产品板块