English 中文(简体)
适应模式和依赖性
原标题:adapter pattern and dependency

I have little doubt about adapter class. I know what s the goal of adapter class. And when should be used. My doubt is about class construction. I ve checked some tutorials and all of them say that I should pass "Adaptee" class as a dependency to my "Adapter". e.g.

Class SampleAdapter implements MyInterface
{
    private AdapteeClass mInstance;
    public SampleAdapter(AdapteeClass instance)
    {
         mInstance=instance;
    }
}

这个例子从血清中.。 由于你能够看到改编版地图被传给我的物体是依赖性的。 问题为何? 如果一刀切改变物体的界面 很显然,我会使用“新的”接口,我赢得了“旧”的需求。 我为什么需要在我的适应者之外创建“老”班级。 有些人可能说,我应使用依赖注射,以便我能通过我所希望的任何东西,但这是适应者——我需要改变具体类别的接口。 我认为,从个人角度讲,法典的节奏较好。

Class SampleAdapter implements MyInterface
{
    private AdapteeClass mInstance;
    public SampleAdapter()
    {
         mInstance= new AdapteeClass();
    }
}

你们的意见是什么?

最佳回答

我要说的是,在涉及复杂物体时,你应始终避免新经营者(除非该类别为<代码>Builder或>Factory),以减少 coup合,使你的代码更能测试。 可在班级方法内制造诸如清单或传真或价值物体等物体(这很可能是班级方法的目的!)

Lets say for example that your AdapteeClass is a Remote Proxy. If you want to use Unit Testing, your unit tests will have to use the real proxy class because there is no way to replace it in your unit tests.

如果你采用第一种办法,你在进行单位测试时可以很容易地向施工者注入一ck或ake,以便你能够测试所有代码道路。

页: 1

B. 不可检测守则的信号

  • new keyword in a constructor or at field declaration
  • Static method calls in a constructor or at field declaration
  • Anything more than field assignment in constructors
  • Object not fully initialized after the constructor finishes (watch out for initialize methods)
  • Control flow (conditional or looping logic) in a constructor
  • Code does complex object graph construction inside a constructor rather than using a factory or builder
  • Adding or using an initialization block
问题回答

<代码>AdapteeClass可有一个或多个非属地建筑商。 在这种情况下,你需要把所有这些内容复制到<>SampleAdapter的构造中,以便具有同样的灵活性。 穿过已经建造的物体更为简单。

我认为,在适应者内部创建适应者受到限制。 如果你们有一天想调整以前存在的事例?

然而,如果真心实意的话,我就这样做了。

Class SampleAdapter implements MyInterface
{

    private AdapteeClass mInstance;

    public SampleAdapter()
       : base (new AdapteeClass())
    {
    }

    public SampleAdapter(AdapteeClass instance)
    {
         mInstance=instance;
    }
}

让我假定,有一个固定的美国港是外部的硬车,你正试图把它 h在一个只有类型港口的Mac。 是的,你可以购买新的轮驱动器,该轮驱动器拥有一个类型港口,但其中的数据如何?

适应模式也是如此。 你们用吨的lav子开始做改编。 当您进行转换时,你想保持一切背景。





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签