标签: composition

设计建议:从包含的对象调用容器对象上的方法

我有一个简单的设置

class Container {
    Handler h;
}
Run Code Online (Sandbox Code Playgroud)

所有Container对象都有一个"warning()"方法.我想有一种方法从Handler对象中输出警告,但是使用包含对象的工具发送这些警告.

我确实意识到在包含的对象中持有对容器的引用是奇怪的(通常包含的对象不应该知道它的容器的任何内容).现在,在一个带闭包的语言中我会这样做(假想的语法):

h.set_warning_handler { | char* message |
    this->warning(message)
}
Run Code Online (Sandbox Code Playgroud)

但我在C++工作,它不是一个像块一样使用Apple方言的地方.解决这个问题的首选方法是什么?或者只是设置参考并忘记它?

c++ oop composition

1
推荐指数
1
解决办法
96
查看次数

使用组合而不是继承时 Comparator<T> 的替代方法是什么?

在重构 Java 项目以使用组合而不是继承时,我仍然存在执行集合的多态排序的问题。

继承示例:

public class AClass
{
    private List<OneClassOfManyThatRequireSortedInstances> unsortedList;

    public List<OneClassOfManyThatRequireSortedInstances> getSortedList()
    {
        List<OneClassOfManyThatRequireSortedInstances> sortedList = new ArrayList(this.unsortedList);
        Collections.sort(sortedList, SuperClassOfManyThatRequireSortedInstances.orderComparator);

        return sortedList;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,经过重构;类OneClassOfManyThatRequireSortedInstances不再从抽象继承,SuperClassOfManyThatRequireSortedInstances而是Collections.sort()期望相互比较的实例。

重构它的最佳方法是什么?

编辑:

为了完整性;我添加了 Comparator 实现并进一步说明了问题。

public class SuperClassOfManyThatRequireSortedInstances
{
    private int order;

    public static final Comparator<SuperClassOfManyThatRequireSortedInstances> orderComparator = new Comparator<SuperClassOfManyThatRequireSortedInstances>()
    {
        public int compare(SuperClassOfManyThatRequireSortedInstances o1, SuperClassOfManyThatRequireSortedInstances o2)
        {
            if ((o1 == null) && (o2 == null))
            {
                return 0;
            }
            if (o1 == null)
            {
                return -1; …
Run Code Online (Sandbox Code Playgroud)

java sorting collections inheritance composition

1
推荐指数
1
解决办法
1544
查看次数

haskell评价$ sign

我正在经历'了解你一些haskell'并且我写了以下应用程序:

import System.IO

main = do
    filename <- getLine
    handle <- openFile filename ReadMode
    content <- hGetContents handle
    putStr . unlines . (map isLong) . lines $ content
    hClose handle

isLong :: String -> String
isLong x = if length x > 10 then x ++ " long enough" else x ++ " could be better!"
Run Code Online (Sandbox Code Playgroud)

它可以工作,但当我删除行和内容之间的"$"时,编译失败.

你能帮我理解为什么这是错的吗?

我想我用点组成语句,然后我得到一个函数(String - > IO())并将其应用于"内容",但为什么这里需要"$"?

谢谢!

haskell composition operator-keyword

1
推荐指数
1
解决办法
69
查看次数

如何在 Golang 中调用父方法并传递任何扩展父结构作为参数的子方法

我还在学习Golang,想请教一些事情。是否可以执行类似的操作并将任何其他子级传递给扩展 Parent 结构的 PMethod ?

type Parent struct{
   PAttribute string
}

func (p *Parent) PMethod(c *Child){
   fmt.Println("this is parent Attribute : " + p.PAttribute)
   fmt.Println("this is child Attribute : " + c.CAttribute)
}

type Child struct{
   Parent
   CAttribute string
}

type Child2 struct{
   Parent
   CAttribute string
}


func main(){
   c := Child{
         Parent{
            "parent"
         },
         "child",
        }
   c.PMethod(&c)

   c2 := Child2{
         Parent{
            "parent"
         },
         "child",
        }
   c2.PMethod(&c2)
}
Run Code Online (Sandbox Code Playgroud)

谢谢

composition go

1
推荐指数
1
解决办法
3114
查看次数

如何在 Typescript 4 中编写咖喱和撰写?

在经历了可变参数类型之后,我想做这个,但我想知道如何使用函数数组。

这是我的第一次尝试:

function curry<T extends any[]>(fn: (...args: T) => any) {
  return function(...args: T) {
    return args.length >= fn.length
      ? fn(...args)
      : curry(fn.bind(undefined, ...args));
  }
}
Run Code Online (Sandbox Code Playgroud)

但是因为fn.bind我得到“'(...args: T) => any' 类型的'this' 上下文不能分配给'(this: undefined, ...args: any[ ]) => 任何'。”

有任何想法吗?

functional-programming currying composition typescript

1
推荐指数
1
解决办法
1006
查看次数

PHP5类:继承一个组合类?

我正努力让以下工作,但我不知所措......

class Foo {
    public $somethingelse;

    function __construct() {
        echo 'I am Foo';
    }
    function composition() {
        $this->somethingelse =& new SomethingElse();
    }
}
Run Code Online (Sandbox Code Playgroud)
class Bar extends Foo {
    function __construct() {
        echo 'I am Bar, my parent is Foo';
    }
}
Run Code Online (Sandbox Code Playgroud)
class SomethingElse {
    function __construct() {
        echo 'I am some other class';
    }
    function test() {
        echo 'I am a method in the SomethingElse class';
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是在类Foo中创建SomethingElse类的实例.这适用于=&.但是当我用类Bar扩展类Foo时,我认为子类继承了父类的所有数据属性和方法.但是,似乎$this->somethingelse在子类Bar中不起作用:

$foo = new Foo(); // …
Run Code Online (Sandbox Code Playgroud)

php inheritance composition

0
推荐指数
1
解决办法
471
查看次数

C++设计(基类中的行为,派生类中提供的私有成员)

我有6个类,它们都执行相同的操作.我想将常见行为转移到一个共同的[基础]类.

要对6个单独的对象执行操作.六个对象位于派生类中.直观地,私有成员对象将通过基类中的子(派生类)进行访问.

我在寻找什么是C++模式?

class Base
{
  // Common behavior, operate on m_object
  ...
  void Foo()
  {
    m_object.Bar();
  }
};

class Derived1 : public Base
{
  // No methods, use Base methods

  private:
  MyObject1 m_object;
}

class Derived2 : public Base
{
  // No methods, use Base methods

  private:
  MyObject2 m_object;
}
Run Code Online (Sandbox Code Playgroud)

这是拳击我到这种情况的事情是MyObject1,MyObject2等报价Bar(),但不共享一个共同的基类.我真的无法修复派生,因为对象来自外部库.

c++ inheritance composition

0
推荐指数
1
解决办法
343
查看次数

C++中的构造函数

我的问题是:例如我有两个类x和y

class X{
public:
      X(int, int, string);

private: int a;
         int b;
         string c;
};

class Y{

private: X x[10];
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何通过Y的构造函数初始化这个10 x对象的数组?初始化列表?如何在y中初始化x的这10个对象.

c++ composition initializer-list

0
推荐指数
1
解决办法
509
查看次数

ES6类中的访问者组成

比方说,我有一个Thing,我想既类HideableOpenable.

使用类似于Douglas Crockford通过合成创建对象的方法,我已经能够从多个类"继承".

这种方法不适用于访问器(getter/setter).

我需要使用类,因为它是一个要求.我也发现我在类之间复制功能,但我不希望它们从基类继承.

有任何想法吗?

到目前为止我所取得的进展如下:

class Openable {

  constructor(isOpen = false) {
    this._isOpen = isOpen;
  }

  get isOpen() {
    return this._isOpen + ' is stupid.';
  }

  set isOpen(value) {
    this._isOpen = value;
  }

}


class Hideable {

  constructor(isHidden = false) {
    this._isHidden = isHidden;
  }

  get isHidden() {
    return this._isHidden + ' is stupid.';
  }

  set isHidden(value) {
    this._isHidden = value;
  }

}


class Thing {

  constructor(config) {
    let { …
Run Code Online (Sandbox Code Playgroud)

javascript class composition mixins ecmascript-6

0
推荐指数
1
解决办法
1131
查看次数